From 7c1806932fb91c7f8f8d17be7c1a63aa3d6cf175 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 25 Mar 2026 14:05:36 -0400 Subject: cmd/go: default to Go 1.20 GODEBUGs in GOPATH mode We used MainModules.GoVersion to get the Go version to use for godebugs in GOPATH mode. That returned Go 1.16 which is the default version for modules to use when they don't have a go directive, but is modules specific and doesn't make sense for GOPATH mode. Set the version to 1.20. For #73973 Change-Id: Iaa5bb77498d5860f2372ffda8a6b88a26a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/759240 LUCI-TryBot-Result: Go LUCI Reviewed-by: t hepudds Reviewed-by: Michael Matloob Reviewed-by: Dmitri Shuralyov Auto-Submit: Michael Matloob --- src/cmd/go/alldocs.go | 10 +++---- src/cmd/go/internal/help/helpdoc.go | 10 +++---- src/cmd/go/internal/load/godebug.go | 31 ++++++++++++++-------- .../go/testdata/script/godebug_default_gopath.txt | 9 +++++++ 4 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/cmd/go/testdata/script/godebug_default_gopath.txt (limited to 'src/cmd') diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 52aa6c7fb4..4be78b33bb 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -2878,11 +2878,11 @@ // GOPATH mode import path checking (see 'go help importpath'). // // In GOPATH mode, the default GODEBUG values built into a binary -// will be those used in Go 1.20, setting the same GODEBUG values -// as when a module specifies "godebug default=go1.20". To use -// different GODEBUG settings, the GODEBUG environment variable must -// be set to override those values. This also means that the standard -// library tests will not run properly with GO111MODULE=off. +// will be the same GODEBUG values as when a module specifies +// "godebug default=go1.20". To use different GODEBUG settings, the +// GODEBUG environment variable must be set to override those values. +// This also means that the standard library tests will not run +// properly with GO111MODULE=off. // // See https://go.dev/s/go15vendor for details. // diff --git a/src/cmd/go/internal/help/helpdoc.go b/src/cmd/go/internal/help/helpdoc.go index 5f77b88536..4b1444ee3b 100644 --- a/src/cmd/go/internal/help/helpdoc.go +++ b/src/cmd/go/internal/help/helpdoc.go @@ -525,11 +525,11 @@ Code in GOPATH mode vendor directories is not subject to GOPATH mode import path checking (see 'go help importpath'). In GOPATH mode, the default GODEBUG values built into a binary -will be those used in Go 1.20, setting the same GODEBUG values -as when a module specifies "godebug default=go1.20". To use -different GODEBUG settings, the GODEBUG environment variable must -be set to override those values. This also means that the standard -library tests will not run properly with GO111MODULE=off. +will be the same GODEBUG values as when a module specifies +"godebug default=go1.20". To use different GODEBUG settings, the +GODEBUG environment variable must be set to override those values. +This also means that the standard library tests will not run +properly with GO111MODULE=off. See https://go.dev/s/go15vendor for details. diff --git a/src/cmd/go/internal/load/godebug.go b/src/cmd/go/internal/load/godebug.go index 7bb873fa7d..a6bcdad627 100644 --- a/src/cmd/go/internal/load/godebug.go +++ b/src/cmd/go/internal/load/godebug.go @@ -42,6 +42,24 @@ func ParseGoDebug(text string) (key, value string, err error) { return k, v, nil } +func defaultGODEBUGGoVersion(loaderstate *modload.State, p *Package) string { + if !loaderstate.Enabled() { + // GOPATH mode. Use Go 1.20. + return "1.20" + } + if loaderstate.RootMode == modload.NoRoot && p.Module != nil { + // This is go install pkg@version or go run pkg@version. + // Use the Go version from the package. + // If there isn't one, then assume Go 1.20, the last + // version before GODEBUGs were introduced (#56986). + if goVersion := p.Module.GoVersion; goVersion != "" { + return goVersion + } + return "1.20" + } + return loaderstate.MainModules.GoVersion(loaderstate) +} + // defaultGODEBUG returns the default GODEBUG setting for the main package p. // When building a test binary, directives, testDirectives, and xtestDirectives // list additional directives from the package under test. @@ -49,17 +67,8 @@ func defaultGODEBUG(loaderstate *modload.State, p *Package, directives, testDire if p.Name != "main" { return "" } - goVersion := loaderstate.MainModules.GoVersion(loaderstate) - if loaderstate.RootMode == modload.NoRoot && p.Module != nil { - // This is go install pkg@version or go run pkg@version. - // Use the Go version from the package. - // If there isn't one, then assume Go 1.20, - // the last version before GODEBUGs were introduced. - goVersion = p.Module.GoVersion - if goVersion == "" { - goVersion = "1.20" - } - } + + goVersion := defaultGODEBUGGoVersion(loaderstate, p) var m map[string]string diff --git a/src/cmd/go/testdata/script/godebug_default_gopath.txt b/src/cmd/go/testdata/script/godebug_default_gopath.txt new file mode 100644 index 0000000000..2bdf8369ae --- /dev/null +++ b/src/cmd/go/testdata/script/godebug_default_gopath.txt @@ -0,0 +1,9 @@ +env GO111MODULE=off + +go list -f '{{.DefaultGODEBUG}}' +! stdout netedns0 + +-- foo.go -- +package main + +func main() {} \ No newline at end of file -- cgit v1.3-5-g45d5