diff --git a/quest/m2/README.md b/quest/m2/README.md index ef0c81701..a752fb4ec 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -26,7 +26,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README ## Required -- [Rebuild a target directory when its std changes](/quest/m2/ts-sysroot-fingerprint.md) - a new std sysroot rebuilds what depends on std, so no E0460 until a clean - [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-sysroot-fingerprint.md b/quest/m2/ts-sysroot-fingerprint.md deleted file mode 100644 index 4aa63b723..000000000 --- a/quest/m2/ts-sysroot-fingerprint.md +++ /dev/null @@ -1,33 +0,0 @@ -# [S] Rebuild a target directory when its std changes - -## Goal - -A target directory that [rust2-ts](/ts/README.md) translated against one std sysroot -rebuilds what depends on std once `RUST2_TS_SYSROOT` names another, so no change to -`ts/src/profile.toml` or the flake fails with rustc's E0460 until the directory is cleaned. - -## Plan - -Found by [#237](https://github.com/moq-dev/rust2/pull/237) on 2026-10-29: changing only -`profile.toml`'s core, alloc, and std lines on the same nightly gives a new sysroot, but -cached crates stay built against the old one. - -- The likely root cause: the wrapper adds `--sysroot` itself, so nothing cargo fingerprints - names the sysroot. The shared flags rebuild everything only because they are in the - profile, which cargo hashes. Confirm with a reproduction before fixing. -- Fix it where cargo looks: the sysroot's identity (its store path, or a hash of its - contents) in something every unit's fingerprint and `-C metadata` cover, so two - sysroots keep separate artifacts rather than evicting each other. The profile's - rustflags are the obvious place, as for the shared flags. -- Pitfalls: cargo checks dep-info env dependencies only for path packages, so declaring - `RUST2_TS_SYSROOT` that way misses registry crates; `RUSTFLAGS` overrides - `target..rustflags` (why #237 dropped it); build scripts and proc macros must keep - the host std; and cargo caches `rustc -vV` in `target/.rustc_info.json` by paths and - mtimes, not by our environment. -- Test it the way it failed: one target directory, two sysroots that differ only in std's - flags, no clean between. - -m2 because it breaks the translator's own fixtures and moq's `just ts` runs whenever a -rust2 bump changes only std's flags, but loudly, and a clean recovers. - -Public API: none. diff --git a/ts/src/cli.rs b/ts/src/cli.rs index c73eff048..a18af2b22 100644 --- a/ts/src/cli.rs +++ b/ts/src/cli.rs @@ -41,12 +41,16 @@ fn try_run(args: &[String]) -> Result { }; let sysroot = sysroot()?; // `cargo clean` reads the profiles too. The sysroot is absolute for the wrapper, which - // cargo runs elsewhere, and empty for none. + // cargo runs elsewhere, and empty for none. The wrapper's version names it, so cargo + // must not cache that version by the executables alone, and must hash all of it into + // `-C metadata`, which on a nightly it does only with `-Zseparate-nightlies`. let env = [ (config::env::CONFIG, args.config.as_os_str()), output, ("CARGO_UNSTABLE_PROFILE_RUSTFLAGS", "true".as_ref()), (config::env::SYSROOT, sysroot.as_deref().unwrap_or("").as_ref()), + ("CARGO_CACHE_RUSTC_INFO", "0".as_ref()), + ("CARGO_UNSTABLE_SEPARATE_NIGHTLIES", "true".as_ref()), ]; let mut cargo = profile_args(sysroot.is_none()); // The profile `PROFILE` sets, which cargo refuses to combine with one the cargo args diff --git a/ts/src/driver.rs b/ts/src/driver.rs index 12952baa4..5d34563e8 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -14,9 +14,18 @@ use crate::{cli, lower, program}; /// Run as rustc, with `args` as cargo passed them: the wrapped rustc's path first. pub fn run(args: Vec) -> ExitCode { rust2_driver::rustc::run("rust2-ts", args, |args| { + let sysroot = cli::sysroot()?; + // Cargo's query of the compiler, whose answer it hashes into every unit's fingerprint + // and `-C metadata`: the sysroot is part of what compiles them, so a target + // directory keeps what it checks against each sysroot apart. + if args[1..] == ["-vV"] + && let Some(sysroot) = &sysroot + { + println!("rust2-ts-sysroot: {sysroot}"); + } // The target's crates only: build scripts and proc macros run on the host, with its std. if args.windows(2).any(|w| w[0] == "--target" && w[1] == cli::TARGET) - && let Some(sysroot) = cli::sysroot()? + && let Some(sysroot) = sysroot { args.extend(["--sysroot".into(), sysroot]); } diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 0e0e0fd3e..e0848f61b 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -570,6 +570,55 @@ fn census() { ); } +/// One target directory keeps what it checked against each sysroot apart: a crate +/// checked against another std is never loaded. Switching sysroots checks a registry +/// dependency again, and switching back checks none. The other sysroot is the same std by +/// another path, which is all that tells sysroots apart. +#[test] +fn sysroot() { + let Some(std) = std::env::var("RUST2_TS_SYSROOT").ok().filter(|dir| !dir.is_empty()) else { + // Each target directory checks its own std instead. + return; + }; + let name = Path::new(&std).file_name().unwrap().to_str().unwrap(); + let other = format!("{std}/../{name}"); + let dir = scratch().join("sysroot"); + let _ = std::fs::remove_dir_all(&dir); + let checked = |sysroot: &str| { + let output = Command::new(env!("CARGO_BIN_EXE_rust2-ts")) + .arg("--config") + .arg(fixture("reject").join("census.toml")) + .arg("--census") + .arg(dir.join("census.md")) + .env("CARGO_TARGET_DIR", dir.join("build")) + .env("RUST2_TS_SYSROOT", sysroot) + .output() + .expect("running rust2-ts"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.success(), + "the census against {sysroot} failed:\n{stderr}" + ); + stderr + .lines() + .filter_map(|line| line.trim_start().strip_prefix("Checking ")) + .map(|line| line.split(' ').next().unwrap().to_string()) + .collect::>() + }; + let all = checked(&std); + assert!(all.iter().any(|c| c == "bytes"), "bytes was not checked: {all:?}"); + assert_eq!( + checked(&other), + all, + "switching to {other} kept a crate checked against {std}" + ); + assert_eq!( + checked(&std), + ["reject-fixture"], + "switching back to {std} checked its crates again" + ); +} + /// Selects the fixture's own getrandom backend, which `rand::rng()` seeds from natively: /// wasm32 has no OS to read. Only getrandom's flags change, so nothing else rebuilds. const GETRANDOM: &str = r#"profile.dev.package.getrandom.rustflags=["--cfg", 'getrandom_backend="custom"']"#;