]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: forbid module pattern 'all' when outside a module
authorJay Conrod <jayconrod@google.com>
Thu, 10 Oct 2019 14:26:21 +0000 (10:26 -0400)
committerJay Conrod <jayconrod@google.com>
Thu, 10 Oct 2019 19:07:50 +0000 (19:07 +0000)
Also, in cmd/doc, avoid calling 'go list -m all' when in module mode
outside a module since it's now an error.

Fixes #32027

Change-Id: I7224c7fdf7e950bce6c058ab2a5837c27ba3b899
Reviewed-on: https://go-review.googlesource.com/c/go/+/200297
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/doc/dirs.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/list.go
src/cmd/go/testdata/script/mod_outside.txt

index c6f5cd9af8516289f336e25dec18128351398869..0f990f70175e3ba9fd235cf89529ec5cd5e2887c 100644 (file)
@@ -162,7 +162,15 @@ func findCodeRoots() []Dir {
                // Check for use of modules by 'go env GOMOD',
                // which reports a go.mod file path if modules are enabled.
                stdout, _ := exec.Command("go", "env", "GOMOD").Output()
-               usingModules = len(bytes.TrimSpace(stdout)) > 0
+               gomod := string(bytes.TrimSpace(stdout))
+               usingModules = len(gomod) > 0
+               if gomod == os.DevNull {
+                       // Modules are enabled, but the working directory is outside any module.
+                       // We can still access std, cmd, and packages specified as source files
+                       // on the command line, but there are no module roots.
+                       // Avoid 'go list -m all' below, since it will not work.
+                       return list
+               }
        }
 
        if !usingModules {
index 6e67eac9832ab531cb4e802390093884c85e7f14..6bb8cdf55ce1292ca8504534d374b1af25712896 100644 (file)
@@ -358,6 +358,10 @@ func runGet(cmd *base.Command, args []string) {
                        // upgrade golang.org/x/tools.
 
                case path == "all":
+                       // If there is no main module, "all" is not meaningful.
+                       if !modload.HasModRoot() {
+                               base.Errorf(`go get %s: cannot match "all": working directory is not part of a module`, arg)
+                       }
                        // Don't query modules until we load packages. We'll automatically
                        // look up any missing modules.
 
index 35d0c28cde10b943f880d449531783c58cf0f27f..6c0b3945cb8d50a38882713b4bf26e2e8eb8965e 100644 (file)
@@ -55,6 +55,9 @@ func listModules(args []string, listVersions bool) []*modinfo.ModulePublic {
                if search.IsRelativePath(arg) {
                        base.Fatalf("go: cannot use relative path %s to specify module", arg)
                }
+               if !HasModRoot() && arg == "all" {
+                       base.Fatalf(`go: cannot match "all": working directory is not part of a module`)
+               }
                if i := strings.Index(arg, "@"); i >= 0 {
                        path := arg[:i]
                        vers := arg[i+1:]
index dd4e2d580067433be5ae9303614d5118a720c7b9..815745e8bff03be6167a8e37c06c279e9a408462 100644 (file)
@@ -25,8 +25,6 @@ stderr 'cannot find main module'
 go list all
 ! stdout .
 stderr 'warning: "all" matched no packages'
-go list -m all
-stderr 'warning: pattern "all" matched no module dependencies'
 
 # 'go list' on standard-library packages should work, since they do not depend
 # on the contents of any module.
@@ -49,10 +47,13 @@ stdout 'example.com/version v1.1.0'
 go list -m -versions example.com/version
 stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'
 
-# 'go list -m <mods> all' does not include the dependencies of <mods> in the computation of 'all'.
-go list -m example.com/printversion@v1.0.0 all
-stdout 'example.com/printversion v1.0.0'
-stderr 'warning: pattern "all" matched no module dependencies'
+# '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'
+
+# 'go list -m <mods> all' should also fail.
+! go list -m example.com/printversion@v1.0.0 all
+stderr 'go: cannot match "all": working directory is not part of a module'
 ! stdout 'example.com/version'
 
 
@@ -87,6 +88,10 @@ go mod download example.com/printversion@v1.0.0
 exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
 ! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip
 
+# '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'
+
 # 'go mod vendor' should fail: it starts by clearing the existing vendor
 # directory, and we don't know where that is.
 ! go mod vendor
@@ -109,9 +114,8 @@ stderr 'cannot find main module'
 
 # 'go get -u all' upgrades the transitive import graph of the main module,
 # which is empty.
-go get -u all
-! stdout .
-stderr 'warning: "all" matched no packages'
+! go get -u all
+stderr 'go get all: cannot match "all": working directory is not part of a module'
 
 # 'go get' should check the proposed module graph for consistency,
 # even though we won't write it anywhere.