Skip to content

fix(testing): propagate LLVM coverage env from isolate_subprocess_env - #2713

Merged
max-sixty merged 1 commit into
mainfrom
profraw-cleanup
May 11, 2026
Merged

max-sixty merged 1 commit into
mainfrom
profraw-cleanup

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

Move the LLVM_PROFILE_FILE / CARGO_LLVM_COV* passthrough loop into isolate_subprocess_env and hoist the env-var list to a shared COVERAGE_ENV_VARS constant. Today the passthrough only runs inside configure_cli_command (the test-side superset). Benches use isolate_subprocess_env directly per its docstring, so any future caller that env_clear()s before us would silently drop the coverage env and the instrumented child would write default_<hash>_<pid>_*.profraw next to its cwd instead of the path cargo llvm-cov chose.

The original report was a stray default_*.profraw at the worktree root from an earlier session. I could not deterministically reproduce the leak under cargo llvm-cov --benches or cargo llvm-cov --test integration after auditing the test/bench call sites, so this is defense-in-depth at the helper that owns the contract rather than a targeted root-cause fix — flagged in commentary above the new const.

Changes

  • src/testing/mod.rs — new pub const COVERAGE_ENV_VARS; isolate_subprocess_env re-applies them at the end; matching block removed from configure_cli_command.
  • tests/common/mod.rs, tests/common/progressive_output.rs — the PTY helpers (pass_coverage_env_to_pty_cmd, configure_pty_environment) iterate the same constant.

.gitignore is intentionally untouched so a future regression surfaces in git status instead of getting silently hidden.

Test plan

  • cargo run -- hook pre-merge --yes — green (3627 tests passed, pre-commit hooks clean, doctest + cargo doc clean).
  • cargo llvm-cov --benches --no-report -- skeleton/warm/1 — green, no stray *.profraw at repo root.
  • cargo llvm-cov --test integration --no-report -- 'integration_tests::shell_wrapper::...test_bash_completion_produces_correct_output' '...user_hooks::test_var_flag_invalid_format_fails' '...readme_sync' '...help::' — 61 passed, no stray *.profraw.
  • Existing isolate_subprocess_env_scrubs_git_and_worktrunk_keys unit test still passes.

Move the `LLVM_PROFILE_FILE` / `CARGO_LLVM_COV*` passthrough loop into
`isolate_subprocess_env` and hoist the env-var list to a shared
`COVERAGE_ENV_VARS` constant. `configure_cli_command` already did this
re-apply, but the test-side superset isn't the only caller — benches use
`isolate_subprocess_env` directly per its docstring, so any future caller
that does `env_clear()` before us would silently drop coverage env, and an
instrumented child would write `default_<hash>_<pid>_*.profraw` next to its
cwd instead of the path `cargo llvm-cov` chose. The PTY helpers
(`pass_coverage_env_to_pty_cmd`, `progressive_output::configure_pty_environment`)
now iterate the same constant so the three lists stay in sync.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One doc nit: the COVERAGE_ENV_VARS rustdoc still describes the PTY helpers as a "mirrored" list to keep in sync, but the same PR makes them iterate this constant directly. Suggestion inline.

Comment thread src/testing/mod.rs
Comment on lines +298 to +303
/// LLVM coverage env vars that the parent's `cargo llvm-cov` / `cargo affected`
/// run sets to point spawned binaries at the right `.profraw` file. Mirrored
/// in `tests/common/mod.rs::pass_coverage_env_to_pty_cmd` for PTY tests that
/// `env_clear()`. Keep both lists in sync — dropping `LLVM_PROFILE_FILE` makes
/// an instrumented child fall back to LLVM's default `default_*.profraw` in
/// the child's cwd, leaving stray files at the repo root.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// LLVM coverage env vars that the parent's `cargo llvm-cov` / `cargo affected`
/// run sets to point spawned binaries at the right `.profraw` file. Mirrored
/// in `tests/common/mod.rs::pass_coverage_env_to_pty_cmd` for PTY tests that
/// `env_clear()`. Keep both lists in sync — dropping `LLVM_PROFILE_FILE` makes
/// an instrumented child fall back to LLVM's default `default_*.profraw` in
/// the child's cwd, leaving stray files at the repo root.
/// LLVM coverage env vars that the parent's `cargo llvm-cov` / `cargo affected`
/// run sets to point spawned binaries at the right `.profraw` file. The PTY
/// helpers (`pass_coverage_env_to_pty_cmd`, `configure_pty_environment`)
/// iterate this same list to re-apply it after `env_clear()`. Dropping
/// `LLVM_PROFILE_FILE` makes an instrumented child fall back to LLVM's default
/// `default_*.profraw` in the child's cwd, leaving stray files at the repo root.

@max-sixty
max-sixty merged commit 434b847 into main May 11, 2026
32 checks passed
@max-sixty
max-sixty deleted the profraw-cleanup branch May 11, 2026 18:22
max-sixty added a commit that referenced this pull request May 11, 2026
## Summary

Adds a `git status --porcelain` check after `wt hook pre-merge --yes` on
each `test (linux/macos/windows)` job. If anything's been written into
the working tree (outside `target/`), CI fails with the file list.
Catches profraw strays, leftover hook logs, generated files tests forgot
to clean, etc.

Motivated by the `default_*_<pid>.profraw` stray that prompted #2713 —
that PR defended the helper, this PR detects future regressions of the
same class (whatever the source). `.gitignore` is intentionally
untouched: future leaks surface here rather than being silently hidden.

## Test plan

- [ ] CI passes on this PR (sanity check: the guard itself doesn't fire
on a clean run).
- [ ] If it does fire on something pre-existing, surface what and fix.

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request May 12, 2026
…nherited (#2730)

## Summary

Defaults `LLVM_PROFILE_FILE` to
`std::env::temp_dir()/wt-test-profraw/cov-%m_%p.profraw` when nothing is
inherited from the parent. `isolate_subprocess_env` previously only
*propagated* the var (per #2713), leaving it unset under plain `cargo
test`. An instrumented child — most commonly a stale `mock-stub` left
instrumented by an earlier coverage build — then fell back to writing
`default_<hash>_<pid>.profraw` into its cwd. For any `wt list` snapshot
that spawns a mock, that cwd is the test worktree, so `git status
--porcelain` saw the profraw as an untracked file and `wt list` rendered
"Showing 5 worktrees, 1 with changes, …" instead of "Showing 5
worktrees, 4 ahead". Different subsets of the gitea CI-status tests
would flake every parallel run.

`default_llvm_profile_file()` is a thin wrapper over
`default_llvm_profile_file_with(inherited)` (same outer/inner split as
`isolate_subprocess_env` / `isolate_subprocess_env_from`) so a unit test
exercises both the inherited and the temp-dir-fallback branches without
mutating process env. `COVERAGE_ENV_VARS` is shrunk to `[CARGO_LLVM_COV,
CARGO_LLVM_COV_TARGET_DIR]` — `LLVM_PROFILE_FILE` is no longer in it
because it's now governed by `default_llvm_profile_file()` — and all
three call sites (`isolate_subprocess_env`,
`pass_coverage_env_to_pty_cmd`, `configure_pty_environment`) route
through that helper plus the constant.

Under `cargo llvm-cov`, behavior is unchanged: the inherited
`LLVM_PROFILE_FILE` wins, and profraws still land at the path the runner
chose. The `%m_%p` placeholders are expanded by the LLVM runtime in the
instrumented child; uninstrumented children ignore the env var entirely.
The CI guard from #2719 will catch any future helper that bypasses this
contract.

## Test plan

Verified against a deliberately-reinstalled instrumented `mock-stub`
each time:

- [x] 5× `cargo test --test integration --
ci_status::test_list_full_with_gitea` — 9/9 pass each run (was flaking
~50%)
- [x] 5× `cargo nextest run --test integration ci_status` (67 tests,
parallel) — all pass each run
- [x] `cargo llvm-cov nextest --test integration
ci_status::test_list_full_with_gitea_pr_status::case_1` — passes;
profraws land in `target/llvm-cov-target/…` (cargo-llvm-cov's chosen
path), not in the worktree
- [x] `cargo test --test integration -- ci_status` (full 67) — pass
- [x] `cargo test --lib testing::tests::default_llvm` — both new unit
tests green (inherited branch + temp-dir fallback)
- [x] `cargo clippy --workspace --tests --features
shell-integration-tests -- -D warnings` — clean
- [x] No stray `default_*.profraw` anywhere after the runs; redirected
profraws land in `$TMPDIR/wt-test-profraw/cov-%m_%p.profraw`

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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