cmd/go: do not confuse files for standard library packages

I often create dummy files holding various data named things like 'z'.
If a file (not directory) GOROOT/src/z exists, it confuses cmd/go into
thinking z is a standard library package, which breaks the test
Script/mod_vendor.

This CL fixes internal/goroot to only report that something is a standard
library package when a directory with that name exists, not just a file.

Change-Id: I986c9a425e78d23c7e033aeadb8e9f71aab2b878
Reviewed-on: https://go-review.googlesource.com/c/go/+/461955
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
This commit is contained in:
Russ Cox 2023-01-13 10:21:56 -05:00 committed by Gopher Robot
parent f773bef9ab
commit 6bb003d032
2 changed files with 11 additions and 2 deletions

View File

@ -0,0 +1,9 @@
# Files in src should not be treated as packages
exists $GOROOT/src/regexp/testdata/README
go list -f '{{.Dir}}' regexp/testdata/README
-- go.mod --
module regexp/testdata/README
-- p.go --
package p

View File

@ -20,8 +20,8 @@ func IsStandardPackage(goroot, compiler, path string) bool {
switch compiler {
case "gc":
dir := filepath.Join(goroot, "src", path)
_, err := os.Stat(dir)
return err == nil
info, err := os.Stat(dir)
return err == nil && info.IsDir()
case "gccgo":
return gccgoSearch.isStandard(path)
default: