]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: disable workspace mode in GOPATH mode
authorBryan C. Mills <bcmills@google.com>
Mon, 27 Feb 2023 22:13:16 +0000 (22:13 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 28 Feb 2023 18:50:18 +0000 (18:50 +0000)
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 <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/work_empty_panic_GOPATH.txt [new file with mode: 0644]

index 61e7335c706715a9b46c5cc3a9f2fcaca153a340..c25b25f15c42a536e1bef95efc05f3f30603bdcd 100644 (file)
@@ -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 (file)
index 0000000..43ebf11
--- /dev/null
@@ -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() {}