Skip to content

Stop linking testify into production binaries via testsignal - #1342

Merged
brandur merged 2 commits into
riverqueue:masterfrom
e-yavuz-1:fix/testsignal-no-testify
Aug 5, 2026
Merged

Stop linking testify into production binaries via testsignal#1342
brandur merged 2 commits into
riverqueue:masterfrom
e-yavuz-1:fix/testsignal-no-testify

Conversation

@e-yavuz-1

Copy link
Copy Markdown
Contributor

testsignal is production code — its own doc comment says it's designed to have "minimal impact on the production code that calls into it" — but it imported riversharedtest for a single function, WaitTimeout.

Go initializes every imported package, so nothing in that graph can be eliminated by the linker. The result is that testify, go-spew, goleak, and yaml end up in every binary that uses River. Measured with go tool nm -size on a minimal program importing river and riverdriver/riverpgxv5:

github.com/stretchr/testify  18 symbols / 1168 bytes
github.com/davecgh/go-spew   11 symbols / 1312 bytes
go.uber.org/goleak            8 symbols /  896 bytes
gopkg.in/yaml                22 symbols / 4320 bytes
riversharedtest              13 symbols / 2248 bytes

WaitTimeout depends only on os and time, so move it to rivershared/util/testutil, which testsignal already imports and which has no dependencies outside the standard library. riversharedtest.WaitTimeout stays as a wrapper so no public API changes, and its two internal callers, WaitOrTimeout and WaitOrTimeoutN, are untouched. Its test moves alongside the implementation.

After the move, the same program links no testify, go-spew, goleak, yaml, or riversharedtest symbols at all: 951 fewer symbols and 957 KB less binary.

Yavuz added 2 commits August 4, 2026 23:15
`testsignal` is production code — its own doc comment says it's designed
to have "minimal impact on the production code that calls into it" — but
it imported `riversharedtest` for a single function, `WaitTimeout`.

Go initializes every imported package, so nothing in that graph can be
eliminated by the linker. The result is that testify, go-spew, goleak,
and yaml end up in every binary that uses River. Measured with
`go tool nm -size` on a minimal program importing `river` and
`riverdriver/riverpgxv5`:

    github.com/stretchr/testify  18 symbols / 1168 bytes
    github.com/davecgh/go-spew   11 symbols / 1312 bytes
    go.uber.org/goleak            8 symbols /  896 bytes
    gopkg.in/yaml                22 symbols / 4320 bytes
    riversharedtest              13 symbols / 2248 bytes

`WaitTimeout` depends only on `os` and `time`, so move it to
`rivershared/util/testutil`, which `testsignal` already imports and which
has no dependencies outside the standard library. `riversharedtest.WaitTimeout`
stays as a wrapper so no public API changes, and its two internal callers,
`WaitOrTimeout` and `WaitOrTimeoutN`, are untouched. Its test moves
alongside the implementation.

After the move, the same program links no testify, go-spew, goleak, yaml,
or riversharedtest symbols at all: 951 fewer symbols and 957 KB less
binary.
@brandur

brandur commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@e-yavuz-1 Dang great find. Do you think could sign our CLA? https://github.com/riverqueue/rivercla

Otherwise, LGTM.

@brandur

brandur commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Also adding #1343 for a more structural fix by checking for this problem during linting.

@e-yavuz-1

e-yavuz-1 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

@brandur CLA signed.

@brandur
brandur merged commit 25de710 into riverqueue:master Aug 5, 2026
12 checks passed
brandur added a commit that referenced this pull request Aug 6, 2026
This one's aimed at producing a more permanent structural fix for #1342
by keeping an eye out for the same problematic condition using the
depguard lint. As a summary of the problem corrected by #1342:

* We use the `testsignal` package in all kinds of non-test code, so
  packages including River always build against it.

* `testsignal` was importing `riversharedtest`, which imports Testify,
  Goleak, YAML (through Testify).

* This was causing all packages built against River to pick up an extra
  ~10 kB worth of dependencies in their production builds, which is bad.

Here, add some new rules for depguard:

* Don't allow `testsignal` to have any dependencies beyond stdlib and
  `testutil`.

* Don't allow `testutil` to have any non-stdlib dependencies. This rule
  is so that `testsignal` doesn't pick up unexpected dependencies
  transitively through `testutil`.
brandur added a commit that referenced this pull request Aug 6, 2026
This one's aimed at producing a more permanent structural fix for #1342
by keeping an eye out for the same problematic condition using the
depguard lint. As a summary of the problem corrected by #1342:

* We use the `testsignal` package in all kinds of non-test code, so
  packages including River always build against it.

* `testsignal` was importing `riversharedtest`, which imports Testify,
  Goleak, YAML (through Testify).

* This was causing all packages built against River to pick up an extra
  ~10 kB worth of dependencies in their production builds, which is bad.

Here, add some new rules for depguard:

* Don't allow test packages like Goleak or Testify to be imported by any
  non-test Go files. We make an exception for internal test support
  packages like `riverdbtest` and `riverdrivertest`.

* Don't allow `testsignal` to have any dependencies beyond stdlib and
  `testutil`.

* Don't allow `testutil` to have any non-stdlib dependencies. This rule
  is so that `testsignal` doesn't pick up unexpected dependencies
  transitively through `testutil`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants