Skip to content

Commit 16da15e

Browse files
committed
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 <[email protected]>
1 parent 4d8d2f8 commit 16da15e

File tree

7 files changed

+33
-16
lines changed

7 files changed

+33
-16
lines changed

vendor/cmd/go/internal/cfg/cfg.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ var (
6969
Goos = BuildContext.GOOS
7070
ExeSuffix string
7171
Gopath = filepath.SplitList(BuildContext.GOPATH)
72+
73+
// ModulesEnabled specifies whether the go command is running
74+
// in module-aware mode (as opposed to GOPATH mode).
75+
// It is equal to modload.Enabled, but not all packages can import modload.
76+
ModulesEnabled bool
7277
)
7378

7479
func init() {

vendor/cmd/go/internal/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func init() {
114114
}
115115

116116
func runGet(cmd *base.Command, args []string) {
117-
if load.ModLookup != nil {
117+
if cfg.ModulesEnabled {
118118
// Should not happen: main.go should install the separate module-enabled get code.
119119
base.Fatalf("go get: modules not implemented")
120120
}

vendor/cmd/go/internal/load/pkg.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo
454454
var modErr error
455455
if isLocal {
456456
importPath = dirToImportPath(filepath.Join(srcDir, path))
457-
} else if ModLookup != nil {
457+
} else if cfg.ModulesEnabled {
458458
parentPath := ""
459459
if parent != nil {
460460
parentPath = parent.ImportPath
@@ -506,7 +506,7 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo
506506
bp.ImportPath = importPath
507507
if cfg.GOBIN != "" {
508508
bp.BinDir = cfg.GOBIN
509-
} else if ModBinDir != nil {
509+
} else if cfg.ModulesEnabled {
510510
bp.BinDir = ModBinDir()
511511
}
512512
if modDir == "" && err == nil && !isLocal && bp.ImportComment != "" && bp.ImportComment != path &&
@@ -596,7 +596,7 @@ func isDir(path string) bool {
596596
// If vendor expansion doesn't trigger, then the path is also subject to
597597
// Go 1.11 module legacy conversion (golang.org/issue/25069).
598598
func ResolveImportPath(parent *Package, path string) (found string) {
599-
if ModLookup != nil {
599+
if cfg.ModulesEnabled {
600600
parentPath := ""
601601
if parent != nil {
602602
parentPath = parent.ImportPath
@@ -1184,7 +1184,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
11841184
// Install cross-compiled binaries to subdirectories of bin.
11851185
elem = full
11861186
}
1187-
if p.Internal.Build.BinDir == "" && ModBinDir != nil {
1187+
if p.Internal.Build.BinDir == "" && cfg.ModulesEnabled {
11881188
p.Internal.Build.BinDir = ModBinDir()
11891189
}
11901190
if p.Internal.Build.BinDir != "" {
@@ -1438,7 +1438,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
14381438
return
14391439
}
14401440

1441-
if ModPackageModuleInfo != nil {
1441+
if cfg.ModulesEnabled {
14421442
p.Module = ModPackageModuleInfo(p.ImportPath)
14431443
if p.Name == "main" {
14441444
p.Internal.BuildInfo = ModPackageBuildInfo(p.ImportPath, p.Deps)
@@ -1722,7 +1722,7 @@ func ImportPaths(args []string) []string {
17221722
if cmdlineMatchers == nil {
17231723
SetCmdlinePatterns(search.CleanImportPaths(args))
17241724
}
1725-
if ModImportPaths != nil {
1725+
if cfg.ModulesEnabled {
17261726
return ModImportPaths(args)
17271727
}
17281728
return search.ImportPaths(args)
@@ -1820,7 +1820,7 @@ func GoFilesPackage(gofiles []string) *Package {
18201820
}
18211821
ctxt.ReadDir = func(string) ([]os.FileInfo, error) { return dirent, nil }
18221822

1823-
if ModImportFromFiles != nil {
1823+
if cfg.ModulesEnabled {
18241824
ModImportFromFiles(gofiles)
18251825
}
18261826

@@ -1852,7 +1852,7 @@ func GoFilesPackage(gofiles []string) *Package {
18521852
}
18531853
if cfg.GOBIN != "" {
18541854
pkg.Target = filepath.Join(cfg.GOBIN, exe)
1855-
} else if ModBinDir != nil {
1855+
} else if cfg.ModulesEnabled {
18561856
pkg.Target = filepath.Join(ModBinDir(), exe)
18571857
}
18581858
}

vendor/cmd/go/internal/modload/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func Init() {
174174
base.Fatalf("go: cannot use modules with build cache disabled")
175175
}
176176

177+
cfg.ModulesEnabled = true
177178
enabled = true
178179
load.ModBinDir = BinDir
179180
load.ModLookup = Lookup

vendor/cmd/go/internal/search/search.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func MatchPackagesInFS(pattern string) []string {
173173
if err != nil || !fi.IsDir() {
174174
return nil
175175
}
176+
top := false
176177
if path == dir {
177178
// filepath.Walk starts at dir and recurses. For the recursive case,
178179
// the path is the result of filepath.Join, which calls filepath.Clean.
@@ -182,6 +183,7 @@ func MatchPackagesInFS(pattern string) []string {
182183
// "cd $GOROOT/src; go list ./io/..." would incorrectly skip the io
183184
// package, because prepending the prefix "./" to the unclean path would
184185
// result in "././io", and match("././io") returns false.
186+
top = true
185187
path = filepath.Clean(path)
186188
}
187189

@@ -192,6 +194,13 @@ func MatchPackagesInFS(pattern string) []string {
192194
return filepath.SkipDir
193195
}
194196

197+
if !top && cfg.ModulesEnabled {
198+
// Ignore other modules found in subdirectories.
199+
if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil {
200+
return filepath.SkipDir
201+
}
202+
}
203+
195204
name := prefix + filepath.ToSlash(path)
196205
if !match(name) {
197206
return nil

vendor/cmd/go/internal/work/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func (b *Builder) build(a *Action) (err error) {
599599
fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
600600
}
601601

602-
if p.Internal.BuildInfo != "" && load.ModInfoProg != nil {
602+
if p.Internal.BuildInfo != "" && cfg.ModulesEnabled {
603603
if err := b.writeFile(objdir+"_gomod_.go", load.ModInfoProg(p.Internal.BuildInfo)); err != nil {
604604
return err
605605
}

vendor/cmd/go/mod_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,14 @@ func TestModFSPatterns(t *testing.T) {
393393
tg.must(ioutil.WriteFile(tg.path("x/y/z/w/w.go"), []byte(`package w`), 0666))
394394

395395
tg.cd(tg.path("x"))
396-
tg.run("list", "all")
397-
tg.grepStdout(`^m$`, "expected m")
398-
tg.grepStdout(`^m/vendor$`, "must see package named vendor")
399-
tg.grepStdoutNot(`vendor/`, "must not see vendored packages")
400-
tg.grepStdout(`^m/y$`, "expected m/y")
401-
tg.grepStdoutNot(`^m/y/z`, "should ignore submodule m/y/z...")
396+
for _, pattern := range []string{"all", "m/...", "./..."} {
397+
tg.run("list", pattern)
398+
tg.grepStdout(`^m$`, "expected m")
399+
tg.grepStdout(`^m/vendor$`, "must see package named vendor")
400+
tg.grepStdoutNot(`vendor/`, "must not see vendored packages")
401+
tg.grepStdout(`^m/y$`, "expected m/y")
402+
tg.grepStdoutNot(`^m/y/z`, "should ignore submodule m/y/z...")
403+
}
402404
}
403405

404406
func TestModGetVersions(t *testing.T) {

0 commit comments

Comments
 (0)