[go] cmd/go/internal/modload: if $GOPATH/go.mod exists, print its value

118 views
Skip to first unread message

Quan Tong (Gerrit)

unread,
Nov 3, 2023, 6:29:30 AM11/3/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Quan Tong has uploaded this change for review.

View Change

cmd/go/internal/modload: if $GOPATH/go.mod exists, print its value

Fixes #46807

Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
---
M src/cmd/go/internal/modload/init.go
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index b9d9d2e..3ddaeb4 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -485,7 +485,7 @@
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")
+ base.Fatalf("%s/go.mod exists but should not", gopath)
}
}
}

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>

Bryan Mills (Gerrit)

unread,
Nov 3, 2023, 11:46:15 AM11/3/23
to Quan Tong, goph...@pubsubhelper.golang.org, Bryan Mills, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Michael Matloob, Quan Tong.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

    • Patch Set #1, Line 488: base.Fatalf("%s/go.mod exists but should not", gopath)

      I'm not sure how this relates to #46807. The problem there is that `go env` doesn't produce its usual output when this error is triggered.

      (This failure mode was added in CL 108379 for #24106.)

      Perhaps we should just ignore such a file (as we do for `/tmp/go.mod` as of CL 129063) instead of calling `base.Fatalf` for it?

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Quan Tong <quant...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 03 Nov 2023 15:46:11 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Branden Brown (Gerrit)

unread,
Nov 3, 2023, 1:07:31 PM11/3/23
to Quan Tong, goph...@pubsubhelper.golang.org, Bryan Mills, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Bryan Mills, Michael Matloob, Quan Tong.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

    • I'm not sure how this relates to #46807. […]

      FWIW (as the original reporter of #46807), printing the value of GOPATH would have made it much easier to figure out the motivating problem. Somehow the user's GOENV had changed to a non-default value and pointed their GOPATH to a non-default directory that contained a go.mod file. At that point, all go commands broke, including `go env`, so we couldn't figure out what directory the go tool was treating as $GOPATH in that error message; their default GOPATH was clean.

      That said, I do agree this isn't quite the right change. If the same situation were to happen again, this message would guide the user to remove a go.mod that probably should exist. We want them to check their environment instead, particularly GOPATH and GOENV; it's just currently hard or impossible to do so. Making `go env` in particular ignore $GOPATH/go.mod feels like the ideal solution.

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Attention: Quan Tong <quant...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 03 Nov 2023 17:07:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan Mills <bcm...@google.com>

Quan Tong (Gerrit)

unread,
Nov 5, 2023, 8:02:04 PM11/5/23
to goph...@pubsubhelper.golang.org, Branden Brown, Bryan Mills, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Bryan Mills, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Mon, 06 Nov 2023 01:01:58 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Branden Brown <zephyr...@gmail.com>
Comment-In-Reply-To: Bryan Mills <bcm...@google.com>

Quan Tong (Gerrit)

unread,
Nov 5, 2023, 8:05:46 PM11/5/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Bryan Mills, Michael Matloob.

Quan Tong uploaded patch set #2 to this change.

View Change

cmd/go/internal/modload: ignore $GOPATH/go.mod

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
---
M src/cmd/go/internal/modload/init.go
1 file changed, 7 insertions(+), 1 deletion(-)

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 2

Bryan Mills (Gerrit)

unread,
Nov 6, 2023, 5:22:21 PM11/6/23
to Quan Tong, goph...@pubsubhelper.golang.org, Branden Brown, Bryan Mills, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Michael Matloob, Quan Tong.

View Change

1 comment:

  • Commit Message:

    • Patch Set #2, Line 13: Fixes #46807

      Thanks! But before we can merge this fix, it needs a regression test — please add one in `src/cmd/go/testdata/script`.

      (See the `README` file there for a description of the script environment, and note that the working directory at the start of each test script is a new `$GOPATH/src` directory.)

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 2
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Quan Tong <quant...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Mon, 06 Nov 2023 22:22:17 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Quan Tong (Gerrit)

unread,
Nov 6, 2023, 6:27:22 PM11/6/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Michael Matloob, Quan Tong.

Quan Tong uploaded patch set #3 to this change.

View Change

cmd/go/internal/modload: ignore $GOPATH/go.mod

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
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/env_issue46807.txt
2 files changed, 19 insertions(+), 1 deletion(-)

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 3

Quan Tong (Gerrit)

unread,
Nov 6, 2023, 6:28:15 PM11/6/23
to goph...@pubsubhelper.golang.org, Branden Brown, Bryan Mills, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Bryan Mills, Michael Matloob.

View Change

1 comment:

  • Commit Message:

    • Thanks! But before we can merge this fix, it needs a regression test — please add one in `src/cmd/go […]

      Done

To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
Gerrit-Change-Number: 539596
Gerrit-PatchSet: 3
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Mon, 06 Nov 2023 23:28:09 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan Mills <bcm...@google.com>

Bryan Mills (Gerrit)

unread,
Nov 8, 2023, 10:48:56 AM11/8/23
to Quan Tong, goph...@pubsubhelper.golang.org, Bryan Mills, Branden Brown, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Branden Brown, Michael Matloob, Quan Tong.

Patch set 3:Code-Review +2Commit-Queue +1

View Change

    To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
    Gerrit-Change-Number: 539596
    Gerrit-PatchSet: 3
    Gerrit-Owner: Quan Tong <quant...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-CC: Branden Brown <zephyr...@gmail.com>
    Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
    Gerrit-Attention: Quan Tong <quant...@gmail.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Wed, 08 Nov 2023 15:48:52 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes

    Bryan Mills (Gerrit)

    unread,
    Nov 8, 2023, 11:22:18 AM11/8/23
    to Quan Tong, goph...@pubsubhelper.golang.org, Bryan Mills, Go LUCI, Branden Brown, Michael Matloob, golang-co...@googlegroups.com

    Attention is currently required from: Branden Brown, Michael Matloob, Quan Tong.

    Patch set 3:Auto-Submit +1

    View Change

      To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
      Gerrit-Change-Number: 539596
      Gerrit-PatchSet: 3
      Gerrit-Owner: Quan Tong <quant...@gmail.com>
      Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-CC: Branden Brown <zephyr...@gmail.com>
      Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
      Gerrit-Attention: Quan Tong <quant...@gmail.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Comment-Date: Wed, 08 Nov 2023 16:22:14 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes

      Heschi Kreinick (Gerrit)

      unread,
      Nov 8, 2023, 12:56:39 PM11/8/23
      to Quan Tong, goph...@pubsubhelper.golang.org, Bryan Mills, Go LUCI, Branden Brown, Michael Matloob, golang-co...@googlegroups.com

      Attention is currently required from: Branden Brown, Michael Matloob, Quan Tong.

      Patch set 3:Code-Review +1

      View Change

        To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
        Gerrit-Change-Number: 539596
        Gerrit-PatchSet: 3
        Gerrit-Owner: Quan Tong <quant...@gmail.com>
        Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
        Gerrit-Reviewer: Heschi Kreinick <hes...@google.com>
        Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
        Gerrit-CC: Branden Brown <zephyr...@gmail.com>
        Gerrit-Attention: Branden Brown <zephyr...@gmail.com>
        Gerrit-Attention: Quan Tong <quant...@gmail.com>
        Gerrit-Attention: Michael Matloob <mat...@golang.org>
        Gerrit-Comment-Date: Wed, 08 Nov 2023 17:56:35 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes

        Gopher Robot (Gerrit)

        unread,
        Nov 8, 2023, 12:56:51 PM11/8/23
        to Quan Tong, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Heschi Kreinick, Bryan Mills, Go LUCI, Branden Brown, Michael Matloob, golang-co...@googlegroups.com

        Gopher Robot submitted this change.

        View Change

        Approvals: Heschi Kreinick: Looks good to me, but someone else must approve Go LUCI: TryBots succeeded Bryan Mills: Looks good to me, approved; Automatically submit change
        cmd/go/internal/modload: ignore $GOPATH/go.mod

        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...@luci-project-accounts.iam.gserviceaccount.com>
        Auto-Submit: Bryan Mills <bcm...@google.com>
        Reviewed-by: Bryan Mills <bcm...@google.com>
        Reviewed-by: Heschi Kreinick <hes...@google.com>

        ---
        M src/cmd/go/internal/modload/init.go
        A src/cmd/go/testdata/script/env_issue46807.txt
        2 files changed, 19 insertions(+), 1 deletion(-)

        
        
        diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
        index b9d9d2e..f4f4a68 100644
        --- a/src/cmd/go/internal/modload/init.go
        +++ b/src/cmd/go/internal/modload/init.go
        @@ -485,7 +485,13 @@

        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
        index 0000000..e37bc63
        --- /dev/null
        +++ b/src/cmd/go/testdata/script/env_issue46807.txt
        @@ -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

        To view, visit change 539596. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: merged
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: If9db4c0377f7c36af9c367398d3da494be04cd41
        Gerrit-Change-Number: 539596
        Gerrit-PatchSet: 4
        Gerrit-Owner: Quan Tong <quant...@gmail.com>
        Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Reply all
        Reply to author
        Forward
        0 new messages