Skip to content

fix(testing): default LLVM_PROFILE_FILE to a temp-dir path when not inherited - #2730

Merged
max-sixty merged 3 commits into
mainfrom
gitea-test-flake
May 12, 2026
Merged

max-sixty merged 3 commits into
mainfrom
gitea-test-flake

Conversation

@max-sixty

@max-sixty max-sixty commented May 12, 2026 •

Copy link
Copy Markdown
Owner

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:

  • 5× cargo test --test integration -- ci_status::test_list_full_with_gitea — 9/9 pass each run (was flaking ~50%)
  • 5× cargo nextest run --test integration ci_status (67 tests, parallel) — all pass each run
  • 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
  • cargo test --test integration -- ci_status (full 67) — pass
  • cargo test --lib testing::tests::default_llvm — both new unit tests green (inherited branch + temp-dir fallback)
  • cargo clippy --workspace --tests --features shell-integration-tests -- -D warnings — clean
  • No stray default_*.profraw anywhere after the runs; redirected profraws land in $TMPDIR/wt-test-profraw/cov-%m_%p.profraw

…nherited

`isolate_subprocess_env` (and the two PTY mirror sites) only propagated
`LLVM_PROFILE_FILE` when the parent had it set — which left it unset
under plain `cargo test`. An instrumented child (e.g. a stale `mock-stub`
left instrumented by an earlier coverage build) then fell back to
writing `default_<hash>_<pid>.profraw` into its cwd. For `wt list`
snapshot tests that's the test worktree, which surfaces as untracked
files in `git status --porcelain` and flakes `wt list` to
"Showing 5 worktrees, 1 with changes, …".

Add `default_llvm_profile_file()` that returns the inherited value when
running under `cargo llvm-cov` and otherwise a per-binary, per-pid path
under `std::env::temp_dir()/wt-test-profraw/cov-%m_%p.profraw`. The
three call sites now always set `LLVM_PROFILE_FILE` via this helper, so
an instrumented child can never fall back to its cwd. Under
`cargo llvm-cov`, behavior is unchanged: profraws still land at the
path the runner expects.

The CI guard from #2719 will catch any future helper that bypasses this
contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <[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.

The fix is well-scoped — default_llvm_profile_file() cleanly captures the inherit-or-temp-dir contract, and the three call sites all route through it consistently. One inline note on now-stale companion documentation.

Comment thread src/testing/mod.rs Resolved

@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.

codecov/patch is failing on the new function. The uncovered lines are 326-329 in src/testing/mod.rs — the temp-dir fallback branch of default_llvm_profile_file(). Under cargo llvm-cov (CI's coverage runner), LLVM_PROFILE_FILE is always inherited, so the function always returns at line 325 and the fallback never executes — yet the fallback is the entire reason for this PR. Inline suggestion below for a small refactor that mirrors the existing isolate_subprocess_env_from pattern and unblocks unit-testing the fallback without touching process env.

Comment thread src/testing/mod.rs Resolved
Address review feedback on #2730:

- Split `default_llvm_profile_file` into a thin outer wrapper + an inner
  `default_llvm_profile_file_with(inherited)` that takes the inherited
  value as a parameter. Same pattern as `isolate_subprocess_env` /
  `isolate_subprocess_env_from`. Lets a unit test exercise the temp-dir
  fallback without mutating process env, which fixes codecov/patch:
  CI always runs under `cargo llvm-cov`, so `LLVM_PROFILE_FILE` is
  always inherited and the production caller never executed the
  fallback branch.
- Shrink `COVERAGE_ENV_VARS` to `[CARGO_LLVM_COV, CARGO_LLVM_COV_TARGET_DIR]`
  (LLVM_PROFILE_FILE is now governed by `default_llvm_profile_file()`),
  route all three call sites through the constant, and update the
  `isolate_subprocess_env` docstring to match the new split.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 12, 2026

@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.

codecov/patch is failing on line 2940 — the expected_dir.display() argument inside assert!. That arg only evaluates when the assertion fires, so a passing test can never cover it. Inline suggestion below drops the format args and relies on the panic context (the test name names the failure, and expected_dir is deterministic per-machine), which clears the gap.

Comment thread src/testing/mod.rs Resolved
@worktrunk-bot
worktrunk-bot dismissed their stale review May 12, 2026 01:38

codecov/patch failed — uncovered line is an assertion format arg; small inline suggestion posted

`assert!(cond, "…", expected_dir.display())` leaves the format-arg
expression uncovered — it only evaluates when the assertion fires, so a
passing test can't reach it (codecov/patch flagged the line). The test
name plus the deterministic `expected_dir` carry enough context; drop to
a bare `assert!(expected_dir.is_dir())`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

test (macos) failed on integration_tests::switch_picker::test_switch_picker_emits_cd_directive_by_default — Timed out after 30s waiting for 1 visible list row(s); saw 2. This is a known switch_picker PTY flake unrelated to the LLVM_PROFILE_FILE change (the most recent main commit, #2729, addressed another facet of the same test's stability). Triggered a rerun of the failed job.

@max-sixty
max-sixty merged commit bd4c045 into main May 12, 2026
45 of 46 checks passed
@max-sixty
max-sixty deleted the gitea-test-flake branch May 12, 2026 02:01
max-sixty added a commit that referenced this pull request May 12, 2026
)

## Summary

`cargo test --test integration` and `cargo llvm-cov --test integration`
don't build the `mock-stub` / `wt-perf` helper bins: a target filter
overrides `workspace.default-members`, and these invocations don't honor
`.config/nextest.toml`'s `build-bins` setup-script. So on a clean
`target/` (and always under `cargo llvm-cov`, which uses its own target
dir) the binary `tests/helpers/mock-stub` is absent. Today that surfaces
as an opaque ENOENT on a dangling symlink, or — for the gitea CI-status
tests — a silently-dropped `●` indicator when `tool_available("tea")`
returns false because the mocked `tea` can't spawn.

`mock_stub_binary()` now resolves the path and hands it to
`assert_mock_stub_present()`, which panics with a message pointing at
the fix (`cargo build -p mock-stub`, or `cargo nextest run` / `cargo
llvm-cov nextest`, which build the helpers via the setup-script). The
check is split into its own function so a `#[should_panic]` unit test
can drive the missing-binary arm without depending on whether the real
binary happens to be built. The assert only fires when a test actually
sets up a mock — it's reached via `MockConfig::write` — so mock-free
tests are untouched.

Also a one-line note in CLAUDE.md's "faster iteration" block, and the
`mock_stub_binary` doc comment now points at the eventual collapse —
folding the helper bins back into the worktrunk crate so `cargo test
--test integration` builds them automatically and this whole layer goes
away — which is blocked on cargo-dist (see `.config/nextest.toml` and
`tests/helpers/mock-stub/Cargo.toml`).

This is the symptom-2 follow-up to #2730 (#2730 fixed the profraw-leak
flake; this makes the missing-binary case fail loudly instead of
mysteriously).

## Test plan

- [x] `cargo test --test integration --
ci_status::test_list_full_with_gitea_no_ci` — passes (normal case
unaffected; binary present)
- [x] Same command with `target/debug/mock-stub` moved away — panics
with the message above, rendered cleanly
- [x] `cargo test --lib testing::` — 9/9 pass, including the new
`assert_mock_stub_present_panics_when_absent` (`#[should_panic]`)
- [x] `cargo clippy --tests --features shell-integration-tests -- -D
warnings` — clean

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request May 15, 2026
Snapshots regenerated in this branch leaked
`LLVM_PROFILE_FILE: /var/folders/.../wt-test-profraw/cov-%m_%p.profraw`
into the env block — `isolate_subprocess_env` (#2730) sets the var on
every test subprocess, and `insta_cmd` records it verbatim with a
platform-specific path. The intended scrub at
`setup_snapshot_settings` was never effective: `add_filter`
substitutes only on captured snapshot content, not the YAML info/env
block where env entries live, so the regex wasn't matching anything.

Switch to `add_redaction(".env.LLVM_PROFILE_FILE", ...)` (and the same
for `CARGO_LLVM_COV*`) inside `add_standard_env_redactions`, which is
already bound by the `repo` rstest fixture for every test that uses
it. Drop the obsolete `add_filter` lines and regenerate the six
affected snapshots — values now read as stable `[LLVM_PROFILE_FILE]`
placeholders matching the convention used for HOME, GIT_CONFIG_GLOBAL,
etc.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request May 15, 2026
…usline (#2764)

`wt list --full` and `wt statusline` now count untracked-file lines in
the `HEAD±` working-diff segment, matching what `wt step diff` shows.
Default `wt list` and the picker stay on the cheap `git diff --shortstat
HEAD` path (tracked-only). Putting the untracked-inclusive variant
behind the same `--full` boundary as `BranchDiff` / `CiStatus` /
`SummaryGenerate` means the explanation stays simple — "`--full`
includes untracked too" — with no new exception class.

## Mechanism

`WorkingTree::working_tree_diff_stats_with_untracked()` registers
untracked files in a temporary copy of the index via `git add
--intent-to-add .`, then runs `git diff --shortstat HEAD` against that
temp index. Same trick `wt step diff` uses; the worktree's real index is
never touched (a unit test diffs the index bytes before/after to lock
that contract in).

`WorkingTreeDiffTask` branches on the new flag and on the cached
porcelain — when no untracked entries exist, the expensive path is
skipped (the two paths are bit-equivalent for tracked-only worktrees).

## Refactor

A new `TempIndex` helper consolidates the "copy index + stage stuff into
it + run a follow-up git command" pattern that was previously open-coded
in three places (`wt step diff`,
`WorkingTreeConflictsTask::write_tree_with_working_tree`, and the new
method above). `TempIndex::git(args)` returns a `Cmd` already wired with
`current_dir`, log context, and `GIT_INDEX_FILE`, so each caller
collapses to a few lines.

## Wiring

A new `include_untracked_in_working_diff: bool` on `CollectOptions`
flows through `TaskContext` to `WorkingTreeDiffTask`. Set from
`show_full` in `collect/mod.rs`, hard-coded `true` in `statusline.rs`,
hard-coded `false` for the picker (the only other `ShowConfig::Resolved`
consumer, which intentionally runs the same fast bucket as default `wt
list`).

## Snapshot env scrub fix (second commit)

A pre-existing latent bug surfaced when regenerating the six `--full` /
statusline snapshots: `LLVM_PROFILE_FILE` (set on every test subprocess
by `isolate_subprocess_env` since #2730) leaked into the env block as a
platform-specific tempdir path. The intended scrub at
`setup_snapshot_settings` was using `add_filter`, which only substitutes
on captured snapshot content — never on the YAML info/env block — so it
was dead code. Replaced with `add_redaction(".env.LLVM_PROFILE_FILE",
...)` (and the same for `CARGO_LLVM_COV*`) inside
`add_standard_env_redactions`, which is already bound by the `repo`
rstest fixture; values now render as stable `[LLVM_PROFILE_FILE]`
placeholders matching the existing convention for HOME /
GIT_CONFIG_GLOBAL / etc.

## Tests

3695 pass: one new unit test for
`working_tree_diff_stats_with_untracked` (verifies untracked lines
counted, real index byte-identical), plus snapshot updates for nine
`--full` / statusline tests where untracked files now contribute to
`HEAD±`.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@worktrunk-bot worktrunk-bot mentioned this pull request May 16, 2026
max-sixty added a commit that referenced this pull request Aug 3, 2026
`cargo test --test integration` neither built nor rebuilt `mock-stub`: a
target filter deselects the dummy test that pulled it in, so a fresh
tree panicked ("mock-stub binary not found") and a warm one could run a
stale stub. The chain that compensated — the separate helper package,
its dummy `builds.rs`, the `default-members` entry, nextest's
experimental `build-bins` setup script, `workspace_bin()` — existed only
because cargo-dist ships every `[[bin]]`, and carried a TODO to collapse
once that changed. dist 0.30.2 does support per-binary exclusion now
(`[dist.binaries]`, since 0.29.0; verified with `dist plan`), but the
TODO's plan has a hole it predates: `cargo install worktrunk` installs
every feature-satisfied `[[bin]]`, and dist config doesn't govern
crates.io installs.

So the mock commands are now the `wt` binary itself. `mock_commands`
links `wt` under the mock's name (`gh`, `glab`, …), and `main()`
dispatches to the ported playback (`testing::mock_stub`) when
`WORKTRUNK_TEST_MOCK_CONFIG_DIR` is set, argv[0] is a foreign name, and
the config dir holds `<argv0>.json` for it. The existence check keeps wt
under a foreign argv[0] *without* a config being wt — the
argv0-validation security test symlinks it as `wt;touch` and must reach
wt's own rejection. The shipped binary already compiles the whole
`testing` module unconditionally, so this adds no new category of test
code to it. Windows links `wt.exe` with `hard_link` (copy fallback for
cross-drive dirs); the debug binary is ~67 MB, so per-mock copies stay
the fallback.

Every runner is now safe by construction — cargo rebuilds a package's
own binaries whenever its integration tests build, so there is no
separate artifact to go missing or stale. Deleted: the helper package,
the setup script plus `experimental = ["setup-scripts"]`, the
`default-members` trick, `workspace_bin()`, and `wt_bin()`'s dead
compile-time branch (unit-test targets get neither the runtime variable
nor the `option_env!` value, so the runtime resolution is the one
mechanism).

Validated locally: the pre-merge gate's full suite passes (4542/4542;
its doc step also caught unescaped `argv[0]` intra-doc links in the new
comments, fixed and `cargo doc -Dwarnings` re-verified). With all stub
artifacts purged from `target/`, plain `cargo test --test integration`
on mock-dependent tests builds them and passes — the command that used
to hit the trap. Two integration tests pin the dispatch's argv[0] edges
(an empty argv[0], and a non-UTF8 one), alongside the existing
`wt;touch` carve-out test.

The first commit is the investigation that preceded the fix: it verified
the `wt` binary itself was never subject to the staleness the mock-stub
was, and documented that in `tests/CLAUDE.md`; the fix then narrows that
paragraph further, since the gap it scoped no longer exists.

A two-reviewer subagent round (one prosecuting the diff against the
failure modes documented in the repo's own mock history — the #401/#407
Windows era, #547, #654, #127, #2544, #2730, #2744 — the other
adversarial) then hardened the dispatch. The reserved-name guard is
case-insensitive, matching the config probe, which goes through a
filesystem that equates `WT.json` with `wt.json` on macOS and Windows;
`command_name()` reads `args_os` — `env::args()` panics on a non-Unicode
argument, and this runs inside `main()` on every invocation (caught by
the tend review) — and returns `None` for a degenerate argv[0] instead
of panicking; the `.exe` suffix is stripped explicitly rather than via
`file_stem`, so a dotted mock name (`python3.11`) resolves identically
on every platform; and `copy_mock_binary` is now private —
`MockConfig::write` writes `<name>.json` before linking and is the only
way to create a mock, so a link cannot exist without its config, and the
dispatch's missing-config fall-through can only mean "wt under a foreign
name" (the argv0-validation tests' `wt;touch`), never a half-configured
mock that silently runs real wt with the mocked tool's arguments. The
`Option<&str>` mock helpers whose `None` arm produced exactly such
configless links lost the arm (every caller passed `Some`), and 25
redundant standalone link calls went with it.

The review also surfaced the one remaining spawn-a-stale-binary path
outside the suite: `wt-perf timeline` resolved a sibling `wt` by path,
checked only existence, and told the user to build it manually — so
`cargo run -p wt-perf -- timeline` after a `src/` edit silently measured
stale code. It now builds `wt` first and takes the artifact path from
cargo's `--message-format=json` report rather than deriving a sibling
location, so target-dir and profile overrides can't divert the build
away from where it's resolved; a release wt-perf builds and measures a
release wt. The build runs before the timeline's wall-clock measurement
starts, cargo's progress streams on stderr, and stdout keeps the
`--chrome` JSON contract.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Fable 5 <[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