Skip to content

Add TRYBUILD_NO_CFG to opt out of cfg injection and share parent target dir#329

Closed
TarunvirBains wants to merge 1 commit into
dtolnay:masterfrom
TarunvirBains:feat-opt-out-cfg-and-target-dir
Closed

Add TRYBUILD_NO_CFG to opt out of cfg injection and share parent target dir#329
TarunvirBains wants to merge 1 commit into
dtolnay:masterfrom
TarunvirBains:feat-opt-out-cfg-and-target-dir

Conversation

@TarunvirBains
Copy link
Copy Markdown

@TarunvirBains TarunvirBains commented May 10, 2026

Adds an opt-in TRYBUILD_NO_CFG env var. When set:

  • rustflags::toml omits --cfg trybuild from the rustflags passed to spawned cargo.
  • cargo::cargo_target_dir returns no CARGO_TARGET_DIR override, so the parent's value (or absence) flows through.

Both halves are needed together. The cfg perturbs the rustc fingerprint, and the target-dir override means cargo never gets the chance to compare fingerprints anyway — either alone leaves the spawned cargo recompiling from cold.

When unset, behavior is unchanged. Filed as #328.

Verified end-to-end with a smoke harness:

$ cargo clean && cargo test
$ ls target/tests/trybuild/
debug/  trybuild-smoke/  x86_64-unknown-linux-gnu/

$ cargo clean && TRYBUILD_NO_CFG=1 cargo test
$ ls target/tests/trybuild/
trybuild-smoke/
$ ls target/debug/incremental/
trybuild_smoke-...

Baseline: spawned cargo writes artifacts under target/tests/trybuild/{debug,x86_64-...}. With TRYBUILD_NO_CFG=1, those subdirs are absent and the spawned cargo's incremental output lands under target/debug/ instead.

Trybuild's existing tests pass (26/26) with no test changes — they're normalization fixtures that don't spawn cargo. I considered adding a unit test that asserts rustflags::toml() returns different values based on the env var, but env-mutation tests are racy without serialization; let me know if you'd prefer it added and which approach you'd want.

Related: #305 (narrow rustflags carve-out for -C instrument-coverage), #106 / #283 (same friction from different angles).

When TRYBUILD_NO_CFG is set, omit the --cfg trybuild rustflag and
don't override CARGO_TARGET_DIR. The spawned cargo's incremental
cache hits the parent workspace's existing artifacts, which
substantially shortens cold-cache builds for crates with heavy
dependency trees.

Default behavior unchanged when the variable is unset.
@TarunvirBains TarunvirBains force-pushed the feat-opt-out-cfg-and-target-dir branch from fefbdcf to 62e3aa8 Compare May 10, 2026 16:11
@TarunvirBains
Copy link
Copy Markdown
Author

Closing in favor of a different design direction.

The shared-target-dir approach in this PR works for cache reuse but introduces build-lock contention between the parent workspace and trybuild's spawned cargo. A cleaner approach is to keep trybuild's target dir isolated and seed it from the parent's artifacts (reflink / hardlink / copy fallback), preserving trybuild's isolation invariant while still getting the cache reuse.

Will follow up with a new PR. Issue #328 stays open as the problem statement.

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.

1 participant