Skip to content

Commit

Permalink
cmd/go/internal/search: ignore submodules in ./... patterns
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rsc committed Jul 12, 2018
1 parent 4d8d2f8 commit 16da15e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions vendor/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion vendor/cmd/go/internal/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
16 changes: 8 additions & 8 deletions vendor/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions vendor/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions vendor/cmd/go/internal/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vendor/cmd/go/internal/work/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions vendor/cmd/go/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 16da15e

Please sign in to comment.