From 16da15e99a35be28c6a9088a4057620d7ea5fc51 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 5 Jul 2018 16:25:07 -0400 Subject: [PATCH] cmd/go/internal/search: ignore submodules in ./... patterns CL 117257 handled path patterns like ... or x/... but not file system patterns like ./... or ./x/... . Fixes golang/go#24605 again. Change-Id: Ia5337a3490dfb3626b0af35199ae732fca0ed476 Reviewed-on: https://go-review.googlesource.com/122397 Reviewed-by: Bryan C. Mills --- vendor/cmd/go/internal/cfg/cfg.go | 5 +++++ vendor/cmd/go/internal/get/get.go | 2 +- vendor/cmd/go/internal/load/pkg.go | 16 ++++++++-------- vendor/cmd/go/internal/modload/init.go | 1 + vendor/cmd/go/internal/search/search.go | 9 +++++++++ vendor/cmd/go/internal/work/exec.go | 2 +- vendor/cmd/go/mod_test.go | 14 ++++++++------ 7 files changed, 33 insertions(+), 16 deletions(-) diff --git a/vendor/cmd/go/internal/cfg/cfg.go b/vendor/cmd/go/internal/cfg/cfg.go index f4817b8..26db867 100644 --- a/vendor/cmd/go/internal/cfg/cfg.go +++ b/vendor/cmd/go/internal/cfg/cfg.go @@ -69,6 +69,11 @@ var ( Goos = BuildContext.GOOS ExeSuffix string Gopath = filepath.SplitList(BuildContext.GOPATH) + + // ModulesEnabled specifies whether the go command is running + // in module-aware mode (as opposed to GOPATH mode). + // It is equal to modload.Enabled, but not all packages can import modload. + ModulesEnabled bool ) func init() { diff --git a/vendor/cmd/go/internal/get/get.go b/vendor/cmd/go/internal/get/get.go index 43aade2..76ba238 100644 --- a/vendor/cmd/go/internal/get/get.go +++ b/vendor/cmd/go/internal/get/get.go @@ -114,7 +114,7 @@ func init() { } func runGet(cmd *base.Command, args []string) { - if load.ModLookup != nil { + if cfg.ModulesEnabled { // Should not happen: main.go should install the separate module-enabled get code. base.Fatalf("go get: modules not implemented") } diff --git a/vendor/cmd/go/internal/load/pkg.go b/vendor/cmd/go/internal/load/pkg.go index b57b273..0e5e758 100644 --- a/vendor/cmd/go/internal/load/pkg.go +++ b/vendor/cmd/go/internal/load/pkg.go @@ -454,7 +454,7 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo var modErr error if isLocal { importPath = dirToImportPath(filepath.Join(srcDir, path)) - } else if ModLookup != nil { + } else if cfg.ModulesEnabled { parentPath := "" if parent != nil { parentPath = parent.ImportPath @@ -506,7 +506,7 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo bp.ImportPath = importPath if cfg.GOBIN != "" { bp.BinDir = cfg.GOBIN - } else if ModBinDir != nil { + } else if cfg.ModulesEnabled { bp.BinDir = ModBinDir() } if modDir == "" && err == nil && !isLocal && bp.ImportComment != "" && bp.ImportComment != path && @@ -596,7 +596,7 @@ func isDir(path string) bool { // If vendor expansion doesn't trigger, then the path is also subject to // Go 1.11 module legacy conversion (golang.org/issue/25069). func ResolveImportPath(parent *Package, path string) (found string) { - if ModLookup != nil { + if cfg.ModulesEnabled { parentPath := "" if parent != nil { parentPath = parent.ImportPath @@ -1184,7 +1184,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { // Install cross-compiled binaries to subdirectories of bin. elem = full } - if p.Internal.Build.BinDir == "" && ModBinDir != nil { + if p.Internal.Build.BinDir == "" && cfg.ModulesEnabled { p.Internal.Build.BinDir = ModBinDir() } if p.Internal.Build.BinDir != "" { @@ -1438,7 +1438,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { return } - if ModPackageModuleInfo != nil { + if cfg.ModulesEnabled { p.Module = ModPackageModuleInfo(p.ImportPath) if p.Name == "main" { p.Internal.BuildInfo = ModPackageBuildInfo(p.ImportPath, p.Deps) @@ -1722,7 +1722,7 @@ func ImportPaths(args []string) []string { if cmdlineMatchers == nil { SetCmdlinePatterns(search.CleanImportPaths(args)) } - if ModImportPaths != nil { + if cfg.ModulesEnabled { return ModImportPaths(args) } return search.ImportPaths(args) @@ -1820,7 +1820,7 @@ func GoFilesPackage(gofiles []string) *Package { } ctxt.ReadDir = func(string) ([]os.FileInfo, error) { return dirent, nil } - if ModImportFromFiles != nil { + if cfg.ModulesEnabled { ModImportFromFiles(gofiles) } @@ -1852,7 +1852,7 @@ func GoFilesPackage(gofiles []string) *Package { } if cfg.GOBIN != "" { pkg.Target = filepath.Join(cfg.GOBIN, exe) - } else if ModBinDir != nil { + } else if cfg.ModulesEnabled { pkg.Target = filepath.Join(ModBinDir(), exe) } } diff --git a/vendor/cmd/go/internal/modload/init.go b/vendor/cmd/go/internal/modload/init.go index 2b39a0e..5f9e72b 100644 --- a/vendor/cmd/go/internal/modload/init.go +++ b/vendor/cmd/go/internal/modload/init.go @@ -174,6 +174,7 @@ func Init() { base.Fatalf("go: cannot use modules with build cache disabled") } + cfg.ModulesEnabled = true enabled = true load.ModBinDir = BinDir load.ModLookup = Lookup diff --git a/vendor/cmd/go/internal/search/search.go b/vendor/cmd/go/internal/search/search.go index 35f469f..0c34009 100644 --- a/vendor/cmd/go/internal/search/search.go +++ b/vendor/cmd/go/internal/search/search.go @@ -173,6 +173,7 @@ func MatchPackagesInFS(pattern string) []string { if err != nil || !fi.IsDir() { return nil } + top := false if path == dir { // filepath.Walk starts at dir and recurses. For the recursive case, // the path is the result of filepath.Join, which calls filepath.Clean. @@ -182,6 +183,7 @@ func MatchPackagesInFS(pattern string) []string { // "cd $GOROOT/src; go list ./io/..." would incorrectly skip the io // package, because prepending the prefix "./" to the unclean path would // result in "././io", and match("././io") returns false. + top = true path = filepath.Clean(path) } @@ -192,6 +194,13 @@ func MatchPackagesInFS(pattern string) []string { return filepath.SkipDir } + if !top && cfg.ModulesEnabled { + // Ignore other modules found in subdirectories. + if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil { + return filepath.SkipDir + } + } + name := prefix + filepath.ToSlash(path) if !match(name) { return nil diff --git a/vendor/cmd/go/internal/work/exec.go b/vendor/cmd/go/internal/work/exec.go index 6408d61..765247d 100644 --- a/vendor/cmd/go/internal/work/exec.go +++ b/vendor/cmd/go/internal/work/exec.go @@ -599,7 +599,7 @@ func (b *Builder) build(a *Action) (err error) { fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built) } - if p.Internal.BuildInfo != "" && load.ModInfoProg != nil { + if p.Internal.BuildInfo != "" && cfg.ModulesEnabled { if err := b.writeFile(objdir+"_gomod_.go", load.ModInfoProg(p.Internal.BuildInfo)); err != nil { return err } diff --git a/vendor/cmd/go/mod_test.go b/vendor/cmd/go/mod_test.go index d5e63ec..fdea873 100644 --- a/vendor/cmd/go/mod_test.go +++ b/vendor/cmd/go/mod_test.go @@ -393,12 +393,14 @@ func TestModFSPatterns(t *testing.T) { tg.must(ioutil.WriteFile(tg.path("x/y/z/w/w.go"), []byte(`package w`), 0666)) tg.cd(tg.path("x")) - tg.run("list", "all") - tg.grepStdout(`^m$`, "expected m") - tg.grepStdout(`^m/vendor$`, "must see package named vendor") - tg.grepStdoutNot(`vendor/`, "must not see vendored packages") - tg.grepStdout(`^m/y$`, "expected m/y") - tg.grepStdoutNot(`^m/y/z`, "should ignore submodule m/y/z...") + for _, pattern := range []string{"all", "m/...", "./..."} { + tg.run("list", pattern) + tg.grepStdout(`^m$`, "expected m") + tg.grepStdout(`^m/vendor$`, "must see package named vendor") + tg.grepStdoutNot(`vendor/`, "must not see vendored packages") + tg.grepStdout(`^m/y$`, "expected m/y") + tg.grepStdoutNot(`^m/y/z`, "should ignore submodule m/y/z...") + } } func TestModGetVersions(t *testing.T) {