]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: do not confuse files for standard library packages
authorRuss Cox <rsc@golang.org>
Fri, 13 Jan 2023 15:21:56 +0000 (10:21 -0500)
committerGopher Robot <gobot@golang.org>
Tue, 17 Jan 2023 22:30:23 +0000 (22:30 +0000)
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>

src/cmd/go/testdata/script/src_file.txt [new file with mode: 0644]
src/internal/goroot/gc.go

diff --git a/src/cmd/go/testdata/script/src_file.txt b/src/cmd/go/testdata/script/src_file.txt
new file mode 100644 (file)
index 0000000..8d5c20b
--- /dev/null
@@ -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
index 79403d29fc02e55c30a929be3eee2478f02e6062..c0216f4ea53d728f711b02c3af748110fe46812b 100644 (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: