[go] cmd/go: clarify errors for commands run outside a module

1,222 views
Skip to first unread message

Jay Conrod (Gerrit)

unread,
Mar 4, 2021, 12:55:55 PM3/4/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

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

Patch set 4:Run-TryBot +1Trust +1

View Change

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 4
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 17:55:48 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Bryan C. Mills (Gerrit)

    unread,
    Mar 4, 2021, 1:09:13 PM3/4/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod, Michael Matloob.

    Patch set 4:Code-Review +2

    View Change

    1 comment:

    • File src/cmd/go/internal/modget/query.go:

      • Patch Set #4, Line 189: modload.NoModRootErrorMessage

        Would it make sense to make this a value of type `error` instead, like `modload.ErrNoModRoot`?

        Then we could (potentially) also use `errors.Is(err, modload.ErrNoModRoot)` if we need that at some point down the road.

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 4
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 18:09:08 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 1:47:16 PM3/4/21
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod, Michael Matloob.

    Jay Conrod uploaded patch set #5 to this change.

    View Change

    cmd/go: clarify errors for commands run outside a module

    The new error message tells the user what was wrong (no go.mod found)
    and directs them to 'go help modules', which links to tutorials.

    Fixes #44745

    Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    ---
    M src/cmd/go/internal/modget/query.go
    M src/cmd/go/internal/modload/import.go
    M src/cmd/go/internal/modload/init.go
    M src/cmd/go/internal/modload/list.go
    M src/cmd/go/internal/run/run.go
    M src/cmd/go/testdata/script/mod_convert_dep.txt
    M src/cmd/go/testdata/script/mod_find.txt
    M src/cmd/go/testdata/script/mod_outside.txt
    M src/go/build/build_test.go
    9 files changed, 47 insertions(+), 59 deletions(-)

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 5
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-MessageType: newpatchset

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 1:48:25 PM3/4/21
    to goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Michael Matloob, golang-co...@googlegroups.com

    Attention is currently required from: Michael Matloob.

    Patch set 5:Trust +1

    View Change

    1 comment:

    • File src/cmd/go/internal/modget/query.go:

      • Would it make sense to make this a value of type `error` instead, like `modload.ErrNoModRoot`? […]

        Done, good idea. I had to pull some logic in importFromBuildList, but it's not too bad.

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 5
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 18:48:19 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
    Gerrit-MessageType: comment

    Bryan C. Mills (Gerrit)

    unread,
    Mar 4, 2021, 1:51:22 PM3/4/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Michael Matloob, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod, Michael Matloob.

    Patch set 5:Code-Review +2

    View Change

    1 comment:

    • File src/cmd/go/internal/modget/query.go:

      • Done, good idea. I had to pull some logic in importFromBuildList, but it's not too bad.

        Nice!

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 5
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 18:51:17 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
    Comment-In-Reply-To: Jay Conrod <jayc...@google.com>
    Gerrit-MessageType: comment

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 5:12:16 PM3/4/21
    to goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Michael Matloob, golang-co...@googlegroups.com

    Attention is currently required from: Michael Matloob.

    Patch set 6:Run-TryBot +1Trust +1

    View Change

    1 comment:

    • Patchset:

      • Patch Set #6:

        Fixed x/tools TryBot failures upstream in CL 298792.

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 6
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 22:12:10 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 5:41:40 PM3/4/21
    to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, Bryan C. Mills, Michael Matloob, golang-co...@googlegroups.com

    Jay Conrod submitted this change.

    View Change

    Approvals: Bryan C. Mills: Looks good to me, approved Jay Conrod: Trusted; Run TryBots Go Bot: TryBots succeeded
    cmd/go: clarify errors for commands run outside a module

    The new error message tells the user what was wrong (no go.mod found)
    and directs them to 'go help modules', which links to tutorials.

    Fixes #44745

    Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Reviewed-on: https://go-review.googlesource.com/c/go/+/298650
    Trust: Jay Conrod <jayc...@google.com>
    Run-TryBot: Jay Conrod <jayc...@google.com>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Bryan C. Mills <bcm...@google.com>

    ---
    M src/cmd/go/internal/modget/query.go
    M src/cmd/go/internal/modload/import.go
    M src/cmd/go/internal/modload/init.go
    M src/cmd/go/internal/modload/list.go
    M src/cmd/go/internal/run/run.go
    M src/cmd/go/testdata/script/mod_convert_dep.txt
    M src/cmd/go/testdata/script/mod_find.txt
    M src/cmd/go/testdata/script/mod_outside.txt
    M src/go/build/build_test.go
    9 files changed, 47 insertions(+), 59 deletions(-)

    diff --git a/src/cmd/go/internal/modget/query.go b/src/cmd/go/internal/modget/query.go
    index d8364c8..1a5a60f 100644
    --- a/src/cmd/go/internal/modget/query.go
    +++ b/src/cmd/go/internal/modget/query.go
    @@ -186,7 +186,7 @@
    if q.pattern == "all" {
    // If there is no main module, "all" is not meaningful.
    if !modload.HasModRoot() {
    - return fmt.Errorf(`cannot match "all": working directory is not part of a module`)
    + return fmt.Errorf(`cannot match "all": %v`, modload.ErrNoModRoot)
    }
    if !versionOkForMainModule(q.version) {
    // TODO(bcmills): "all@none" seems like a totally reasonable way to
    diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go
    index 182429a..995641c 100644
    --- a/src/cmd/go/internal/modload/import.go
    +++ b/src/cmd/go/internal/modload/import.go
    @@ -51,7 +51,7 @@
    if e.isStd {
    return fmt.Sprintf("package %s is not in GOROOT (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path))
    }
    - if e.QueryErr != nil {
    + if e.QueryErr != nil && e.QueryErr != ErrNoModRoot {
    return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
    }
    if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
    @@ -66,13 +66,11 @@
    return fmt.Sprintf("module %s provides package %s and is replaced but not required; to add it:\n\tgo get %s", e.replaced.Path, e.Path, suggestArg)
    }

    - suggestion := ""
    - if !HasModRoot() {
    - suggestion = ": working directory is not part of a module"
    - } else {
    - suggestion = fmt.Sprintf("; to add it:\n\tgo get %s", e.Path)
    + message := fmt.Sprintf("no required module provides package %s", e.Path)
    + if e.QueryErr != nil {
    + return fmt.Sprintf("%s: %v", message, e.QueryErr)
    }
    - return fmt.Sprintf("no required module provides package %s%s", e.Path, suggestion)
    + return fmt.Sprintf("%s; to add it:\n\tgo get %s", message, e.Path)
    }

    if e.newMissingVersion != "" {
    @@ -318,7 +316,11 @@
    return mods[0], dirs[0], nil
    }

    - return module.Version{}, "", &ImportMissingError{Path: path, isStd: pathIsStd}
    + var queryErr error
    + if !HasModRoot() {
    + queryErr = ErrNoModRoot
    + }
    + return module.Version{}, "", &ImportMissingError{Path: path, QueryErr: queryErr, isStd: pathIsStd}
    }

    // queryImport attempts to locate a module that can be added to the current
    diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
    index 4de5ac9..8ec1c86 100644
    --- a/src/cmd/go/internal/modload/init.go
    +++ b/src/cmd/go/internal/modload/init.go
    @@ -177,7 +177,7 @@
    base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
    }
    if RootMode == NeedRoot {
    - base.Fatalf("go: cannot find main module; see 'go help modules'")
    + base.Fatalf("go: %v", ErrNoModRoot)
    }
    if !mustUseModules {
    // GO111MODULE is 'auto', and we can't find a module root.
    @@ -338,9 +338,11 @@
    }
    base.Fatalf("go: cannot find main module, but found %s in %s\n\tto create a module there, run:\n\t%sgo mod init", name, dir, cdCmd)
    }
    - base.Fatalf("go: cannot find main module; see 'go help modules'")
    + base.Fatalf("go: %v", ErrNoModRoot)
    }

    +var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
    +
    // LoadModFile sets Target and, if there is a main module, parses the initial
    // build list from its go.mod file.
    //
    diff --git a/src/cmd/go/internal/modload/list.go b/src/cmd/go/internal/modload/list.go
    index 6dba6be..c7ef8c9 100644
    --- a/src/cmd/go/internal/modload/list.go
    +++ b/src/cmd/go/internal/modload/list.go
    @@ -73,7 +73,7 @@
    base.Fatalf("go: cannot use relative path %s to specify module", arg)
    }
    if !HasModRoot() && (arg == "all" || strings.Contains(arg, "...")) {
    - base.Fatalf("go: cannot match %q: working directory is not part of a module", arg)
    + base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
    }
    if i := strings.Index(arg, "@"); i >= 0 {
    path := arg[:i]
    diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go
    index 99578b2..666b1a0 100644
    --- a/src/cmd/go/internal/run/run.go
    +++ b/src/cmd/go/internal/run/run.go
    @@ -96,28 +96,12 @@
    base.Fatalf("go run: no go files listed")
    }
    cmdArgs := args[i:]
    - if p.Error != nil {
    - base.Fatalf("%s", p.Error)
    - }
    + load.CheckPackageErrors([]*load.Package{p})

    - p.Internal.OmitDebug = true
    - if len(p.DepsErrors) > 0 {
    - // Since these are errors in dependencies,
    - // the same error might show up multiple times,
    - // once in each package that depends on it.
    - // Only print each once.
    - printed := map[*load.PackageError]bool{}
    - for _, err := range p.DepsErrors {
    - if !printed[err] {
    - printed[err] = true
    - base.Errorf("%s", err)
    - }
    - }
    - }
    - base.ExitIfErrors()
    if p.Name != "main" {
    base.Fatalf("go run: cannot run non-main package")
    }
    + p.Internal.OmitDebug = true
    p.Target = "" // must build - not up to date
    if p.Internal.CmdlineFiles {
    //set executable name if go file is given as cmd-argument
    diff --git a/src/cmd/go/testdata/script/mod_convert_dep.txt b/src/cmd/go/testdata/script/mod_convert_dep.txt
    index ad22aca..875a836 100644
    --- a/src/cmd/go/testdata/script/mod_convert_dep.txt
    +++ b/src/cmd/go/testdata/script/mod_convert_dep.txt
    @@ -18,7 +18,7 @@
    # Test that we ignore directories when trying to find alternate config files.
    cd $WORK/gopkgdir/x
    ! go list .
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! stderr 'Gopkg.lock'

    -- $WORK/test/Gopkg.lock --
    diff --git a/src/cmd/go/testdata/script/mod_find.txt b/src/cmd/go/testdata/script/mod_find.txt
    index 9468acf..1e01973 100644
    --- a/src/cmd/go/testdata/script/mod_find.txt
    +++ b/src/cmd/go/testdata/script/mod_find.txt
    @@ -49,7 +49,7 @@
    # Test that we ignore directories when trying to find go.mod.
    cd $WORK/gomoddir
    ! go list .
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    [!symlink] stop

    diff --git a/src/cmd/go/testdata/script/mod_outside.txt b/src/cmd/go/testdata/script/mod_outside.txt
    index 7b45f1a..9d4c22c 100644
    --- a/src/cmd/go/testdata/script/mod_outside.txt
    +++ b/src/cmd/go/testdata/script/mod_outside.txt
    @@ -12,13 +12,13 @@
    # 'go list' without arguments implicitly operates on the current directory,
    # which is not in a module.
    ! go list
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    go list -m
    stdout '^command-line-arguments$'
    # 'go list' in the working directory should fail even if there is a a 'package
    # main' present: without a main module, we do not know its package path.
    ! go list ./needmod
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go list all' lists the transitive import graph of the main module,
    # which is empty if there is no main module.
    @@ -41,7 +41,7 @@

    # 'go list' on a package from a module should fail.
    ! go list example.com/printversion
    -stderr '^no required module provides package example.com/printversion: working directory is not part of a module$'
    +stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go list -m' with an explicit version should resolve that version.
    @@ -54,19 +54,19 @@

    # 'go list -m all' should fail. "all" is not meaningful outside of a module.
    ! go list -m all
    -stderr 'go: cannot match "all": working directory is not part of a module'
    +stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go list -m <mods> all' should also fail.
    ! go list -m example.com/printv...@v1.0.0 all
    -stderr 'go: cannot match "all": working directory is not part of a module'
    +stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! stdout 'example.com/version'

    # 'go list -m' with wildcards should fail. Wildcards match modules in the
    # build list, so they aren't meaningful outside a module.
    ! go list -m ...
    -stderr 'go: cannot match "...": working directory is not part of a module'
    +stderr 'go: cannot match "...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! go list -m rsc.io/quote/...
    -stderr 'go: cannot match "rsc.io/quote/...": working directory is not part of a module'
    +stderr 'go: cannot match "rsc.io/quote/...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go clean' should skip the current directory if it isn't in a module.
    @@ -76,20 +76,20 @@

    # 'go mod graph' should fail, since there's no module graph.
    ! go mod graph
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go mod why' should fail, since there is no main module to depend on anything.
    ! go mod why -m example.com/version
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
    # there is no go.mod file to edit.
    ! go mod tidy
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! go mod edit -fmt
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! go mod edit -require example.com/ver...@v1.0.0
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go mod download' without arguments should report an error.
    @@ -104,33 +104,33 @@

    # 'go mod download all' should fail. "all" is not meaningful outside of a module.
    ! go mod download all
    -stderr 'go: cannot match "all": working directory is not part of a module'
    +stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go mod vendor' should fail: it starts by clearing the existing vendor
    # directory, and we don't know where that is.
    ! go mod vendor
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go mod verify' should fail: we have no modules to verify.
    ! go mod verify
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go get' without arguments implicitly operates on the main module, and thus
    # should fail.
    ! go get
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! go get -u
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    ! go get -u ./needmod
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go get -u all' upgrades the transitive import graph of the main module,
    # which is empty.
    ! go get -u all
    -stderr 'go get: cannot match "all": working directory is not part of a module'
    +stderr '^go get: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go get' should check the proposed module graph for consistency,
    # even though we won't write it anywhere.
    @@ -147,16 +147,16 @@
    # 'go build' without arguments implicitly operates on the current directory, and should fail.
    cd needmod
    ! go build
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    cd ..

    # 'go build' of a non-module directory should fail too.
    ! go build ./needmod
    -stderr 'cannot find main module'
    +stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go build' of source files should fail if they import anything outside std.
    ! go build -n ./needmod/needmod.go
    -stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: working directory is not part of a module$'
    +stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go build' of source files should succeed if they do not import anything outside std.
    go build -n -o ignore ./stdonly/stdonly.go
    @@ -179,7 +179,7 @@

    # 'go doc' should fail for a package path outside a module.
    ! go doc example.com/version
    -stderr 'doc: no required module provides package example.com/version: working directory is not part of a module'
    +stderr 'doc: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go install' with a version should succeed if all constraints are met.
    # See mod_install_pkg_version.
    @@ -194,7 +194,7 @@
    # 'go install' should fail if a source file imports a package that must be
    # resolved to a module.
    ! go install ./needmod/needmod.go
    -stderr 'needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: working directory is not part of a module'
    +stderr 'needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go install' should succeed with a package in GOROOT.
    go install cmd/addr2line
    @@ -206,12 +206,12 @@

    # 'go run' should fail if a package argument must be resolved to a module.
    ! go run example.com/printversion
    -stderr '^no required module provides package example.com/printversion: working directory is not part of a module$'
    +stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'

    # 'go run' should fail if a source file imports a package that must be
    # resolved to a module.
    ! go run ./needmod/needmod.go
    -stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: working directory is not part of a module$'
    +stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'


    # 'go fmt' should be able to format files outside of a module.
    diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go
    index 0762a15..6529b6e 100644
    --- a/src/go/build/build_test.go
    +++ b/src/go/build/build_test.go
    @@ -644,7 +644,7 @@
    ctxt.GOPATH = gopath
    ctxt.Dir = filepath.Join(gopath, "src/example.com/p")

    - want := "working directory is not part of a module"
    + want := "go.mod file not found in current directory or any parent directory"
    if _, err := ctxt.Import("example.com/p", gopath, FindOnly); err == nil {
    t.Fatal("importing package when no go.mod is present succeeded unexpectedly")
    } else if errStr := err.Error(); !strings.Contains(errStr, want) {

    5 is the latest approved patch-set. No files were changed between the latest approved patch-set and the submitted one.

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298650
    Gerrit-PatchSet: 7
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-MessageType: merged

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 5:51:00 PM3/4/21
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Jay Conrod has uploaded this change for review.

    View Change

    [release-branch.go1.16] cmd/go: clarify errors for commands run outside a module


    The new error message tells the user what was wrong (no go.mod found)
    and directs them to 'go help modules', which links to tutorials.

    Fixes #44746


    Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Reviewed-on: https://go-review.googlesource.com/c/go/+/298650
    Trust: Jay Conrod <jayc...@google.com>
    Run-TryBot: Jay Conrod <jayc...@google.com>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Bryan C. Mills <bcm...@google.com>
    (cherry picked from commit b87e9b9f68f1eb0d685fd250b3b47495710e0059)
    index 3491f94..7b1aa7f 100644
    index 8f01b5d..5655892 100644
    index d8f264c..d13ea81 100644
    --- a/src/go/build/build_test.go
    +++ b/src/go/build/build_test.go
    @@ -612,7 +612,7 @@

    ctxt.GOPATH = gopath
    ctxt.Dir = filepath.Join(gopath, "src/example.com/p")

    - want := "working directory is not part of a module"
    + want := "go.mod file not found in current directory or any parent directory"
    if _, err := ctxt.Import("example.com/p", gopath, FindOnly); err == nil {
    t.Fatal("importing package when no go.mod is present succeeded unexpectedly")
    } else if errStr := err.Error(); !strings.Contains(errStr, want) {

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-MessageType: newchange

    Jay Conrod (Gerrit)

    unread,
    Mar 4, 2021, 5:51:34 PM3/4/21
    to goph...@pubsubhelper.golang.org, Bryan C. Mills, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Bryan C. Mills.

    Patch set 1:Run-TryBot +1Trust +1

    View Change

    1 comment:

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
    Gerrit-Comment-Date: Thu, 04 Mar 2021 22:51:30 +0000

    Bryan C. Mills (Gerrit)

    unread,
    Mar 4, 2021, 9:01:26 PM3/4/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod.

    View Change

    1 comment:

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Comment-Date: Fri, 05 Mar 2021 02:01:21 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Bryan C. Mills (Gerrit)

    unread,
    Mar 4, 2021, 10:06:35 PM3/4/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #1:

        > Patch Set 1: TryBot-Result-1 […]

        Squashing a backport of CL 298072 on top of this one may fix it.

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Comment-Date: Fri, 05 Mar 2021 03:06:30 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
    Gerrit-MessageType: comment

    Bryan C. Mills (Gerrit)

    unread,
    Mar 4, 2021, 10:07:37 PM3/4/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #1:

        Squashing a backport of CL 298072 on top of this one may fix it.

        Err, copypasta. CL 298794 is the one to squash on this.

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-Comment-Date: Fri, 05 Mar 2021 03:07:33 +0000

    Jay Conrod (Gerrit)

    unread,
    Mar 5, 2021, 9:19:34 AM3/5/21
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod.

    Jay Conrod uploaded patch set #2 to this change.

    View Change

    [release-branch.go1.16] cmd/go: clarify errors for commands run outside a module

    The new error message tells the user what was wrong (no go.mod found)
    and directs them to 'go help modules', which links to tutorials.

    Includes test fix from CL 298794

    Fixes #44746

    Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Reviewed-on: https://go-review.googlesource.com/c/go/+/298650
    Trust: Jay Conrod <jayc...@google.com>
    Run-TryBot: Jay Conrod <jayc...@google.com>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Bryan C. Mills <bcm...@google.com>
    (cherry picked from commit b87e9b9f68f1eb0d685fd250b3b47495710e0059)
    ---
    M src/cmd/go/internal/modget/query.go
    M src/cmd/go/internal/modload/import.go
    M src/cmd/go/internal/modload/init.go
    M src/cmd/go/internal/modload/list.go
    M src/cmd/go/internal/run/run.go
    M src/cmd/go/testdata/script/mod_convert_dep.txt
    M src/cmd/go/testdata/script/mod_find.txt
    M src/cmd/go/testdata/script/mod_install_pkg_version.txt
    M src/cmd/go/testdata/script/mod_outside.txt
    M src/go/build/build_test.go
    10 files changed, 49 insertions(+), 61 deletions(-)

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 2
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Jay Conrod <jayc...@google.com>
    Gerrit-MessageType: newpatchset

    Jay Conrod (Gerrit)

    unread,
    Mar 5, 2021, 9:20:24 AM3/5/21
    to goph...@pubsubhelper.golang.org, Go Bot, Bryan C. Mills, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Bryan C. Mills.

    Patch set 2:Run-TryBot +1Trust +1

    View Change

    2 comments:

      • Err, copypasta. CL 298794 is the one to squash on this.

      • Done. Thanks for fixing that, and sorry about the break.

    • Patchset:

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

    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.16
    Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
    Gerrit-Change-Number: 298929
    Gerrit-PatchSet: 2
    Gerrit-Owner: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
    Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
    Gerrit-Comment-Date: Fri, 05 Mar 2021 14:20:20 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes

    Bryan C. Mills (Gerrit)

    unread,
    Mar 5, 2021, 11:08:19 AM3/5/21
    to Jay Conrod, goph...@pubsubhelper.golang.org, Bryan C. Mills, Go Bot, Dmitri Shuralyov, golang-co...@googlegroups.com

    Attention is currently required from: Jay Conrod.

    Patch set 2:Code-Review +2

    View Change

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

      Gerrit-Project: go
      Gerrit-Branch: release-branch.go1.16
      Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
      Gerrit-Change-Number: 298929
      Gerrit-PatchSet: 2
      Gerrit-Owner: Jay Conrod <jayc...@google.com>
      Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
      Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
      Gerrit-Attention: Jay Conrod <jayc...@google.com>
      Gerrit-Comment-Date: Fri, 05 Mar 2021 16:08:15 +0000

      Alexander Rakoczy (Gerrit)

      unread,
      Mar 10, 2021, 4:27:13 PM3/10/21
      to Jay Conrod, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Bryan C. Mills, Go Bot, Dmitri Shuralyov, golang-co...@googlegroups.com

      Alexander Rakoczy submitted this change.

      View Change

      Approvals: Bryan C. Mills: Looks good to me, approved Jay Conrod: Trusted; Run TryBots Go Bot: TryBots succeeded
      [release-branch.go1.16] cmd/go: clarify errors for commands run outside a module


      The new error message tells the user what was wrong (no go.mod found)
      and directs them to 'go help modules', which links to tutorials.

      Includes test fix from CL 298794
      Fixes #44746

      Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
      Reviewed-on: https://go-review.googlesource.com/c/go/+/298650
      Trust: Jay Conrod <jayc...@google.com>
      Run-TryBot: Jay Conrod <jayc...@google.com>
      TryBot-Result: Go Bot <go...@golang.org>
      Reviewed-by: Bryan C. Mills <bcm...@google.com>
      (cherry picked from commit b87e9b9f68f1eb0d685fd250b3b47495710e0059)
      Reviewed-on: https://go-review.googlesource.com/c/go/+/298929

      ---
      M src/cmd/go/internal/modget/query.go
      M src/cmd/go/internal/modload/import.go
      M src/cmd/go/internal/modload/init.go
      M src/cmd/go/internal/modload/list.go
      M src/cmd/go/internal/run/run.go
      M src/cmd/go/testdata/script/mod_convert_dep.txt
      M src/cmd/go/testdata/script/mod_find.txt
      M src/cmd/go/testdata/script/mod_install_pkg_version.txt
      M src/cmd/go/testdata/script/mod_outside.txt
      M src/go/build/build_test.go
      10 files changed, 49 insertions(+), 61 deletions(-)

      diff --git a/src/cmd/go/testdata/script/mod_install_pkg_version.txt b/src/cmd/go/testdata/script/mod_install_pkg_version.txt
      index e27ebc5..6ed600f 100644
      --- a/src/cmd/go/testdata/script/mod_install_pkg_version.txt
      +++ b/src/cmd/go/testdata/script/mod_install_pkg_version.txt
      @@ -59,9 +59,9 @@
      env GO111MODULE=on
      go mod download rsc.io/for...@v1.0.0
      ! go install $GOPATH/pkg/mod/rsc.io/for...@v1.0.0
      -stderr '^go: cannot find main module; see ''go help modules''$'
      +stderr '^go: go\.mod file not found in current directory or any parent directory; see ''go help modules''$'
      ! go install ../pkg/mod/rsc.io/for...@v1.0.0
      -stderr '^go: cannot find main module; see ''go help modules''$'
      +stderr '^go: go\.mod file not found in current directory or any parent directory; see ''go help modules''$'
      mkdir tmp
      cd tmp
      go mod init tmp

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

      Gerrit-Project: go
      Gerrit-Branch: release-branch.go1.16
      Gerrit-Change-Id: I98f31fec4a8757eb1792b45491519da4c552cb0f
      Gerrit-Change-Number: 298929
      Gerrit-PatchSet: 3
      Gerrit-Owner: Jay Conrod <jayc...@google.com>
      Gerrit-Reviewer: Alexander Rakoczy <al...@golang.org>
      Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
      Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
      Gerrit-MessageType: merged
      Reply all
      Reply to author
      Forward
      0 new messages