From 5ffeac369a835eb069c74f18a73232a33fb8eb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Eriksson?= Date: Wed, 9 Aug 2023 17:32:51 +0200 Subject: [PATCH] Fix go 1.21 build (#838) - Upgrade modfile dep - Fix go.mod building --- .reviewdog.yml | 7 ++++--- e2e-tests/testdata/echo/go.mod | 2 +- e2e-tests/testscript_test.go | 8 ++++---- go.mod | 4 ++-- go.sum | 2 ++ runtime/go.mod | 1 + runtime/go.sum | 2 ++ v2/compiler/build/build.go | 1 + v2/compiler/build/tests.go | 1 + v2/internals/pkginfo/loader.go | 22 +++++++++++----------- 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.reviewdog.yml b/.reviewdog.yml index 5f01049b43..74a5d9e16a 100644 --- a/.reviewdog.yml +++ b/.reviewdog.yml @@ -16,9 +16,10 @@ runner: go-fmt: cmd: make -s -C .github/workflows go-fmt format: diff - staticcheck: - cmd: make -s -C .github/workflows staticcheck - format: rdjsonl +# Disable staticcheck until it supports Go 1.21: https://github.com/dominikh/go-tools/issues/1431 +# staticcheck: +# cmd: make -s -C .github/workflows staticcheck +# format: rdjsonl errcheck: cmd: make -s -C .github/workflows errcheck errorformat: diff --git a/e2e-tests/testdata/echo/go.mod b/e2e-tests/testdata/echo/go.mod index 4038a7a37e..bc687ddbae 100644 --- a/e2e-tests/testdata/echo/go.mod +++ b/e2e-tests/testdata/echo/go.mod @@ -1,5 +1,5 @@ module encore.app -go 1.18 +go 1.21.0 require encore.dev v1.9.3 diff --git a/e2e-tests/testscript_test.go b/e2e-tests/testscript_test.go index 44091e9b56..73b3ad4511 100644 --- a/e2e-tests/testscript_test.go +++ b/e2e-tests/testscript_test.go @@ -48,7 +48,7 @@ func doRun(t *testing.T, experiments []string) { e.Setenv("EXTRA_EXPERIMENTS", strings.Join(experiments, ",")) e.Setenv("HOME", home) e.Setenv("GOFLAGS", "-modcacherw") - gomod := []byte("module test\n\nrequire encore.dev v1.13.4") + gomod := []byte("module test\n\ngo 1.21.0\n\nrequire encore.dev v1.13.4") if err := os.WriteFile(filepath.Join(e.WorkDir, "go.mod"), gomod, 0755); err != nil { return err } @@ -86,8 +86,8 @@ func doRun(t *testing.T, experiments []string) { } err := RunTests(getTB(ts), getWorkdir(ts), &log.stdout, &log.stderr, []string{"ENCORE_EXPERIMENT=" + exp}) - os.Stdout.Write(log.stdout.Bytes()) - os.Stderr.Write(log.stderr.Bytes()) + _, _ = os.Stdout.Write(log.stdout.Bytes()) + _, _ = os.Stderr.Write(log.stderr.Bytes()) if !neg && err != nil { ts.Fatalf("tests failed: %v", err) } else if neg && err == nil { @@ -154,7 +154,7 @@ func doRun(t *testing.T, experiments []string) { w := httptest.NewRecorder() app.Run.ServeHTTP(w, req) respBody := w.Body.Bytes() - os.Stdout.Write(respBody) + _, _ = os.Stdout.Write(respBody) if w.Code != http.StatusOK && !neg { ts.Fatalf("unexpected status code: %v: %s", w.Code, respBody) diff --git a/go.mod b/go.mod index f94ea4840f..e7eb8dc45b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module encr.dev -go 1.20 +go 1.21.0 require ( cuelang.org/go v0.4.3 @@ -54,7 +54,7 @@ require ( go4.org v0.0.0-20201209231011-d4a079459e60 golang.org/x/crypto v0.11.0 golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 - golang.org/x/mod v0.10.0 + golang.org/x/mod v0.12.0 golang.org/x/oauth2 v0.10.0 golang.org/x/sync v0.3.0 golang.org/x/sys v0.10.0 diff --git a/go.sum b/go.sum index 6a8a467fc9..aaa24ab0ed 100644 --- a/go.sum +++ b/go.sum @@ -1558,6 +1558,8 @@ golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/runtime/go.mod b/runtime/go.mod index cf69363413..daac0634c2 100644 --- a/runtime/go.mod +++ b/runtime/go.mod @@ -83,6 +83,7 @@ require ( github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect go.opencensus.io v0.24.0 // indirect + golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.13.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect golang.org/x/sync v0.3.0 // indirect diff --git a/runtime/go.sum b/runtime/go.sum index 743f87b001..dc5d141032 100644 --- a/runtime/go.sum +++ b/runtime/go.sum @@ -272,6 +272,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/v2/compiler/build/build.go b/v2/compiler/build/build.go index 342a59e8a7..bdc8677564 100644 --- a/v2/compiler/build/build.go +++ b/v2/compiler/build/build.go @@ -258,6 +258,7 @@ func (b *builder) buildMain() { env = append(env, "GO111MODULE=on", "GOROOT="+goroot.ToIO(), + "GOTOOLCHAIN=local", ) if goos := build.GOOS; goos != "" { env = append(env, "GOOS="+goos) diff --git a/v2/compiler/build/tests.go b/v2/compiler/build/tests.go index ce1d69627d..008754c06c 100644 --- a/v2/compiler/build/tests.go +++ b/v2/compiler/build/tests.go @@ -136,6 +136,7 @@ func (b *builder) runTests() { env = append(env, "GO111MODULE=on", "GOROOT="+goroot.ToIO(), + "GOTOOLCHAIN=local", ) if goos := build.GOOS; goos != "" { env = append(env, "GOOS="+goos) diff --git a/v2/internals/pkginfo/loader.go b/v2/internals/pkginfo/loader.go index 23fcbf84d5..fd4e677b2e 100644 --- a/v2/internals/pkginfo/loader.go +++ b/v2/internals/pkginfo/loader.go @@ -5,10 +5,10 @@ import ( "go/token" "os" "path/filepath" + "strings" "sync" "golang.org/x/exp/slices" - exec "golang.org/x/sys/execabs" "golang.org/x/tools/go/packages" "encr.dev/pkg/paths" @@ -83,7 +83,7 @@ func (l *Loader) init() { cgoEnabled = "1" } - l.handleMissingGo(b) + updateGoPath(b) l.packagesConfig = &packages.Config{ Mode: packages.NeedName | packages.NeedFiles | packages.NeedModule, Context: l.c.Ctx, @@ -178,14 +178,14 @@ func (l *Loader) LoadPkg(cause token.Pos, pkgPath paths.Pkg) (pkg *Package, ok b return result.pkg, result.ok } -// handleMissingGo updates the PATH environment variable to use the -// "go" binary from Encore's GOROOT if the system does not have Go installed. -// This is necessary because packages.Load invokes "go list" under the hood. -func (l *Loader) handleMissingGo(b parsectx.BuildInfo) { - if _, err := exec.LookPath("go"); err == nil { - return +// updateGoPath updates the PATH environment variable to use the +// "go" binary from Encore's GOROOT. +// This is necessary because packages.Load invokes "go list" under the hood, +// and we want to ensure it uses the same 'go' binary as Encore. +func updateGoPath(b parsectx.BuildInfo) { + curr := os.Getenv("PATH") + prefix := b.GOROOT.Join("bin", "go").ToIO() + string(filepath.ListSeparator) + if !strings.HasPrefix(curr, prefix) { + _ = os.Setenv("PATH", prefix+curr) } - prev := os.Getenv("PATH") - updated := b.GOROOT.Join("bin").ToIO() + string(filepath.ListSeparator) + prev - os.Setenv("PATH", updated) }