Skip to content

Commit a11644a

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: do not prune the module root when walking directories
When walking filesystem paths to locate packages, we normally prune out subdirectories with names beginning with ".", "_", or equal to "testdata". However, we should not prune out such a directory if it is at or above the module root, since its name is not part of the package path. Fixes #28481 Updates #27852 Change-Id: Ice82b1f908afaab50f5592f6c38ca6a0fe911edf Reviewed-on: https://go-review.googlesource.com/c/go/+/196297 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent e13dd94 commit a11644a

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/cmd/go/internal/modload/search.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,20 @@ func matchPackages(pattern string, tags map[string]bool, useStd bool, modules []
4848
return nil
4949
}
5050

51-
// Don't use GOROOT/src but do walk down into it.
52-
if path == root && importPathRoot == "" {
53-
return nil
54-
}
55-
5651
want := true
57-
// Avoid .foo, _foo, and testdata directory trees.
58-
_, elem := filepath.Split(path)
59-
if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
60-
want = false
52+
elem := ""
53+
54+
// Don't use GOROOT/src but do walk down into it.
55+
if path == root {
56+
if importPathRoot == "" {
57+
return nil
58+
}
59+
} else {
60+
// Avoid .foo, _foo, and testdata subdirectory trees.
61+
_, elem = filepath.Split(path)
62+
if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
63+
want = false
64+
}
6165
}
6266

6367
name := importPathRoot + filepath.ToSlash(path[len(root):])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Regression test for golang.org/issue/28481:
2+
# 'mod tidy' removed dependencies if the module root was
3+
# within a directory named 'testdata' or '_foo'.
4+
5+
env GO111MODULE=on
6+
7+
# A module should be allowed in a directory named testdata.
8+
cd $WORK/testdata
9+
go mod init testdata.tld/foo
10+
11+
# Building a package within that module should resolve its dependencies.
12+
go build
13+
grep 'rsc.io/quote' go.mod
14+
15+
# Tidying the module should preserve those dependencies.
16+
go mod tidy
17+
grep 'rsc.io/quote' go.mod
18+
19+
[short] stop
20+
21+
# Vendoring the module's dependencies should work too.
22+
go mod vendor
23+
exists vendor/rsc.io/quote
24+
25+
# The same should work in directories with names starting with underscores.
26+
cd $WORK/_ignored
27+
go mod init testdata.tld/foo
28+
29+
go build
30+
grep 'rsc.io/quote' go.mod
31+
32+
go mod tidy
33+
grep 'rsc.io/quote' go.mod
34+
35+
go mod vendor
36+
exists vendor/rsc.io/quote
37+
38+
-- $WORK/testdata/main.go --
39+
package foo
40+
41+
import _ "rsc.io/quote"
42+
-- $WORK/_ignored/main.go --
43+
package foo
44+
45+
import _ "rsc.io/quote"

0 commit comments

Comments
 (0)