Open matthewhughes934 opened 6 months ago
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 cmd/go/internal/load
) change I found to give the behaviour I expected:
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 rel
path there is like "vendor/github.com/russross/blackfriday/v2"
and matchPath
will return false
. In the other case the path is e.g. .go/mod_cache/github.com/russross/blackfriday/v2@v2.1.0"
(attn @thanm)
Here's an updated patch which doesn't break existing tests, and includes a new PerPackageFlag
(just because that was the first place I found that was directly checking the behaviour of the package matching). Happy to submit for review if it is at all reasonable.
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.
Happy to submit for review if it is at all reasonable.
That would be fine with me, please do. I tried your fix in my own repo and it looks good to me.
Thanks for the report, and thanks for sending a patch.
Happy to submit for review if it is at all reasonable.
That would be fine with me, please do. I tried your fix in my own repo and it looks good to me.
Thanks, I've created https://github.com/golang/go/pull/66171 for this
Change https://go.dev/cl/569895 mentions this issue: cmd/go: fix
-coverpkgnot ignoring special directories
Looks good. I left some comments on the CL.
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: