Skip to content

Commit

Permalink
Fix go 1.21 build (encoredev#838)
Browse files Browse the repository at this point in the history
- Upgrade modfile dep
- Fix go.mod building
  • Loading branch information
eandre authored Aug 9, 2023
1 parent 6093367 commit 5ffeac3
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 21 deletions.
7 changes: 4 additions & 3 deletions .reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/testdata/echo/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module encore.app

go 1.18
go 1.21.0

require encore.dev v1.9.3
8 changes: 4 additions & 4 deletions e2e-tests/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module encr.dev

go 1.20
go 1.21.0

require (
cuelang.org/go v0.4.3
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions runtime/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions runtime/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions v2/compiler/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions v2/compiler/build/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions v2/internals/pkginfo/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

0 comments on commit 5ffeac3

Please sign in to comment.