Commit c2412a76 authored by Jay Conrod's avatar Jay Conrod

cmd/go: emit go list error for local non-existant packages

In CL 129061, a check was added for patterns that reference
nonexistent local directories. While this prevented unnecessary
network lookups (fixing #26874), it caused "go list -e" to exit with
an error instead of listing packages with error messages.

This change avoids the network lookup and does not exit for these
kinds of packages. Errors are still reported by
internal/load.LoadImport for packages that don't exist.

Fixes #28023

Change-Id: I0a648269e437aed3a95bfb05461a397264f3793f
Reviewed-on: https://go-review.googlesource.com/c/151800
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 58ffe505
...@@ -90,7 +90,7 @@ func ImportPaths(patterns []string) []*search.Match { ...@@ -90,7 +90,7 @@ func ImportPaths(patterns []string) []*search.Match {
// the exact version of a particular module increases during // the exact version of a particular module increases during
// the loader iterations. // the loader iterations.
m.Pkgs = str.StringList(fsDirs[i]) m.Pkgs = str.StringList(fsDirs[i])
for i, pkg := range m.Pkgs { for j, pkg := range m.Pkgs {
dir := pkg dir := pkg
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
dir = filepath.Join(cwd, pkg) dir = filepath.Join(cwd, pkg)
...@@ -124,19 +124,15 @@ func ImportPaths(patterns []string) []*search.Match { ...@@ -124,19 +124,15 @@ func ImportPaths(patterns []string) []*search.Match {
} }
info, err := os.Stat(dir) info, err := os.Stat(dir)
if err != nil || !info.IsDir() { if err != nil || !info.IsDir() {
// If the directory does not exist, // If the directory is local but does not exist, don't return it
// don't turn it into an import path // while loader is iterating, since this would trigger a fetch.
// that will trigger a lookup. // After loader is done iterating, we still need to return the
pkg = "" // path, so that "go list -e" produces valid output.
if !iterating { if iterating {
if err != nil { pkg = ""
base.Errorf("go: no such directory %v", m.Pattern)
} else {
base.Errorf("go: %s is not a directory", m.Pattern)
}
} }
} }
m.Pkgs[i] = pkg m.Pkgs[j] = pkg
} }
case strings.Contains(m.Pattern, "..."): case strings.Contains(m.Pattern, "..."):
......
...@@ -34,11 +34,11 @@ stderr 'import lookup disabled' ...@@ -34,11 +34,11 @@ stderr 'import lookup disabled'
! go build -mod=readonly ./nonexist ! go build -mod=readonly ./nonexist
! stderr 'import lookup disabled' ! stderr 'import lookup disabled'
stderr '^go: no such directory ./nonexist' stderr 'unknown import path "m/nonexist": cannot find package'
! go build -mod=readonly ./go.mod ! go build -mod=readonly ./go.mod
! stderr 'import lookup disabled' ! stderr 'import lookup disabled'
stderr '^go: ./go.mod is not a directory' stderr 'unknown import path "m/go.mod": cannot find package'
-- x/go.mod -- -- x/go.mod --
module m module m
......
...@@ -10,7 +10,9 @@ stdout ^math$ ...@@ -10,7 +10,9 @@ stdout ^math$
go list -f '{{.ImportPath}}' . go list -f '{{.ImportPath}}' .
stdout ^x$ stdout ^x$
! go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2 ! go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
stderr '^go: no such directory.*quote@v1.5.2' stderr 'unknown import path "rsc.io/quote": cannot find package'
go list -e -f '{{with .Error}}{{.}}{{end}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
stdout 'unknown import path "rsc.io/quote": cannot find package'
go mod download rsc.io/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 go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
stdout '^rsc.io/quote$' stdout '^rsc.io/quote$'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment