-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/go: coverpkg doesn't ignore directories starting with '.' #66038
Comments
Observation, the behaviour changes if I vendor the dependencies: $ GOMODCACHE="$PWD/.go/mod_cache" GOCACHE="$PWD/.go/cache" go mod vendor
$ GOMODCACHE="$PWD/.go/mod_cache" GOCACHE="$PWD/.go/cache" go test -coverprofile=coverage.out -coverpkg=./... ./...
ok example-proj 0.002s coverage: 100.0% of statements in ./... Digging further, here's the first quick-and-dirty (not acceptable, it breaks a test in diff --git a/src/cmd/go/internal/load/search.go b/src/cmd/go/internal/load/search.go
index 565996a21f..da533661c6 100644
--- a/src/cmd/go/internal/load/search.go
+++ b/src/cmd/go/internal/load/search.go
@@ -39,7 +39,7 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
return false
}
rel = filepath.ToSlash(rel)
- if rel == ".." || strings.HasPrefix(rel, "../") {
+ if rel == ".." || strings.HasPrefix(rel, "../") || rel[0] == '.' || rel[0] == '_' {
return false
}
return matchPath(rel) In the case of vendoring the |
(attn @thanm) |
Here's an updated patch which doesn't break existing tests, and includes a new diff --git a/src/cmd/go/internal/load/flag_test.go b/src/cmd/go/internal/load/flag_test.go
index d3223e12d5..f5b38f5323 100644
--- a/src/cmd/go/internal/load/flag_test.go
+++ b/src/cmd/go/internal/load/flag_test.go
@@ -92,6 +92,7 @@ type ppfTest struct {
ppfDirTest("./...", 3, "/my/test/dir", "/my/test/dir/sub", "/my/test/dir/sub/sub", "/my/test/other", "/my/test/other/sub"),
ppfDirTest("../...", 4, "/my/test/dir", "/my/test/other", "/my/test/dir/sub", "/my/test/other/sub", "/my/other/test"),
ppfDirTest("../...sub...", 3, "/my/test/dir/sub", "/my/test/othersub", "/my/test/yellowsubmarine", "/my/other/test"),
+ ppfDirTest("./...", 1, "/my/test/dir", "/my/test/dir/.sub", "/test/dir/_sub", "/test/dir/testdata/sub"),
}
func ppfDirTest(pattern string, nmatch int, dirs ...string) ppfTest {
diff --git a/src/cmd/go/internal/load/search.go b/src/cmd/go/internal/load/search.go
index 565996a21f..3af07a9475 100644
--- a/src/cmd/go/internal/load/search.go
+++ b/src/cmd/go/internal/load/search.go
@@ -38,10 +38,20 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
// Cannot make relative - e.g. different drive letters on Windows.
return false
}
+
rel = filepath.ToSlash(rel)
if rel == ".." || strings.HasPrefix(rel, "../") {
return false
}
+
+ cwdRel, err := filepath.Rel(cwd, p.Dir)
+ if (
+ err == nil && cwdRel != "." && !strings.HasPrefix(cwdRel, "../") &&
+ // Avoid .foo, _foo, and testdata subdirectory trees.
+ (strings.HasPrefix(cwdRel, ".") || strings.HasPrefix(cwdRel, "_") || cwdRel == "testdata")) {
+ return false
+ }
+
return matchPath(rel)
}
case pattern == "all": |
Thanks for the report, and thanks for sending a patch.
That would be fine with me, please do. I tried your fix in my own repo and it looks good to me. |
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
Thanks, I've created #66171 for this |
Change https://go.dev/cl/569895 mentions this issue: |
Looks good. I left some comments on the CL. |
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. The scope of the change is limted to package matching to only the -coverpkg flag of `go test` to avoid impacting -gcflags and the other per package flags, e.g. we don't want to change behaviour for a user building something that imports a leading dot package who wants to set gcflags for it Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. The scope of the change is limted to package matching to only the -coverpkg flag of `go test` to avoid impacting -gcflags and the other per package flags, e.g. we don't want to change behaviour for a user building something that imports a leading dot package who wants to set gcflags for it Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. The scope of the change is limted to package matching to only the -coverpkg flag of `go test` to avoid impacting -gcflags and the other per package flags, e.g. we don't want to change behaviour for a user building something that imports a leading dot package who wants to set gcflags for it Two tests are added for this change, one is a regression test attempting to directly replicate the behaviour described in the issue, the other is updating another test I saw fail when trialling other solutions to this issue so I thought it worthwhile to be explicit about the change there. See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
The pattern passed to `-coverpkg` when running `go test` would not ignore directories usually ignored by the `go` command, i.e. those beginning with "." or "_" are ignored by the go tool, as are directories named "testdata". Fix this by adding an explicit check for these (by following a similar check in `src/cmd/doc/dirs.go`[1]) allowing us to ignore them. The scope of the change is limted to package matching to only the -coverpkg flag of `go test` to avoid impacting -gcflags and the other per package flags, e.g. we don't want to change behaviour for a user building something that imports a leading dot package who wants to set gcflags for it See linked issue for a reproduction. Fixes golang#66038 [1] https://go.googlesource.com/go/+/16e5d24480dca7ddcbdffb78a8ed5de3e5155dec/src/cmd/doc/dirs.go#136
Go version
go version go1.22.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Ran Go tests with
-coverpkg=./...
in a directory which includedGOMODCACHE/GOCACHE
in a subdirectory starting with"."
(the use case is for caching in GitLab, which requires directories stored in caches exist in the working directory, see their Go example https://docs.gitlab.com/ee/ci/caching/#cache-go-dependencies)Here's a bash script to reproduce:
What did you see happen?
The modules under
.go
were included in test coverage, output of the above script (note the coverage % in the last line):inspecting the coverage profile:
What did you expect to see?
Modules under the
.go
directory to be ignored, per https://pkg.go.dev/cmd/go#hdr-Package_lists_and_patternsEDIT: for anyone looking for a workaround, I just filtered out lines not belonging to my module in the coverage file:
The text was updated successfully, but these errors were encountered: