From 6bb003d0323648d5f11689dab40ba4b158d7d6b4 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 13 Jan 2023 10:21:56 -0500 Subject: [PATCH] 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 Reviewed-by: Bryan Mills Auto-Submit: Russ Cox Run-TryBot: Russ Cox --- src/cmd/go/testdata/script/src_file.txt | 9 +++++++++ src/internal/goroot/gc.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/cmd/go/testdata/script/src_file.txt diff --git a/src/cmd/go/testdata/script/src_file.txt b/src/cmd/go/testdata/script/src_file.txt new file mode 100644 index 0000000000..8d5c20bc97 --- /dev/null +++ b/src/cmd/go/testdata/script/src_file.txt @@ -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 diff --git a/src/internal/goroot/gc.go b/src/internal/goroot/gc.go index 79403d29fc..c0216f4ea5 100644 --- a/src/internal/goroot/gc.go +++ b/src/internal/goroot/gc.go @@ -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: -- 2.48.1