Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/src/content/docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions internal_docs/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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_<name>/`) for each provider it supports that mockci's parser
Expand Down
1 change: 1 addition & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

pub mod catalogue;
mod expand;
mod paths;
pub mod rust;
mod shell;

Expand Down
55 changes: 55 additions & 0 deletions src/modules/paths.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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<String> {
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"]);
}
}
10 changes: 6 additions & 4 deletions src/modules/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 15 additions & 8 deletions src/modules/rust/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -250,20 +250,27 @@ impl Module for Rust {
}

fn inputs(&self) -> Vec<String> {
let mut inputs: Vec<String> = [
// 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();
Expand Down
34 changes: 32 additions & 2 deletions src/modules/rust/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:?}"
);
}
}
45 changes: 45 additions & 0 deletions tests/module_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
Expand All @@ -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();
Expand Down
Loading