From 346af4b34903ee5a6cca7519a0b913eccdd5eb52 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 17:34:23 -0800 Subject: [PATCH] CI: cargo xtask ci warm records its build units for the cache trim In the gate leg that saves the dependency cache, craft-common-actions' rust-cache action names a file in CRAFT_CACHE_UNITS, and its cache-trim action keeps only the build units that file names. xtask wrote nothing to it, so every warm leg logged "cache-trim: no unit record, so the target directory stays as it is" and the saved entry kept the old builds of every dependency still in Cargo.lock. With CRAFT_CACHE_UNITS set, each cargo build of `cargo xtask ci warm` runs with --message-format json-render-diagnostics. Cargo's JSON messages go to the record, and diagnostics stay readable on stderr. xtask records its own build first (cargo build -p xtask), since `cargo xtask` builds it before xtask can record anything. Without the variable the commands run as before. --- xtask/src/cache_units.rs | 148 +++++++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 20 ++++-- 2 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 xtask/src/cache_units.rs diff --git a/xtask/src/cache_units.rs b/xtask/src/cache_units.rs new file mode 100644 index 0000000000..a3b0a611c0 --- /dev/null +++ b/xtask/src/cache_units.rs @@ -0,0 +1,148 @@ +//! The build units a CI build used, for the dependency cache entry the gate saves. +//! +//! In a gate leg that saves the dependency cache, craft-common-actions' `rust-cache` action names a +//! file in `CRAFT_CACHE_UNITS`, and its `cache-trim` action deletes every build unit of the target +//! directory that the file does not name before the entry is saved. Cargo prints a JSON message for +//! every unit a build compiled or found fresh. The builds of `cargo xtask ci warm` append those +//! messages to the file, so the saved entry holds this build and no older builds of updated +//! dependencies. Without `CRAFT_CACHE_UNITS` the commands run as they always have. + +use std::fs::OpenOptions; +use std::io::{BufRead, BufReader, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +/// Cargo prints its messages as JSON on stdout and the diagnostics as text on stderr. +const MESSAGE_FORMAT: [&str; 2] = ["--message-format", "json-render-diagnostics"]; + +/// The record file, when this leg saves the cache. +fn file() -> Option { + std::env::var_os("CRAFT_CACHE_UNITS").filter(|v| !v.is_empty()).map(PathBuf::from) +} + +/// Runs `cmd`, a cargo build shown as `what`, which must exit successfully. When this leg saves the +/// cache, it adds `--message-format json-render-diagnostics` and appends Cargo's messages to the +/// record; every other stdout line (test output) still goes to stdout. +pub fn run(cmd: Command, what: &str) -> Result<(), String> { + match file() { + Some(file) => run_recorded(cmd, what, &file), + None => crate::run(cmd, what), + } +} + +/// [`run`] with the record in `file`. +fn run_recorded(mut cmd: Command, what: &str, file: &Path) -> Result<(), String> { + // After `--` the flag would go to rustc or the test binaries instead of cargo. + if cmd.get_args().any(|a| a == "--") { + return Err(format!("{what}: a recorded build takes no `--` arguments")); + } + cmd.args(MESSAGE_FORMAT); + eprintln!("$ {what} {}", MESSAGE_FORMAT.join(" ")); + let mut record = OpenOptions::new().create(true).append(true).open(file).map_err(|e| format!("{}: {e}", file.display()))?; + let mut child = cmd.stdout(Stdio::piped()).spawn().map_err(|e| format!("{what}: failed to spawn: {e}"))?; + let routed = match child.stdout.take() { + Some(stdout) => route(BufReader::new(stdout), &mut record, &mut std::io::stdout().lock()), + None => Err(std::io::Error::other("no stdout pipe")), + }; + let status = child.wait().map_err(|e| format!("{what}: {e}"))?; + routed.map_err(|e| format!("{what}: recording the build units in {}: {e}", file.display()))?; + if status.success() { Ok(()) } else { Err(format!("{what}: exited with {status}")) } +} + +/// Records xtask's own units. `cargo xtask` builds xtask before xtask can record anything, and the +/// trim deletes an unrecorded unit from the entry, so the next run would build xtask again. The +/// build is fresh by now, so this only prints the messages. +pub fn record_xtask() -> Result<(), String> { + if file().is_none() { + return Ok(()); + } + let mut c = crate::cargo(); + c.args(XTASK_BUILD); + run(c, &format!("cargo {}", XTASK_BUILD.join(" "))) +} + +/// The build `cargo xtask` (the `run --package xtask` alias in `.cargo/config.toml`) runs first. +const XTASK_BUILD: [&str; 3] = ["build", "-p", "xtask"]; + +/// Lines that are Cargo's JSON messages go to `record`, every other line to `out`. +fn route(mut input: impl BufRead, record: &mut impl Write, out: &mut impl Write) -> std::io::Result<()> { + let mut line = Vec::new(); + while input.read_until(b'\n', &mut line)? != 0 { + if line.starts_with(b"{\"reason\":") { + record.write_all(&line)?; + if !line.ends_with(b"\n") { + record.write_all(b"\n")?; + } + } else { + out.write_all(&line)?; + } + line.clear(); + } + record.flush()?; + out.flush() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cargo_messages_are_recorded_and_test_output_passes_through() { + let input = + "{\"reason\":\"compiler-artifact\",\"fresh\":true}\nrunning 3 tests\ntest a ... ok\n{\"reason\":\"build-finished\",\"success\":true}"; + let (mut record, mut out) = (Vec::new(), Vec::new()); + route(input.as_bytes(), &mut record, &mut out).unwrap(); + assert_eq!( + String::from_utf8(record).unwrap(), + "{\"reason\":\"compiler-artifact\",\"fresh\":true}\n{\"reason\":\"build-finished\",\"success\":true}\n" + ); + assert_eq!(String::from_utf8(out).unwrap(), "running 3 tests\ntest a ... ok\n"); + } + + #[test] + fn a_recorded_build_takes_no_separator() { + // A program that does not exist: the check comes before anything is spawned. + let mut c = Command::new("vectorcraft-no-such-cargo"); + c.args(["clippy", "--", "-D", "warnings"]); + let file = std::env::temp_dir().join(format!("xtask-cache-units-separator-{}.jsonl", std::process::id())); + let err = run_recorded(c, "cargo clippy -- -D warnings", &file).unwrap_err(); + assert!(err.contains("no `--` arguments"), "{err}"); + assert!(!file.exists()); + } + + #[test] + fn xtask_record_build_matches_the_alias() { + let config = std::fs::read_to_string(crate::root().join(".cargo/config.toml")).unwrap(); + assert!(config.lines().any(|l| l.trim() == "xtask = \"run --package xtask --\""), "{config}"); + } + + #[test] + fn a_real_build_appends_its_units_fresh_on_the_second_run() { + let dir = std::env::temp_dir().join(format!("xtask-cache-units-build-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("src")).unwrap(); + std::fs::write(dir.join("Cargo.toml"), "[package]\nname = \"cache-unit-fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\n") + .unwrap(); + std::fs::write(dir.join("src/lib.rs"), "pub fn one() -> u32 { 1 }\n").unwrap(); + let record = dir.join("units.jsonl"); + for expected_fresh in [false, true] { + let mut c = crate::cargo(); + c.current_dir(&dir) + .env("CARGO_TARGET_DIR", dir.join("target")) + .env("CARGO_BUILD_BUILD_DIR", dir.join("target")) + .env("CARGO_NET_OFFLINE", "true") + .args(["build", "--lib"]); + run_recorded(c, "cargo build --lib", &record).unwrap(); + let messages: Vec = + std::fs::read_to_string(&record).unwrap().lines().map(|line| serde_json::from_str(line).unwrap()).collect(); + let artifacts: Vec<&serde_json::Value> = + messages.iter().filter(|m| m["reason"] == "compiler-artifact" && m["target"]["name"] == "cache_unit_fixture").collect(); + // The record is appended to: the first build's message is still there after the second. + assert_eq!(artifacts.len(), if expected_fresh { 2 } else { 1 }); + assert_eq!(artifacts.last().unwrap()["fresh"].as_bool(), Some(expected_fresh)); + assert!(artifacts.iter().all(|m| !m["filenames"].as_array().unwrap().is_empty())); + assert_eq!(messages.last().unwrap()["reason"], "build-finished"); + } + std::fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index ab2e2cc6c9..91e9c1b922 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -7,6 +7,7 @@ mod affinity_corpus; mod assets; mod brands; mod bundle; +mod cache_units; mod cursors; mod ico; mod layers; @@ -41,7 +42,8 @@ commands: ci fmt --check, clippy -D warnings, test, doctests, no-bundled-fonts, pdf-deps, assets, brands, cursors (warnings only), ui-commands --check, layers, wasm (stops at first failure) ci shard shard $GATE_SHARD of $GATE_SHARDS of ci (xtask/src/shards.rs): a test shard runs the tests and doctests of its crates, the last shard the other steps - ci warm build what every shard builds, without running it (fills the gate's cache) + ci warm build what every shard builds, without running it (fills the gate's cache); with + CRAFT_CACHE_UNITS set, appends Cargo's JSON messages for each build to that file shards list the crates each shard tests; fails when a member is in no shard or in two, or builds with other features in its shard than in the whole workspace corpus [--download | --affinity | --scripts] @@ -250,7 +252,7 @@ fn cmd_wasm() -> Result<(), String> { c.args(["-p", id]); } let names: Vec<&str> = set.iter().map(|(name, _)| name.as_str()).collect(); - run(c, &format!("cargo check --target wasm32-unknown-unknown --keep-going ({} crates: {})", set.len(), names.join(", "))) + cache_units::run(c, &format!("cargo check --target wasm32-unknown-unknown --keep-going ({} crates: {})", set.len(), names.join(", "))) } /// Whether `cargo nextest` is installed. @@ -350,7 +352,11 @@ fn cmd_ci_shard() -> Result<(), String> { /// dependencies of every shard with the features that shard builds them with (cargo resolves /// features over the packages each command selects). The integration tests are left out: they /// build no dependency that the library tests do not, since both take the crate's dev-dependencies. +/// +/// In a leg that saves the cache, every build records its units in `CRAFT_CACHE_UNITS` +/// (xtask/src/cache_units.rs), xtask's own build included. fn cmd_ci_warm() -> Result<(), String> { + cache_units::record_xtask()?; let checked = shards::run()?; let mut steps: Vec = Vec::new(); for shard in checked { @@ -360,7 +366,7 @@ fn cmd_ci_warm() -> Result<(), String> { Box::new(move || { let mut c = cargo(); c.arg("test").args(shards::select(&crates)).args(["--lib", "--bins", "--no-run"]); - run(c, &format!("cargo test --lib --bins --no-run ({})", shards::names(&crates))) + cache_units::run(c, &format!("cargo test --lib --bins --no-run ({})", shards::names(&crates))) }), )); } @@ -370,7 +376,7 @@ fn cmd_ci_warm() -> Result<(), String> { Box::new(|| { let mut c = cargo(); c.args(["clippy", "--workspace", "--all-targets", "--keep-going"]); - run(c, "cargo clippy --workspace --all-targets --keep-going") + cache_units::run(c, "cargo clippy --workspace --all-targets --keep-going") }), )); steps.push(( @@ -378,13 +384,13 @@ fn cmd_ci_warm() -> Result<(), String> { Box::new(|| { let mut c = cargo(); c.args(["test", "-p", "vectorcraft-text", "--no-default-features", "--test", "without_bundled_fonts", "--no-run"]); - run(c, "cargo test -p vectorcraft-text --no-default-features --test without_bundled_fonts --no-run")?; + cache_units::run(c, "cargo test -p vectorcraft-text --no-default-features --test without_bundled_fonts --no-run")?; let mut c = cargo(); c.args(["clippy", "-p", "vectorcraft-ui-egui", "--no-default-features", "--all-targets"]); - run(c, "cargo clippy -p vectorcraft-ui-egui --no-default-features --all-targets")?; + cache_units::run(c, "cargo clippy -p vectorcraft-ui-egui --no-default-features --all-targets")?; let mut c = cargo(); c.args(["test", "-p", "vectorcraft-ui-egui", "--no-default-features", "--test", "install_fonts", "--no-run"]); - run(c, "cargo test -p vectorcraft-ui-egui --no-default-features --test install_fonts --no-run") + cache_units::run(c, "cargo test -p vectorcraft-ui-egui --no-default-features --test install_fonts --no-run") }), )); steps.push(("wasm", Box::new(cmd_wasm)));