]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go/internal/modload: ignore $GOPATH/go.mod
authorQuan Tong <quantonganh@gmail.com>
Mon, 6 Nov 2023 01:00:31 +0000 (08:00 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 8 Nov 2023 17:56:44 +0000 (17:56 +0000)
The existing implementation returns a fatal error if $GOPATH/go.mod exists.
We couldn't figure out what directory the go tool was treating as $GOPATH
in the error message.

Fixes #46807

Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Reviewed-on: https://go-review.googlesource.com/c/go/+/539596
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/env_issue46807.txt [new file with mode: 0644]

index b9d9d2e55278fbe80015f8f1a55fd635a2d44e41..f4f4a68254b1663a3b3743eac90ecbb3bad16a6f 100644 (file)
@@ -485,7 +485,13 @@ func Init() {
        if len(list) > 0 && list[0] != "" {
                gopath = list[0]
                if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
-                       base.Fatalf("$GOPATH/go.mod exists but should not")
+                       fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
+                       if RootMode == NeedRoot {
+                               base.Fatal(ErrNoModRoot)
+                       }
+                       if !mustUseModules {
+                               return
+                       }
                }
        }
 }
diff --git a/src/cmd/go/testdata/script/env_issue46807.txt b/src/cmd/go/testdata/script/env_issue46807.txt
new file mode 100644 (file)
index 0000000..e37bc63
--- /dev/null
@@ -0,0 +1,12 @@
+! go mod tidy
+stderr '^go: warning: ignoring go.mod in \$GOPATH'
+stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules'''
+
+go env
+stdout 'GOPATH='
+stderr '^go: warning: ignoring go.mod in \$GOPATH'
+
+-- $GOPATH/go.mod --
+module bug
+
+go 1.21
\ No newline at end of file