From af758177c06f24c870bb0b07c3f23da4943f12cf Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 27 Feb 2023 22:13:16 +0000 Subject: [PATCH] cmd/go: disable workspace mode in GOPATH mode Workspace mode is specifically for working with modules; it doesn't make sense in GOPATH mode. This also fixes a panic in (*modload.MainModuleSet).GoVersion when go.work is present in GOPATH mode. For #58767. Change-Id: Ic6924352afb486fecc18e009e6b517f078e81094 Reviewed-on: https://go-review.googlesource.com/c/go/+/471600 Auto-Submit: Bryan Mills Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: David Chase --- src/cmd/go/internal/modload/init.go | 7 +++++-- .../go/testdata/script/work_empty_panic_GOPATH.txt | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 src/cmd/go/testdata/script/work_empty_panic_GOPATH.txt diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 61e7335c70..c25b25f15c 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -388,8 +388,8 @@ func Init() { base.Fatalf("go: -modfile cannot be used with commands that ignore the current module") } modRoots = nil - } else if inWorkspaceMode() { - // We're in workspace mode. + } else if workFilePath != "" { + // We're in workspace mode, which implies module mode. } else { if modRoot := findModuleRoot(base.Cwd()); modRoot == "" { if cfg.ModFile != "" { @@ -496,6 +496,9 @@ func inWorkspaceMode() bool { if !initialized { panic("inWorkspaceMode called before modload.Init called") } + if !Enabled() { + return false + } return workFilePath != "" } diff --git a/src/cmd/go/testdata/script/work_empty_panic_GOPATH.txt b/src/cmd/go/testdata/script/work_empty_panic_GOPATH.txt new file mode 100644 index 0000000000..43ebf113b5 --- /dev/null +++ b/src/cmd/go/testdata/script/work_empty_panic_GOPATH.txt @@ -0,0 +1,13 @@ +# Regression test for https://go.dev/issue/58767: +# with an empty go.work file in GOPATH mode, calls to load.defaultGODEBUG for a +# package named "main" panicked in modload.MainModules.GoVersion. + +env GO111MODULE=off +cd example +go list example/m + +-- example/go.work -- +go 1.21 +-- example/m/main.go -- +package main +func main() {} -- 2.48.1