diff --git a/docs/src/content/docs/modules.md b/docs/src/content/docs/modules.md index 137a060..5ceb3c3 100644 --- a/docs/src/content/docs/modules.md +++ b/docs/src/content/docs/modules.md @@ -71,9 +71,8 @@ A module changes what a job runs, so everything about it goes into the job's has - its parameters, after defaults and detection (the toolchain it found in `rust-toolchain.toml`, say); - the steps it adds, for every provider the config names; -- the files it depends on. The `rust` module adds `Cargo.toml`, every tracked `**/Cargo.toml`, `Cargo.lock`, the - toolchain files and `.cargo/config.toml`, so a job that uses it reruns when any of them changes, without listing them - in its `inputs`. +- the files it depends on. The `rust` module adds `Cargo.toml`, `Cargo.lock`, the toolchain files and + `.cargo/config.toml` at any depth under its `path` and in each directory above it, as cargo and rustup read them, so a job that uses it reruns when any of them changes, without listing them in its `inputs`. The generated hash step also passes `--modules-digest`, a digest of the job's module steps when the config was generated. `cigen hash` mixes it into the hash and warns when its own steps differ, so a config generated by another diff --git a/internal_docs/STATUS.md b/internal_docs/STATUS.md index 98fd6b6..479f576 100644 --- a/internal_docs/STATUS.md +++ b/internal_docs/STATUS.md @@ -81,7 +81,9 @@ What the code does today, checked against the code in December 2026. Where cigen hash and warns when its own expansion differs, so a config generated by another cigen never reuses a pass. Modules: - `rust`: installs the toolchain `rust-toolchain.toml` names (or a channel) with `rustup`, adds components and targets, and caches with `Swatinem/rust-cache` (GitHub) or `restore_cache`/`save_cache` (CircleCI). It adds - `Cargo.toml`, `**/Cargo.toml`, `Cargo.lock`, the toolchain files and `.cargo/config.toml` to the job's inputs. + every tracked `Cargo.toml` under `path`, and `Cargo.toml`, `Cargo.lock`, the toolchain files and + `.cargo/config.toml` in `path` and each directory above it, and the same files at any depth under `path` (a nested + workspace such as a fuzz crate reads its own), to the job's inputs. - **Job graph** (`src/orchestrator/dag.rs`): dependency resolution and matrix expansion, with job names both providers accept (`src/orchestrator/job_names.rs`). - **Providers** run as separate binaries spawned over a length-prefixed protobuf stdio protocol (`proto/plugin.proto`, @@ -162,7 +164,9 @@ What the code does today, checked against the code in December 2026. Where cigen `cigen generate`; examples of errors must fail with the error they show. Every file it reads must be an input of cigen's own `test` job. - `tests/module_scenarios.rs`: a job using the `rust` module installs nothing when it skips, and reruns in mockci when - a file the module contributes or its parameters change; the generated `--modules-digest` changes the hash, and a + a file the module contributes or its parameters change, including, for a crate below the root, the workspace's + `Cargo.toml` and `Cargo.lock`, a `.cargo/config.toml` in any directory above it, and the root toolchain file, and + for a crate at the root, a nested workspace's lockfile, config and toolchain file; the generated `--modules-digest` changes the hash, and a local `hash --job` without it gives the CI step's hash; unknown modules and parameters fail generation with a fix. - `tests/module_catalogue.rs`: every registered module has a skill with frontmatter, a README documenting exactly its parameters, a golden fixture (`integration_tests/module_/`) for each provider it supports that mockci's parser diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 1c32d81..a4904f1 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -8,6 +8,7 @@ pub mod catalogue; mod expand; +mod paths; pub mod rust; mod shell; diff --git a/src/modules/paths.rs b/src/modules/paths.rs new file mode 100644 index 0000000..ab16ba3 --- /dev/null +++ b/src/modules/paths.rs @@ -0,0 +1,55 @@ +//! Where a module's files are: package managers and toolchains read config files in the +//! project's directory and in every directory above it, up to the project root. +use super::in_path; + +/// `path` and every directory above it, ending with the root (`.`). +pub fn dirs_up(path: &str) -> Vec { + let mut dirs = vec![path.to_string()]; + let mut current = path; + while current != "." { + current = match current.rsplit_once('/') { + Some((parent, _)) => parent, + None => ".", + }; + dirs.push(current.to_string()); + } + dirs +} + +/// The input pattern for `file` in `dir`. A pattern without a slash matches at any depth, as in +/// gitignore, so a file at the root gets a leading `/` to match only there. +pub fn anchored(dir: &str, file: &str) -> String { + match dir { + "." if file.contains('/') => file.to_string(), + "." => format!("/{file}"), + _ => in_path(dir, file), + } +} + +/// `files` in `path` and in every directory above it, as anchored patterns. +pub fn in_dirs_up(path: &str, files: &[&str]) -> Vec { + dirs_up(path) + .iter() + .flat_map(|dir| files.iter().map(move |file| anchored(dir, file))) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dirs_go_up_to_the_root() { + assert_eq!(dirs_up("."), ["."]); + assert_eq!(dirs_up("web"), ["web", "."]); + assert_eq!(dirs_up("apps/web"), ["apps/web", "apps", "."]); + } + + #[test] + fn root_files_are_anchored() { + assert_eq!(anchored(".", "Cargo.lock"), "/Cargo.lock"); + assert_eq!(anchored(".", ".cargo/config.toml"), ".cargo/config.toml"); + assert_eq!(anchored("crates", "Cargo.lock"), "crates/Cargo.lock"); + assert_eq!(in_dirs_up("a/b", &["x"]), ["a/b/x", "a/x", "/x"]); + } +} diff --git a/src/modules/rust/README.md b/src/modules/rust/README.md index 9cb2901..1cfa645 100644 --- a/src/modules/rust/README.md +++ b/src/modules/rust/README.md @@ -54,10 +54,12 @@ The cache, when `cache` is true: ## Inputs -The module adds these files, inside `path`, to the hash of every job that uses it: `Cargo.toml`, every tracked -`**/Cargo.toml`, `Cargo.lock`, `rust-toolchain`, `rust-toolchain.toml`, `.cargo/config.toml` and `.cargo/config`, plus -the toolchain file it found. Its parameters and the steps it adds go into the hash too, so a job reruns when either -changes. +Cargo reads the workspace's `Cargo.toml` and `Cargo.lock` and every `.cargo/config.toml` from the crate up to the +root, and rustup the nearest toolchain file, so the module adds `Cargo.toml`, `Cargo.lock`, `rust-toolchain`, +`rust-toolchain.toml`, `.cargo/config.toml` and `.cargo/config` in `path` and in every directory above it, up to the +project root, to the hash of every job that uses it. It also adds the same files at any depth under `path`, since a +step can `cd` into a nested workspace (a fuzz crate) that reads its own lockfile, config and toolchain file, and the +toolchain file it found. Its parameters and the steps it adds go into the hash too, so a job reruns when either changes. ## Pinned actions diff --git a/src/modules/rust/mod.rs b/src/modules/rust/mod.rs index 73d9fc8..c66a313 100644 --- a/src/modules/rust/mod.rs +++ b/src/modules/rust/mod.rs @@ -1,7 +1,7 @@ //! The `rust` module: installs the toolchain `rust-toolchain.toml` names (or a channel), adds //! components and targets, and caches cargo's downloads and `target/`. use super::shell::check_token; -use super::{Expansion, Module, ModuleSpec, Parameter, in_path, module_path, parse_params}; +use super::{Expansion, Module, ModuleSpec, Parameter, in_path, module_path, parse_params, paths}; use crate::schema::{ RestoreCacheDefinition, RunCommand, RunStep, SaveCacheDefinition, Step, UsesStep, }; @@ -250,20 +250,27 @@ impl Module for Rust { } fn inputs(&self) -> Vec { - let mut inputs: Vec = [ + // Cargo reads the workspace's manifest and lockfile and every `.cargo/config.toml` from + // `path` up to the root, and rustup the nearest toolchain file. + const FILES: &[&str] = &[ "Cargo.toml", - "**/Cargo.toml", "Cargo.lock", "rust-toolchain", "rust-toolchain.toml", ".cargo/config.toml", ".cargo/config", - ] - .iter() - .map(|file| in_path(&self.path, file)) - .collect(); + ]; + let mut inputs = paths::in_dirs_up(&self.path, FILES); + // Under `path`, at any depth too: a step can `cd` into a nested workspace (a fuzz + // crate) that reads its own lockfile, config and toolchain file. + inputs.extend( + FILES + .iter() + .map(|file| in_path(&self.path, &format!("**/{file}"))), + ); if let Toolchain::File(file) = &self.toolchain { - inputs.push(file.clone()); + let (dir, name) = file.rsplit_once('/').unwrap_or((".", file)); + inputs.push(paths::anchored(dir, name)); } inputs.sort(); inputs.dedup(); diff --git a/src/modules/rust/tests.rs b/src/modules/rust/tests.rs index 746b78f..5da1132 100644 --- a/src/modules/rust/tests.rs +++ b/src/modules/rust/tests.rs @@ -86,8 +86,17 @@ fn a_workspace_in_a_directory() { let inputs = module.inputs(); assert!(inputs.contains(&"crates/app/Cargo.lock".to_string())); assert!(inputs.contains(&"crates/app/**/Cargo.toml".to_string())); - // The toolchain file in a parent directory is the one rustup reads. - assert!(inputs.contains(&"rust-toolchain.toml".to_string())); + // The toolchain file in a parent directory is the one rustup reads, anchored at the root. + assert!(inputs.contains(&"/rust-toolchain.toml".to_string())); + // Cargo reads the workspace manifest and config above the crate. + for above in [ + "crates/Cargo.toml", + "/Cargo.toml", + "/Cargo.lock", + ".cargo/config.toml", + ] { + assert!(inputs.contains(&above.to_string()), "{above}: {inputs:?}"); + } assert_eq!( module.resolved()["toolchain"]["file"], "rust-toolchain.toml" @@ -168,3 +177,24 @@ fn mistakes_fail_with_a_fix() { let error = rust("{}", &empty).err().unwrap().to_string(); assert!(error.contains("no `Cargo.toml`"), "{error}"); } + +#[test] +fn nested_lockfiles_configs_and_toolchain_files_are_inputs() { + // A nested workspace (a fuzz crate) reads its own; over-including is the safe direction. + let dir = project(&["Cargo.toml", "Cargo.lock", "rust-toolchain.toml"]); + let inputs = rust("{}", &dir).unwrap().inputs(); + for expected in [ + "/Cargo.lock", + "**/Cargo.lock", + "**/Cargo.toml", + "**/.cargo/config.toml", + "**/.cargo/config", + "**/rust-toolchain", + "**/rust-toolchain.toml", + ] { + assert!( + inputs.contains(&expected.to_string()), + "{expected}: {inputs:?}" + ); + } +} diff --git a/tests/module_scenarios.rs b/tests/module_scenarios.rs index 579aee8..bebd316 100644 --- a/tests/module_scenarios.rs +++ b/tests/module_scenarios.rs @@ -77,6 +77,14 @@ fn changing_a_file_the_module_contributes_reruns_the_job() { ), ("crates/extra/Cargo.toml", "[package]\nname = \"extra\"\n"), (".cargo/config.toml", "[build]\njobs = 2\n"), + // A nested workspace a step can `cd` into (a fuzz crate) reads its own lockfile, + // config and toolchain file. + ("fuzz/Cargo.lock", "version = 4\n# fuzz\n"), + ("fuzz/.cargo/config.toml", "[build]\njobs = 3\n"), + ( + "fuzz/rust-toolchain.toml", + "[toolchain]\nchannel = \"nightly\"\n", + ), ] { repo.write(path, contents); let changed = repo.commit(&format!("change {path}")); @@ -88,6 +96,43 @@ fn changing_a_file_the_module_contributes_reruns_the_job() { } } +/// A crate in a workspace: cargo reads the workspace's `Cargo.toml` and `Cargo.lock` and every +/// `.cargo/config.toml` from the crate up to the root, and rustup the nearest toolchain file. +#[test] +fn a_crate_below_the_root_reruns_when_files_above_it_change() { + for (path, contents) in [ + ( + "Cargo.toml", + "[workspace]\nmembers = [\"crates/core\"]\n[profile.dev]\nopt-level = 1\n", + ), + ("Cargo.lock", "version = 4\n# changed\n"), + (".cargo/config.toml", "[build]\njobs = 2\n"), + ("crates/.cargo/config.toml", "[build]\njobs = 2\n"), + ("rust-toolchain.toml", "[toolchain]\nchannel = \"beta\"\n"), + ] { + let repo = FixtureRepo::new(); + repo.write(".cigen/config.yml", "provider: github\n") + .write( + ".cigen/workflows/ci/jobs/test.yml", + "modules:\n - rust: { path: crates/core }\ninputs: \"crates/core/src/**\"\nsteps:\n - run:\n name: Run tests\n command: cd crates/core && cargo test\n", + ) + .write("Cargo.toml", "[workspace]\nmembers = [\"crates/core\"]\n") + .write("Cargo.lock", "version = 4\n") + .write("crates/core/Cargo.toml", "[package]\nname = \"core\"\n") + .write("crates/core/src/lib.rs", "pub fn one() {}\n") + .generate(); + let mut sim = rust_simulator(&repo); + let first = repo.commit("initial"); + assert!(tests_ran(&sim.run(WORKFLOW, &push(&first)).unwrap())); + assert!(!tests_ran(&sim.run(WORKFLOW, &push(&first)).unwrap())); + + repo.write(path, contents); + let changed = repo.commit(&format!("change {path}")); + let run = sim.run(WORKFLOW, &push(&changed)).unwrap(); + assert!(tests_ran(&run), "changing {path} must rerun the job\n{run}"); + } +} + #[test] fn changing_the_module_parameters_reruns_the_job() { let repo = rust_fixture();