Skip to content

Commit 203abfb

Browse files
committed
cmd/go/internal/vgo: fix handling of vendor dir in package patterns
A directory x/vendor containing code is just a package called vendor. A directory x/vendor/y is vendored code. When a package pattern scans the current module's file tree it should include a package called vendor but should not include the directories containing vendored code. Maybe related to a comment on golang/go#25624. Change-Id: I083a98a9c70c2121cff7a2f394ff985a54bed37a Reviewed-on: https://go-review.googlesource.com/116759 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 20f1cf1 commit 203abfb

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,18 @@ func matchPackages(pattern string, buildList []module.Version) []string {
131131
return filepath.SkipDir
132132
}
133133

134-
if have[name] {
135-
return nil
136-
}
137-
have[name] = true
138-
if !match(name) {
139-
return nil
140-
}
141-
if _, _, err := imports.ScanDir(path, imports.Tags()); err == imports.ErrNoGo {
142-
return nil
134+
if !have[name] {
135+
have[name] = true
136+
if match(name) {
137+
if _, _, err := imports.ScanDir(path, imports.Tags()); err != imports.ErrNoGo {
138+
pkgs = append(pkgs, name)
139+
}
140+
}
143141
}
144142

145143
if elem == "vendor" {
146-
return filepath.SkipDir // ignore children
144+
return filepath.SkipDir
147145
}
148-
149-
pkgs = append(pkgs, name)
150146
return nil
151147
})
152148
}

vendor/cmd/go/vgo_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ func TestTags(t *testing.T) {
137137
tg.grepStdout(`\[x.go y.go\]`, "Go source files for tag1 and tag2")
138138
}
139139

140+
func TestAllVsVendor(t *testing.T) {
141+
tg := testgo(t)
142+
defer tg.cleanup()
143+
tg.makeTempdir()
144+
145+
tg.must(os.MkdirAll(tg.path("x/vendor/v"), 0777))
146+
tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
147+
module m
148+
`), 0666))
149+
150+
tg.must(ioutil.WriteFile(tg.path("x/x.go"), []byte(`package x`), 0666))
151+
tg.must(ioutil.WriteFile(tg.path("x/vendor/v/v.go"), []byte(`package v; import "golang.org/x/crypto"`), 0666))
152+
tg.must(ioutil.WriteFile(tg.path("x/vendor/v.go"), []byte(`package main`), 0666))
153+
154+
tg.cd(tg.path("x"))
155+
tg.run("-vgo", "list", "all")
156+
tg.grepStdout(`^m$`, "expected m")
157+
tg.grepStdout(`^m/vendor$`, "must see package named vendor")
158+
tg.grepStdoutNot(`vendor/`, "must not see vendored packages")
159+
}
160+
140161
func TestFillGoMod(t *testing.T) {
141162
testenv.MustHaveExternalNetwork(t)
142163
tg := testgo(t)

0 commit comments

Comments
 (0)