diff --git a/docs/src/content/docs/inputs.md b/docs/src/content/docs/inputs.md index 62e3114..63ae960 100644 --- a/docs/src/content/docs/inputs.md +++ b/docs/src/content/docs/inputs.md @@ -96,6 +96,8 @@ On top of the job's `inputs`, a job's hash covers: `.mise.toml`, and `package.json`'s `packageManager`. - **Lockfiles** in the project root: `Cargo.lock`, `Gemfile.lock`, `package-lock.json`, `pnpm-lock.yaml`, `yarn.lock`, `bun.lock`, `bun.lockb`, `poetry.lock`, `uv.lock`, `Pipfile.lock`, `composer.lock`, `go.sum`. +- **Every `package.json`** in the project, tracked by git. `npm run test` runs whatever the `test` script says, and + cigen can't tell which job runs a package manager, or in which package. A command or service the job doesn't use isn't in its hash, so changing it doesn't rerun the job. diff --git a/internal_docs/STATUS.md b/internal_docs/STATUS.md index 8de1a8a..e01659d 100644 --- a/internal_docs/STATUS.md +++ b/internal_docs/STATUS.md @@ -47,7 +47,8 @@ What the code does today, checked against the code in December 2026. Where cigen `inputs`, the hashes of the jobs it `needs` (transitively, so a changed build reruns the tests that need it), the commands it uses (through other commands too), the services it names, the top-level provider blocks, every project file its steps name (`sh scripts/check.sh`, `require('./x.js')`, every file of a local `uses: ./action`), and the root - toolchain pins, lockfiles and `package.json` `packageManager`, plus each module's resolved parameters, its steps for + toolchain pins, lockfiles and `package.json` `packageManager`, every tracked `package.json` (its + scripts are what `npm run` runs; `src/inputs/packages.rs`), plus each module's resolved parameters, its steps for every provider the config names, and the files it contributes. Each rule includes too much rather than too little. - **Globs** (`src/pathspec.rs`): every glob cigen hashes (job and group `inputs`, module inputs, `hash --pattern`) is matched by `pathspec::list_files`, with gitignore's rules: a glob with no `/` matches at any depth (`*.md` is @@ -125,8 +126,8 @@ What the code does today, checked against the code in December 2026. Where cigen - `tests/git_ownership.rs`: `cigen hash` in a checkout git treats as owned by another user (`GIT_TEST_ASSUME_DIFFERENT_OWNER`), at the repository root and in a subdirectory, matches the hash computed as its owner. -- `tests/derived_inputs.rs`: changing a script a step runs, a command the job uses or a toolchain pin makes the job - run in mockci, and changing a used command, a named service, a top-level provider block, a local action, a lockfile or the +- `tests/derived_inputs.rs`: changing a script a step runs, a command the job uses, a toolchain pin or the + `package.json` script that `npm run test` runs (the root's or a sub-package's) makes the job run in mockci, and changing a used command, a named service, a top-level provider block, a local action, a lockfile or the `packageManager` pin changes the job's hash; an unused command or service doesn't. - `tests/docs_examples.rs`: every YAML example in the docs (`docs/src/content/docs/`) and the README, and the homepage's config, is run through `cigen generate`; examples of errors must fail with the error they show. diff --git a/src/inputs.rs b/src/inputs.rs index 17c071f..9e34fcf 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -10,13 +10,17 @@ //! `require('./.github/wait-for-checks.js')`. A local action (`uses: ./path`) brings every file //! in its directory. Files those files mention are not followed. //! - **Toolchain pins and lockfiles** in the project root (`rust-toolchain.toml`, `.ruby-version`, -//! `Cargo.lock`, `pnpm-lock.yaml`, and so on) and `package.json`'s `packageManager`, for every -//! job: cigen can't tell which tools a step runs. +//! `Cargo.lock`, `pnpm-lock.yaml`, and so on), `package.json`'s `packageManager`, and every +//! tracked `package.json` (its scripts are what `npm run` runs), for every job: cigen can't +//! tell which tools a step runs. //! - **Modules**: each module's resolved parameters and its steps for every provider, and the //! files it contributes (see `crate::modules`). +mod packages; + use crate::schema::{CigenConfig, Job}; use anyhow::{Context, Result}; +use packages::{PACKAGE_FILES, package_manager}; use serde::Serialize; use serde_json::{Map, Value}; use std::collections::BTreeSet; @@ -61,7 +65,8 @@ pub struct Derived { pub config: Vec<(String, String)>, /// Files relative to the project root, sorted. pub files: Vec, - /// Patterns, relative to the project root, of files the job's modules contribute. + /// Patterns, relative to the project root, of tracked files: those the job's modules + /// contribute, and every `package.json`. pub patterns: Vec, /// The digest of the job's module expansions (see `crate::modules::for_job`). pub modules_digest: String, @@ -101,6 +106,9 @@ pub fn derive(config: &CigenConfig, job_id: &str, project_root: &Path) -> Result derived.config.extend(modules.entries); derived.patterns = modules.inputs; derived.modules_digest = modules.digest; + derived + .patterns + .extend(PACKAGE_FILES.iter().map(|p| p.to_string())); if let Some(manager) = package_manager(project_root)? { derived .config @@ -125,19 +133,6 @@ pub fn derive(config: &CigenConfig, job_id: &str, project_root: &Path) -> Result Ok(derived) } -/// `packageManager` from the root `package.json` (it pins the package manager's version). -fn package_manager(root: &Path) -> Result> { - let path = root.join("package.json"); - if !path.is_file() { - return Ok(None); - } - let text = - std::fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?; - let json: Value = serde_json::from_str(&text) - .with_context(|| format!("parsing {} to find `packageManager`", path.display()))?; - Ok(json.get("packageManager").map(Value::to_string)) -} - /// Commands the job's steps use, directly or through other commands. A step uses a command by /// naming it, as a string (`- setup_db`) or a key (`- setup_db: {…}`); any string or key that /// matches a command's name counts, wherever it is in the steps. diff --git a/src/inputs/packages.rs b/src/inputs/packages.rs new file mode 100644 index 0000000..a0df2a3 --- /dev/null +++ b/src/inputs/packages.rs @@ -0,0 +1,22 @@ +//! Node's `package.json` files. `npm run test` runs whatever the `test` script says, in +//! whichever package it runs in, and cigen can't tell which job runs a package manager or where, +//! so every job gets every tracked `package.json`, like the lockfiles. +use anyhow::{Context, Result}; +use serde_json::Value; +use std::path::Path; + +/// Every tracked `package.json`, the root's included. +pub const PACKAGE_FILES: &[&str] = &["**/package.json"]; + +/// `packageManager` from the root `package.json` (it pins the package manager's version). +pub fn package_manager(root: &Path) -> Result> { + let path = root.join("package.json"); + if !path.is_file() { + return Ok(None); + } + let text = + std::fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?; + let json: Value = serde_json::from_str(&text) + .with_context(|| format!("parsing {} to find `packageManager`", path.display()))?; + Ok(json.get("packageManager").map(Value::to_string)) +} diff --git a/tests/derived_inputs.rs b/tests/derived_inputs.rs index ba229c7..a2b9eec 100644 --- a/tests/derived_inputs.rs +++ b/tests/derived_inputs.rs @@ -105,6 +105,41 @@ fn lockfiles_and_the_package_manager_pin_are_inputs() { assert_ne!(job_hash(&nine), job_hash(&ten)); } +/// A job that runs `npm run test`: what runs is the `test` script in `package.json`. +#[test] +fn changing_the_package_script_the_job_runs_makes_it_run() { + let repo = FixtureRepo::new(); + repo.write(".cigen/config.yml", "provider: github\n") + .write( + ".cigen/workflows/ci/jobs/test.yml", + "inputs: \"src/**\"\nsteps:\n - run:\n name: Run tests\n command: npm run test\n", + ) + .write("package.json", "{\"scripts\": {\"test\": \"node --test\"}}\n") + .write("web/package.json", "{\"scripts\": {\"test\": \"vitest\"}}\n") + .write("src/index.js", "export const one = 1;\n") + .generate(); + let mut sim = simulator(&repo); + sim.provide_command("npm", "echo \"npm $*\"\n").unwrap(); + 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( + "package.json", + "{\"scripts\": {\"test\": \"node --test --bail\"}}\n", + ); + let root = repo.commit("change the root test script"); + assert!(tests_ran(&sim.run(WORKFLOW, &push(&root)).unwrap())); + + // cigen can't tell which package a step's npm runs in, so every package.json counts. + repo.write( + "web/package.json", + "{\"scripts\": {\"test\": \"vitest run\"}}\n", + ); + let web = repo.commit("change a sub-package's test script"); + assert!(tests_ran(&sim.run(WORKFLOW, &push(&web)).unwrap())); +} + #[test] fn a_file_no_step_names_still_skips() { let repo = script_fixture();