Skip to content

Commit 6c1c055

Browse files
committed
cmd/internal/moddeps: use filepath.SkipDir only on directories
If a filepath.WalkFunc returns filepath.SkipDir when invoked on a non-directory file, it skips the remaining files in the containing directory.¹ CL 276272 accidentally added a code path that triggers this behavior whenever filepath.Walk reaches a non-directory file that begins with a dot, such as .gitattributes or .DS_Store, causing findGorootModules to return early without finding any modules in GOROOT. Tests that use it ceased to provide test coverage that the tree is tidy. Add an explicit check for info.IsDir in the 5 places that intend to use filepath.SkipDir to skip traversing that directory. Even paths like GOROOT/bin and GOROOT/pkg which are unlikely to be anything but a directory are worth checking, since the goal of moddeps is to take a possibly problematic GOROOT tree as input and detect problems. While the goal of findGorootModules is to find all modules in GOROOT programmatically (in case new modules are added or modified), there are 4 modules now that are quite likely to exist, so check for their presence to avoid similar regressions. (It's not hard to update this test if a well-known GOROOT module is removed or otherwise modified; but if it becomes hard we can simplify it to check for a reasonable number of modules instead.) Also fix the minor skew that has crept in since the test got disabled. ¹ This wasn't necessarily an intentional design decision, but it was found only when Go 1.4 was already out. See CL 11690 for details. Fixes #46254. Change-Id: Id55ed926f8c0094b1af923070de72bacca05996f Reviewed-on: https://go-review.googlesource.com/c/go/+/320991 Trust: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 658b5e6 commit 6c1c055

File tree

8 files changed

+137
-48
lines changed

8 files changed

+137
-48
lines changed

src/cmd/internal/moddeps/moddeps_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func makeGOROOTCopy(t *testing.T) string {
227227
if err != nil {
228228
return err
229229
}
230-
if src == filepath.Join(runtime.GOROOT(), ".git") {
230+
if info.IsDir() && src == filepath.Join(runtime.GOROOT(), ".git") {
231231
return filepath.SkipDir
232232
}
233233

@@ -237,9 +237,8 @@ func makeGOROOTCopy(t *testing.T) string {
237237
}
238238
dst := filepath.Join(gorootCopyDir, rel)
239239

240-
switch src {
241-
case filepath.Join(runtime.GOROOT(), "bin"),
242-
filepath.Join(runtime.GOROOT(), "pkg"):
240+
if info.IsDir() && (src == filepath.Join(runtime.GOROOT(), "bin") ||
241+
src == filepath.Join(runtime.GOROOT(), "pkg")) {
243242
// If the OS supports symlinks, use them instead
244243
// of copying the bin and pkg directories.
245244
if err := os.Symlink(src, dst); err == nil {
@@ -414,15 +413,15 @@ func findGorootModules(t *testing.T) []gorootModule {
414413
if info.IsDir() && (info.Name() == "vendor" || info.Name() == "testdata") {
415414
return filepath.SkipDir
416415
}
417-
if path == filepath.Join(runtime.GOROOT(), "pkg") {
416+
if info.IsDir() && path == filepath.Join(runtime.GOROOT(), "pkg") {
418417
// GOROOT/pkg contains generated artifacts, not source code.
419418
//
420419
// In https://golang.org/issue/37929 it was observed to somehow contain
421420
// a module cache, so it is important to skip. (That helps with the
422421
// running time of this test anyway.)
423422
return filepath.SkipDir
424423
}
425-
if strings.HasPrefix(info.Name(), "_") || strings.HasPrefix(info.Name(), ".") {
424+
if info.IsDir() && (strings.HasPrefix(info.Name(), "_") || strings.HasPrefix(info.Name(), ".")) {
426425
// _ and . prefixed directories can be used for internal modules
427426
// without a vendor directory that don't contribute to the build
428427
// but might be used for example as code generators.
@@ -457,8 +456,31 @@ func findGorootModules(t *testing.T) []gorootModule {
457456
goroot.modules = append(goroot.modules, m)
458457
return nil
459458
})
460-
})
459+
if goroot.err != nil {
460+
return
461+
}
461462

463+
// knownGOROOTModules is a hard-coded list of modules that are known to exist in GOROOT.
464+
// If findGorootModules doesn't find a module, it won't be covered by tests at all,
465+
// so make sure at least these modules are found. See issue 46254. If this list
466+
// becomes a nuisance to update, can be replaced with len(goroot.modules) check.
467+
knownGOROOTModules := [...]string{
468+
"std",
469+
"cmd",
470+
"misc",
471+
"test/bench/go1",
472+
}
473+
var seen = make(map[string]bool) // Key is module path.
474+
for _, m := range goroot.modules {
475+
seen[m.Path] = true
476+
}
477+
for _, m := range knownGOROOTModules {
478+
if !seen[m] {
479+
goroot.err = fmt.Errorf("findGorootModules didn't find the well-known module %q", m)
480+
break
481+
}
482+
}
483+
})
462484
if goroot.err != nil {
463485
t.Fatal(goroot.err)
464486
}

src/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ go 1.17
55
require (
66
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
77
golang.org/x/net v0.0.0-20210510120150-4163338589ed
8-
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 // indirect
8+
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
99
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f // indirect
1010
)

src/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e h1:8foAy0aoO5GkqCvAEJ4VC4
22
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
33
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
44
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
5-
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 h1:cdsMqa2nXzqlgs183pHxtvoVwU7CyzaCTAUOg94af4c=
6-
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5+
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
6+
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f h1:yQJrRE0hDxDFmZLlRaw+3vusO4fwNHgHIjUOMO7bHYI=
88
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=

src/net/http/h2_bundle.go

Lines changed: 100 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/net/http/socks_bundle.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vendor/golang.org/x/sys/cpu/cpu.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vendor/golang.org/x/sys/cpu/cpu_aix.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ golang.org/x/net/idna
1818
golang.org/x/net/lif
1919
golang.org/x/net/nettest
2020
golang.org/x/net/route
21-
# golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6
21+
# golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744
2222
## explicit; go 1.17
2323
golang.org/x/sys/cpu
2424
# golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f

0 commit comments

Comments
 (0)