[go] Support 'go.work' file and resolve shared deps relative to work path (#9708)

This PR fixes a few issues related to `vc dev`.

1. Create a default `go.work` file in the cache dir when building the `vercel-dev-server-go` executable
2. Copy the existing `go.mod` file into the cache dir and update any "replace" relative paths
3. Split the "build" logic into separate functions for the legacy "main" package build and the `go.mod` build

Additionally, it fixes:

1. `vc build`: pass in `build.env` from `vercel.json`
2. Fix several tests to work with `vc dev` and `vc build`

Linear: https://linear.app/vercel/issue/VCCLI-638/vc-dev-go-builder-cant-resolve-workspace-dependencies

The user that reported the issue has tested this build and seems to fix their use case: https://github.com/vercel/vercel/issues/9393#issuecomment-1490726785.
This commit is contained in:
Chris Barber
2023-04-13 12:59:47 -05:00
committed by GitHub
parent 732ac2072c
commit 38eb0bca04
23 changed files with 729 additions and 425 deletions

View File

@@ -0,0 +1,3 @@
module go-work-with-shared/api
go 1.20

View File

@@ -0,0 +1,12 @@
package handler
import (
"fmt"
"net/http"
"go-work-with-shared/mylib"
)
// Handler function
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, mylib.Say("hello"))
}

View File

@@ -0,0 +1,6 @@
go 1.20
use (
./api/
./mylib/
)

View File

@@ -0,0 +1,3 @@
module go-work-with-shared/mylib
go 1.20

View File

@@ -0,0 +1,9 @@
package mylib
import (
"runtime"
)
func Say(text string) string {
return text + ":" + runtime.Version()
}

View File

@@ -182,6 +182,13 @@ test(
})
);
test(
'[vercel dev] Should support `*.go` API serverless functions with `go.work` and lib',
testFixtureStdio('go-work-with-shared', async (testPath: any) => {
await testPath(200, `/api`, 'hello:go1.20.2');
})
);
test(
'[vercel dev] Should set the `ts-node` "target" to match Node.js version',
testFixtureStdio('node-ts-node-target', async (testPath: any) => {