d3d12-shader: read CARGO_MANIFEST_DIR at runtime in build.rs #2882
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2882"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T14:06:39Z.
Claim:
D3D12-SHADER-BUILD-MANIFEST-DIR(https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7313637483)Fixes
@Neutron-0's finding ind3d12/crates/leonida-shader/build.rs.Root cause
build.rsreadenv!("CARGO_MANIFEST_DIR")at build-script compile time and silently returned onstd::fs::read_dir/std::fs::readerrors. Whenever Cargo reruns a cachedbuild.rsexecutable against a different or moved/removed checkout (for example, a modified source file in another worktree sharingCARGO_TARGET_DIR, or a relocated checkout), it hashed the stale baked path -- or silently emitted the empty seed hash (cbf29ce48422232584222325cbf29ce4) and emptyLEONIDA_SHADER_SWITCHESif that original directory was gone.Note on scope: sharing a single
CARGO_TARGET_DIRacross two identical checkouts at the same commit still produces a Cargo no-op (Cargo does not rerun the build script at all), so sharingCARGO_TARGET_DIRacross checkouts remains unsupported in general. This change guarantees correct hashing and fail-loud behavior whenever the build script runs.Changes
d3d12/crates/leonida-shader/build.rs:CARGO_MANIFEST_DIRat runtime viastd::env::var_os("CARGO_MANIFEST_DIR")instead of compile-timeenv!.cargo:rerun-if-changed=build.rs.panic!) onread_dir/readerrors or an empty non-test source list instead of silently emitting an empty fingerprint.d3d12/crates/leonida-shader/src/source_hash_tests.rs:build_script_uses_runtime_manifest_dir_and_rejects_missing_src, which compilesbuild.rsonce and drives the compiled binary directly with distinct temporaryCARGO_MANIFEST_DIRtrees (verifying distinct hashes and switches) and a missing directory (verifying non-zero failure) to validate runtime path resolution and fail-loud behavior whenever the script runs.Verification
build.rs):build_script_uses_runtime_manifest_dir_and_rejects_missing_srcfailed (0 passed; 1 failed).cargo test --locked -p leonida-shader --lib source_hash_tests -- --nocapture: 4 passed, 0 failed.cargo check --locked -p leonida-shader --all-targets: passed.leonida-shader --libon this machine hits the unrelatedPSV0DXIL validator failures tracked in #2864.)GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)
AI-assisted: Antigravity
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T14:24:15Z.
Review: COMMENTED
Direction is right, and the fail-loud paths are a real improvement: with the original checkout gone and the build script forced to rerun, base emits the empty
cbf29ce4…hash and emptyLEONIDA_SHADER_SWITCHES, while this branch emits the correct hash.rerun-if-changedgains exactlybuild.rs, which is right because the script now affects the hash, and I confirmed the rerun behaviour: build → no-op → touchsrc/lib.rs→ rebuild → no-op. It also builds correctly from the repo root, from an unrelated cwd, with a customCARGO_TARGET_DIR, and as a path dependency (same hash as in-tree).One overclaim, though: with two checkouts at the same commit sharing a
CARGO_TARGET_DIR, the second is still a 0.04 s no-op and ships the first checkout's hash — before and after this change — because Cargo never runs the script at all, and a runtime read cannot help that. What is fixed is the case where the script does rerun against a stale baked path. The new test drives the compiled binary directly with a forcedCARGO_MANIFEST_DIR, so it validates the mechanism but not the Cargo-level reuse described in the body.Please narrow the claim to "correct whenever the build script reruns", or state that sharing a target dir across checkouts remains unsupported (the build dir is literally shared —
leonida-shader-8d2ca1f295c0ab1efor both).Two mutations worth recording, both of which the change now catches: a cwd-relative base passes an in-tree
cargo build(Cargo runs the script with cwd = package root) and is only caught by the test, while a wrong base now panics loudly where base silently emitted the seed hash. That is the right kind of failure.Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T14:27:46Z.
I exercised the real Cargo reuse path on Linux with Rust 1.98.1, at
bfa275eamerged ontob3910e5e. I used two worktrees per arm and a target directory shared by those two worktrees. Only B had a newLEONIDA_REVIEW_CHECKOUT_Bstring in a non-test source. Cargo compiled the build program once in A and reran that same executable without recompiling it in B, in both arms.The experiment specifically exercises the case where B's edited source makes Cargo rerun the cached build program. It agrees with the distinction in the previous review about an identical checkout that produces a Cargo no-op. No game files or GPU dependency. MEASURED, AI-assisted (Codex).
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T14:48:44Z.
@KelboLive Thanks for the careful review! I have updated the PR description to narrow the claim to "correct whenever the build script reruns" and explicitly note that identical-commit checkouts sharing a
CARGO_TARGET_DIRstill produce a Cargo no-op (so sharingCARGO_TARGET_DIRacross checkouts remains unsupported in general). Also clarified that the new test drives the compiledbuild.rsbinary directly.@merzleckeier Thanks for exercising the two-worktree Cargo rerun path with a modified source file!
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:46:00Z.
VERIFY-2882 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files
Verdict: PASS. The fix closes a hole where the build script would hash nothing and say so with exit code 0. I measured the wrong hash and the loud failure side by side.
Head
bfa275ea, base the currentmain88a9f133, merge base3e7b34a5, 1 commit, 2 files, +84/−7:d3d12/crates/leonida-shader/build.rsand the newd3d12/crates/leonida-shader/src/source_hash_tests.rs.1. What was wrong
The build script hashes the translator's own sources so the shader cache can tell whether a cached translation is still current. It found them like this:
Two problems:
env!("CARGO_MANIFEST_DIR")is expanded when build.rs is compiled, so the path is baked into the build-script binary. The pull request reads it when the script runs:std::env::var_os("CARGO_MANIFEST_DIR").2. What that cost, measured
I compiled
main's build script with the path baked in, deleted the directory it pointed at, and ran it:cbf29ce48422232584222325cbf29ce4is the FNV-1a offset basis — the hash of the empty set. Every shader would then get the same cache key, so the shader cache could hand back the wrong translation, and nothing would say so. The same run of the pull request's build script:The pull request also adds
cargo:rerun-if-changed=build.rsandassert!(!list.is_empty(), "no translator sources found in …"), so the two ways of coming up with nothing are now both loud.3. The behaviour difference, measured
With the pull request's build script,
CARGO_MANIFEST_DIRdecides what is hashed. Withmain's, the baked path does and the environment variable is ignored — the pull request's own test shows it, and I ran it:That is,
checkout_a's own file was never read; the crate it was compiled against was hashed instead.4. The new test
build_script_uses_runtime_manifest_dir_and_rejects_missing_srcdoes not only read the script: it compiles build.rs withrustcand runs it:and then requires that
checkout_aandcheckout_bgive different output, that each names its own switch, and that the missing directory fails rather than emitting an empty hash.5. Red and green, and the discrimination
cargo test -p leonida-shaderon the pull request's branch: 247 passed, 0 failed, 2 ignored.With only
build.rsput back tomain's (the pull request's tests kept):That is the test's first assertion, a check of the script's text. To see the behaviour rather than the text, I removed that one assertion and ran again with
main's build.rs:So the behavioural half of the test also fails against
main's script, for the reason §3 describes.6. A small note on the test's first assertion
The text check (
!source.contains("env!(\"CARGO_MANIFEST_DIR\")")) is the brittle kind: it would also fire on a spelling that is not actually wrong, and it does not fire on a wrong spelling that is notenv!. The behavioural part of the same test is the one that matters, and it is there; I would not ask for the text check to be dropped, but it is worth knowing which half carries the weight.7. What I did not check
rustc 1.98.1). The build script's behaviour is the same on both; the test compiles it withrustc --edition 2024and runs the binary, and on Windows it would name the probebuild_probe.exe.leonida-gpu,leonida-hleandleonida-loaderdo not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ranleonida-shaderon its own.cargo:rustc-envvalues the build script emits beyond the source hash and the switches list.cargo clippy(already failing onmain, run as a report by the workflow) andcargo fmt(left out of the d3d12 workflow).AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by Neutron-0 on 2026-12-01T16:19:29Z.
Verified on Windows 11, Rust 1.98.1, no GPU and no game files.
The head's own test passes.
cargo test -p leonida-shader --lib source_hash_testsonbfa275eaa: 4 passed, 0 failed (the three tests that were there before, plusbuild_script_uses_runtime_manifest_dir_and_rejects_missing_src).The hazard is real, and here is exactly when it bites. The build script is reused when the second checkout's files are older than the fingerprint Cargo recorded - a fresh checkout's files are newer, so Cargo recompiles and the fingerprint is right. Measured with two worktrees of different trees (
origin/mainand the PSV0 branch) sharing oneCARGO_TARGET_DIR:e2b9d9a2fc8a22526a3a3019fe3d1927for the second tree).when-a) - and the fingerprint stays the first tree's (269bc6c85de256e6ec282019707158db) while a different tree is being built.So the failure is not "sharing a target directory" in general, but sharing one across checkouts whose files are older than the recorded fingerprint, or after a checkout was deleted and rebuilt in place. The fix removes the whole class: the path is read at run time, so the script hashes the tree that is there, and a missing tree fails the build instead of hashing nothing.
Two notes, neither blocking.
build.rswithrustc --edition 2024directly. The crate's own edition is whatCargo.tomlsays; if the repository ever moves off edition 2024 this probe would still compile the file as 2024 while Cargo would compile it as the crate's edition. Pinning the edition to the crate's (include_str!("../Cargo.toml")parsed, or a fixed constant checked against it) would keep the probe honest. Today both are 2024, so the test measures the right thing.cargo:rerun-if-changed=build.rsis a good addition on its own: without it, a change to the build script alone is not a reason to rerun it.The fix is right, and the test is the right shape: it drives the compiled script with two different
CARGO_MANIFEST_DIRtrees and with a missing one, so it fails on the oldenv!version and passes on this one.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T22:35:32Z.
Merging (added to the merge queue). Head
bfa275eaamerges cleanly with mainc4ca57615. Claim: D3D12-SHADER-BUILD-MANIFEST-DIR (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7313637483). Red/green: imShangLi (Linux, PASS, wrong hash vs loud failure measured), Neutron-0 (Windows 11, 4/0 on the head, hazard reproduced on main). I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto mainc4ca57615: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.