]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: emit an error for extraneous files in GOROOT/src in module mode
authorMichael Matloob <matloob@golang.org>
Fri, 24 Jan 2020 22:21:48 +0000 (17:21 -0500)
committerMichael Matloob <matloob@golang.org>
Tue, 25 Feb 2020 22:42:00 +0000 (22:42 +0000)
If there's a go file immediately in GOROOT/src, it was probably
accidentally added by the user. Since that package shouldn't
exist, return an error if a user tries to list it. We're only making
this change for GOPATH mode because we don't want to break cases
where users have been doing this historically, but want to fix
this case for the future.

This also leaves open the weird cases where files are placed directly
in vendor directories.

Fixes #36587

Change-Id: I9738e47b1e89fd5048cbb8dd28e44648834b8ea7
Reviewed-on: https://go-review.googlesource.com/c/go/+/216381
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modload/import.go
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/list_gofile_in_goroot.txt [new file with mode: 0644]

index d7fca8fd2c5c9f15b3a348f2dea279ca9757fd50..309d654987e433081d73ef2fa8fa3fa8ef8f4465 100644 (file)
@@ -126,6 +126,8 @@ func Import(path string) (m module.Version, dir string, err error) {
                }
                dir := filepath.Join(cfg.GOROOT, "src", path)
                return module.Version{}, dir, nil
+       } else if pathIsStd && path == cfg.GOROOTsrc {
+               return module.Version{}, dir, errors.New("directory should not directly contain source files")
        }
 
        // -mod=vendor is special.
index 89d906f194d438daf654286dd062e22f98e65d95..b28776b81c79fe1a5332c04f5ad2145725b615f7 100644 (file)
@@ -126,6 +126,12 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
                                        // It's not strictly necessary but helpful to keep the checks.
                                        if modRoot != "" && dir == modRoot {
                                                pkg = targetPrefix
+                                               if modRoot == cfg.GOROOTsrc {
+                                                       // A package in GOROOT/src would have an empty path.
+                                                       // Keep the path as cfg.GOROOTsrc. We'll report an error in Import.
+                                                       // See golang.org/issue/36587.
+                                                       pkg = modRoot
+                                               }
                                        } else if modRoot != "" && strings.HasPrefix(dir, modRoot+string(filepath.Separator)) && !strings.Contains(dir[len(modRoot):], "@") {
                                                suffix := filepath.ToSlash(dir[len(modRoot):])
                                                if strings.HasPrefix(suffix, "/vendor/") {
diff --git a/src/cmd/go/testdata/script/list_gofile_in_goroot.txt b/src/cmd/go/testdata/script/list_gofile_in_goroot.txt
new file mode 100644 (file)
index 0000000..0a3b128
--- /dev/null
@@ -0,0 +1,46 @@
+# Return an error if the user tries to list a go source file directly in $GOROOT/src.
+# Tests golang.org/issue/36587
+
+mkdir $WORK/fakegoroot/src
+mkdir $WORK/fakegopath/src
+
+env GOROOT=$WORK/fakegoroot
+env GOPATH=$WORK/fakegopath
+
+cp go.mod $GOROOT/src/go.mod
+cp foo.go $GOROOT/src/foo.go
+
+go env GOROOT
+stdout $WORK(/|\\)fakegoroot
+
+# switch to GOROOT/src
+cd $GOROOT/src
+
+# GO111MODULE=on,GOROOT
+env GO111MODULE=on
+! go list ./...
+stderr 'directory should not directly contain source files'
+go list -e .
+go list -f '{{if .Error}}{{.Error.Err}}{{end}}' -e ./...
+stdout 'directory should not directly contain source files'
+
+# GO111MODULE=off,GOROOT
+env GO111MODULE=off
+go list ./...
+[!windows] stdout _$WORK/fakegoroot/src
+[windows] stdout fakegoroot/src # On windows the ":" in the volume name is mangled
+
+# switch to GOPATH/src
+cp $WORK/gopath/src/foo.go $GOPATH/src/foo.go
+cd $GOPATH/src
+
+# GO111MODULE=off,GOPATH
+env GO111MODULE=off
+go list ./...
+
+-- go.mod --
+module g
+
+go 1.14
+-- foo.go --
+package foo
\ No newline at end of file