]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: do not turn list ./nonexist into a network lookup
authorRuss Cox <rsc@golang.org>
Fri, 10 Aug 2018 20:28:48 +0000 (16:28 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 17 Aug 2018 19:22:03 +0000 (19:22 +0000)
If you're in a directory corresponding to x/y
and you run go list ./z, we do at some point
want to turn that into x/y/z. But if ./z does
not exist that will make the go command
check the network to see if it can find x/y/z.
That's clearly wrong: ./z means that directory,
nothing else. And it turns a typo into a long delay,
which is even worse.

Fixes #26874.

Change-Id: Iec15fa7b359af11b6a4fc6cb082e593658fb6e41
Reviewed-on: https://go-review.googlesource.com/129061
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_fs_patterns.txt
src/cmd/go/testdata/script/mod_list_dir.txt

index b00f81458f8628a537f81c8df19947886f063fe1..5ca2ed2d10df45724bb4d903081215bab5b0f063 100644 (file)
@@ -116,10 +116,24 @@ func ImportPaths(patterns []string) []*search.Match {
                                        } else if path := pathInModuleCache(dir); path != "" {
                                                pkg = path
                                        } else {
+                                               pkg = ""
                                                if !iterating {
                                                        base.Errorf("go: directory %s outside available modules", base.ShortPath(dir))
                                                }
+                                       }
+                                       info, err := os.Stat(dir)
+                                       if err != nil || !info.IsDir() {
+                                               // If the directory does not exist,
+                                               // don't turn it into an import path
+                                               // that will trigger a lookup.
                                                pkg = ""
+                                               if !iterating {
+                                                       if err != nil {
+                                                               base.Errorf("go: no such directory %v", m.Pattern)
+                                                       } else {
+                                                               base.Errorf("go: %s is not a directory", m.Pattern)
+                                                       }
+                                               }
                                        }
                                        m.Pkgs[i] = pkg
                                }
index b5350c3eedab654ac8d4855dded9bd6fd6805186..d7d3e0321b53d499fa770a018fc2da67e8b49673 100644 (file)
@@ -28,6 +28,18 @@ stdout ^m/vendor$
 stdout ^m/y$
 ! stdout ^m/y/z
 
+# non-existent directory should not prompt lookups
+! go build -mod=readonly example.com/nonexist
+stderr 'import lookup disabled'
+
+! go build -mod=readonly ./nonexist
+! stderr 'import lookup disabled'
+stderr '^go: no such directory ./nonexist'
+
+! go build -mod=readonly ./go.mod
+! stderr 'import lookup disabled'
+stderr '^go: ./go.mod is not a directory'
+
 -- x/go.mod --
 module m
 
index 29cde71fb8db5b716d6572e13ae008e4460a3960..800f2775591a895e2a20aa2fb76381ba1e52e613 100644 (file)
@@ -9,11 +9,14 @@ go list -f '{{.ImportPath}}' $GOROOT/src/math
 stdout ^math$
 go list -f '{{.ImportPath}}' .
 stdout ^x$
+! go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
+stderr '^go: no such directory.*quote@v1.5.2'
+go mod download rsc.io/quote@v1.5.2
 go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
 stdout '^rsc.io/quote$'
 go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/sampler@v1.3.0
 stdout '^rsc.io/sampler$'
-go get rsc.io/sampler@v1.3.1
+go get -d rsc.io/sampler@v1.3.1
 go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/sampler@v1.3.1
 stdout '^rsc.io/sampler$'
 ! go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/sampler@v1.3.0