]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make 'go mod why' require a main module
authorBryan C. Mills <bcmills@google.com>
Mon, 4 Nov 2019 01:53:47 +0000 (20:53 -0500)
committerBryan C. Mills <bcmills@google.com>
Tue, 5 Nov 2019 18:03:54 +0000 (18:03 +0000)
Updates #32027

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

index eb94d68aa83731f89e7e784e7751b85e8e1e717e..eb0534f24b37bafcb43143741dc868e7df55788e 100644 (file)
@@ -373,12 +373,14 @@ func runGet(cmd *base.Command, args []string) {
 
                default:
                        // The argument is a package or module path.
-                       if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
-                               // The path is in the main module. Nothing to query.
-                               if vers != "upgrade" && vers != "patch" {
-                                       base.Errorf("go get %s: can't request explicit version of path in main module", arg)
+                       if modload.HasModRoot() {
+                               if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
+                                       // The path is in the main module. Nothing to query.
+                                       if vers != "upgrade" && vers != "patch" {
+                                               base.Errorf("go get %s: can't request explicit version of path in main module", arg)
+                                       }
+                                       continue
                                }
-                               continue
                        }
 
                        first := path
index acb968cdfac1607c7a4acf4fffb7ab509f5b1c7d..2172f81797856602159c66eb2a9b95b734efc2cd 100644 (file)
@@ -406,6 +406,10 @@ func loadAll(testAll bool) []string {
 // matching pattern, which may be relative to the working directory, under all
 // build tag settings.
 func TargetPackages(pattern string) []string {
+       // TargetPackages is relative to the main module, so ensure that the main
+       // module is a thing that can contain packages.
+       ModRoot()
+
        return matchPackages(pattern, imports.AnyTags(), false, []module.Version{Target})
 }
 
index 6f569ca351ab4277e77c3a776b2601f8af9bba6f..34570c76c2cd8767f411c35a1fc018a6a728b1a4 100644 (file)
@@ -67,10 +67,9 @@ go mod graph
 ! stdout .
 ! stderr .
 
-# 'go mod why' should report that nothing is a dependency.
-go mod why -m example.com/version
-stdout 'does not need'
-
+# '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'
 
 # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
 # there is no go.mod file to edit.