• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 39908198252

23 Oct 2026 05:52AM UTC coverage: 92.962% (+0.01%) from 92.952%
39908198252

push

github

web-flow
go: fix test coverage on Go v1.25+ by registering via `testdeps` (#23666)

Closes #20689, and fixes the breakage tracked in #17951.

## The bug

With a Go v1.25 or newer toolchain, `pants test --use-coverage` fails on
every Go package:

```
testing: cannot use -test.coverprofile because test binary was not built with coverage enabled
IndexError: tuple index out of range
```

Pants instruments sources with Go's legacy coverage mechanism (`go tool
cover -mode=<m> -var=<v>`), which stores counters in exported
package-level variables, and hands them to `testing` by generating a
`registerCover()` that calls `testing.RegisterCover`.

Go v1.20's "coverage redesign" made `testing.CoverMode()` read state
registered via `testing.MainStart` rather than anything `RegisterCover`
writes. Up to v1.24 Pants worked around that by building everything with
`GOEXPERIMENT=nocoverageredesign` (`sdk.py`), which restores the old
implementation — so coverage genuinely worked there. Go v1.25 removed
the experiment *and* gutted `RegisterCover` to an empty body, so
`CoverMode()` is always `""` and `testing` aborts with exit 2.

## The fix

On Go v1.25+, the generated setup code registers coverage the way
`cmd/go`'s own generated test main does — by pointing the exported
`testing/internal/testdeps` hooks (`CoverMode`, `CoverSnapshotFunc`,
`CoverProcessTestDirFunc`, `CoverMarkProfileEmittedFunc`) at a profile
writer which emits textfmt from the counters Pants already collects.

Registering there, rather than writing the profile after `m.Run()`:

|  | write after `m.Run()` | `testdeps` |
| -- | -- | -- |
| `testing.CoverMode()` | still `""` → fatal guard still fires |
non-empty → guard passes |
| `-test.coverprofile=cover.out` | must be removed; needs an env var or
custom flag | unchanged |
| `TestMain` doing `os.Exit(m.Run())` | **profile silently lost** |
written |
| `generate_testmain/main.go` | must restructure `main()` | **zero
change** |
| `testing.Coverage()` | broken | work... (continued)

132 of 137 new or added lines in 9 files covered. (96.35%)

1 existing line in 1 file now uncovered.

96573 of 103884 relevant lines covered (92.96%)

3.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.69
/src/python/pants_release/generate_github_workflows.py


Source Not Available

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc