From 54e69d716e543e01e78197907cd13bb7961cfd2c Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 4 Dec 2026 13:36:02 +1300 Subject: [PATCH 1/2] Add cigen expand: the whole config as one cigen.yml cigen expand [--config ] [--format yaml|json] checks the config and prints the document the loader builds: a .cigen/ directory's files merged into one cigen.yml. tests/expand.rs expands every config in the repo and checks the output loads to the same config and expands to itself. --- internal_docs/STATUS.md | 4 ++ src/commands/expand.rs | 42 +++++++++++++++++++++ src/commands/mod.rs | 2 + src/config/mod.rs | 17 ++++++++- src/main.rs | 6 +++ tests/expand.rs | 84 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/commands/expand.rs create mode 100644 tests/expand.rs diff --git a/internal_docs/STATUS.md b/internal_docs/STATUS.md index 8943639..bee69dc 100644 --- a/internal_docs/STATUS.md +++ b/internal_docs/STATUS.md @@ -22,6 +22,8 @@ What the code does today, checked against the code in December 2026. Where cigen generated step passes no `--modules-digest` for the needed job): a one-time rerun of those jobs, never a skip. For a job that runs GitHub's matrix, explain says each entry is hashed in, and with `--matrix` it shows that entry's hash (`src/hashing/matrix.rs` mixes the entry into the job's hash, for `hash --matrix` too). + `cigen expand [--config ] [--format yaml|json]` checks the config and prints it as one `cigen.yml` document, + a `.cigen/` directory's files merged the way the loader merges them (comments aren't kept). - **Config loading** (`src/config/`, `src/schema/`): one schema in two layouts. `cigen.yml` is the whole document; `.cigen/` is the same document split into files (`config.yml` and `config/*.yml` for the top level, `commands/.yml`, `workflows/.yml` or `workflows//config.yml` for a workflow's settings, and @@ -146,6 +148,8 @@ What the code does today, checked against the code in December 2026. Where cigen - `tests/explain.rs`: `cigen explain` lists declared and derived inputs (module inputs included) with their files and the jobs a job needs, in JSON and text, and its hash equals `cigen hash --job`'s before and after an input, a module input or a needed job changes, and for a matrix entry. +- `tests/expand.rs`: expanding every config in the repository gives a `cigen.yml` that loads to the same config and + expands to itself; the JSON form is the same document; an invalid config isn't expanded. - `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. diff --git a/src/commands/expand.rs b/src/commands/expand.rs new file mode 100644 index 0000000..b8467f0 --- /dev/null +++ b/src/commands/expand.rs @@ -0,0 +1,42 @@ +//! `cigen expand`: the whole config as one `cigen.yml` document. + +use super::report::print_json; +use anyhow::{Context, Result}; +use clap::{Args, ValueEnum}; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] +pub enum DocumentFormat { + Yaml, + Json, +} + +#[derive(Debug, Args)] +pub struct ExpandArgs { + /// The config: a cigen.yml file or a .cigen/ directory (found in the current directory by default) + #[arg(short, long)] + pub config: Option, + + /// Output format. YAML output is a cigen.yml that loads to the same config (comments aren't kept). + #[arg(long, value_enum, default_value_t = DocumentFormat::Yaml)] + pub format: DocumentFormat, +} + +pub fn expand_command(args: ExpandArgs) -> Result<()> { + let path = match args.config { + Some(path) => path, + None => cigen::config::find(Path::new("."))?, + }; + let document = serde_yaml::Value::Mapping(cigen::config::expand(&path)?); + match args.format { + DocumentFormat::Yaml => { + print!("{}", serde_yaml::to_string(&document)?); + Ok(()) + } + DocumentFormat::Json => { + let json = serde_json::to_value(&document) + .context("the config has a value JSON can't represent; use --format yaml")?; + print_json(&json) + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a9a5365..423f64c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,9 +1,11 @@ +mod expand; mod explain; mod generate; mod hash; pub mod report; mod validate; +pub use expand::{ExpandArgs, expand_command}; pub use explain::{ExplainArgs, explain_command}; pub use generate::generate_command; pub use hash::{HashArgs, hash_command}; diff --git a/src/config/mod.rs b/src/config/mod.rs index 78d3bdd..aa13e03 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -47,6 +47,12 @@ fn holds_split_config(dir: &Path) -> bool { /// Loads and checks the config at `path` (a `cigen.yml` file or a `.cigen/` directory). pub fn load(path: &Path) -> Result { + let document = read(path)?; + lower::lower(&document) +} + +/// Reads either layout into one document, refusing a project that has both. +fn read(path: &Path) -> Result { if !path.exists() { return Err(ConfigError::new("config not found") .in_file(path) @@ -68,8 +74,15 @@ pub fn load(path: &Path) -> Result { return Err(both_layouts(project_dir).into()); } } - let document = layout::read(path)?; - lower::lower(&document) + Ok(layout::read(path)?) +} + +/// The config at `path` as one `cigen.yml` document: a `.cigen/` directory's files merged the +/// way the loader merges them. The config is checked first, so the document always loads. +pub fn expand(path: &Path) -> Result { + let document = read(path)?; + lower::lower(&document)?; + Ok(document.root) } /// Loads and checks a whole config from text, in the single-file (`cigen.yml`) layout. diff --git a/src/main.rs b/src/main.rs index 2c95cad..cbc404f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,11 @@ enum Commands { #[command(flatten)] args: commands::ExplainArgs, }, + /// Print the whole config as one cigen.yml document, the way the loader sees it + Expand { + #[command(flatten)] + args: commands::ExpandArgs, + }, /// Check the config and list every problem in it Validate { #[command(flatten)] @@ -71,6 +76,7 @@ fn run(cli: Cli) -> Result { Some(Commands::Hash { args }) => { commands::hash_command(args)?; } + Some(Commands::Expand { args }) => commands::expand_command(args)?, Some(Commands::Explain { args }) => commands::explain_command(args)?, Some(Commands::Validate { args }) => return commands::validate_command(args), None => { diff --git a/tests/expand.rs b/tests/expand.rs new file mode 100644 index 0000000..c261715 --- /dev/null +++ b/tests/expand.rs @@ -0,0 +1,84 @@ +//! `cigen expand`: one `cigen.yml` document that loads to the same config as the original. + +mod support; + +use std::fs; +use std::path::{Path, PathBuf}; +use tempfile::tempdir; + +fn configs_in_repo() -> Vec { + let root = support::repo_root(); + let mut dirs = vec![root.join(".cigen")]; + for entry in fs::read_dir(root.join("integration_tests")).unwrap() { + let dir = entry.unwrap().path().join(".cigen"); + if dir.is_dir() { + dirs.push(dir); + } + } + dirs.sort(); + dirs +} + +fn expand(config: &Path, format: &str) -> String { + let output = support::cigen() + .args(["expand", "--format", format, "--config"]) + .arg(config) + .output() + .unwrap(); + assert!(output.status.success(), "{config:?}: {output:?}"); + String::from_utf8(output.stdout).unwrap() +} + +#[test] +fn an_expanded_directory_loads_to_the_same_config() { + for dir in configs_in_repo() { + let original = cigen::config::load(&dir).unwrap(); + let expanded = expand(&dir, "yaml"); + + let project = tempdir().unwrap(); + let file = project.path().join("cigen.yml"); + fs::write(&file, &expanded).unwrap(); + assert_eq!( + cigen::config::load(&file).unwrap(), + original, + "{}", + dir.display() + ); + + // Expanding the single file again changes nothing. + assert_eq!(expand(&file, "yaml"), expanded, "{}", dir.display()); + } +} + +#[test] +fn json_is_the_same_document() { + for dir in configs_in_repo() { + let yaml: serde_yaml::Value = serde_yaml::from_str(&expand(&dir, "yaml")).unwrap(); + let json: serde_json::Value = serde_json::from_str(&expand(&dir, "json")).unwrap(); + assert_eq!( + serde_json::to_value(yaml).unwrap(), + json, + "{}", + dir.display() + ); + } +} + +#[test] +fn an_invalid_config_is_not_expanded() { + let project = tempdir().unwrap(); + fs::write( + project.path().join("cigen.yml"), + "provider: github\nworkflows:\n ci:\n jobs:\n test:\n stpes: []\n", + ) + .unwrap(); + let output = support::cigen() + .current_dir(project.path()) + .arg("expand") + .output() + .unwrap(); + assert!(!output.status.success()); + assert!(output.stdout.is_empty()); + let stderr = String::from_utf8(output.stderr).unwrap(); + assert!(stderr.contains("did you mean `steps`?"), "{stderr}"); +} From c12bc00ac73cdfc3eb2b63a420a7ff5b9b2c088a Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 4 Dec 2026 14:34:15 +1300 Subject: [PATCH 2/2] Docs: cigen expand The CLI page documents cigen expand: its options, the output for an example .cigen/ directory, and what it keeps and doesn't. The overview, roadmap and homepage list it as built. --- docs/src/content/docs/cli.md | 52 +++++++++++++++++++++++++++++++- docs/src/content/docs/index.md | 1 + docs/src/content/docs/roadmap.md | 5 +-- docs/src/pages/index.astro | 4 +-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/docs/src/content/docs/cli.md b/docs/src/content/docs/cli.md index dcbecd4..b8472f6 100644 --- a/docs/src/content/docs/cli.md +++ b/docs/src/content/docs/cli.md @@ -1,6 +1,6 @@ --- title: CLI -description: cigen validate, cigen generate, cigen hash and cigen explain, with every option. +description: cigen validate, generate, hash, explain and expand, with every option. --- ```text @@ -183,6 +183,56 @@ With `--format json`, it prints one object with `job`, `workflow`, `inputs` (eac `hash`), `also_hashed`, `runs_matrix`, `matrix_entry` (the entry given, or `null`) and `hash`. An unknown job is an error that suggests the closest id. +## `cigen expand` + +Prints the whole config as one `cigen.yml`, the way the loader sees it: a `.cigen/` directory's files merged into one +document. Use it to read a split config in one place, to turn a `.cigen/` directory into a single `cigen.yml`, or to +hand an agent the whole config at once. + +```sh +cigen expand # finds cigen.yml or .cigen/ in the current directory +cigen expand --config .cigen > cigen.yml +cigen expand --format json +``` + +| Option | What it does | +| ----------------------- | ------------------------------------------------------------------------------------------------------ | +| `-c`, `--config ` | The config: a `cigen.yml` file or a `.cigen/` directory. Default: whichever the current directory has. | +| `--format ` | `yaml` (the default) or `json`. | + +For a `.cigen/` directory with `config.yml`, `commands/setup.yml`, `workflows/ci.yml` and +`workflows/ci/jobs/test.yml`: + +```text +$ cigen expand +provider: github +input_groups: + rust: + - src/**/*.rs + - Cargo.lock +commands: + setup: + steps: + - run: cargo fetch +workflows: + ci: + jobs: + test: + image: rust:1.85 + inputs: + - '@rust' + steps: + - setup + - run: cargo test + github: + on: + - push +``` + +The YAML loads to the same config as the directory, but comments aren't kept. Commands +stay as references (`- setup`): expanding them into steps is the provider's job. The config is checked first, so a +config with problems prints the same errors as `cigen validate` and nothing on stdout. + ## Environment | Variable | What it does | diff --git a/docs/src/content/docs/index.md b/docs/src/content/docs/index.md index b2e41fa..28da913 100644 --- a/docs/src/content/docs/index.md +++ b/docs/src/content/docs/index.md @@ -27,6 +27,7 @@ what to write instead. - `cigen generate` for GitHub Actions and CircleCI. - `cigen hash`, which computes a job's hash from its inputs and the inputs cigen derives. - `cigen explain`, which shows what a job's hash covers: each input, the files it matches, and the derived inputs. +- `cigen expand`, which prints the whole config as one `cigen.yml`, the way the loader sees it. - Skipping on GitHub Actions, using markers in the GitHub Actions cache. - Deterministic output: generating twice gives the same bytes. diff --git a/docs/src/content/docs/roadmap.md b/docs/src/content/docs/roadmap.md index ee4a158..b9b9233 100644 --- a/docs/src/content/docs/roadmap.md +++ b/docs/src/content/docs/roadmap.md @@ -15,9 +15,10 @@ right. This page lists what's planned. Nothing on it exists yet; what exists is already passed never start a runner, and a final job counts "skipped" as passed for required checks. 3. **Pluggable pass-record stores.** The GitHub Actions cache by default, self-hosted stores such as Redis and S3, and an open protocol. If a store can't be reached, everything runs. -4. **Agent tools.** `cigen plan` (which jobs will run or skip, and why) and `cigen expand `. (The +4. **Agent tools.** `cigen plan`: which jobs will run or skip, and why. (The [JSON schemas](/docs/configuration/#json-schemas), [`cigen validate`](/docs/cli/#cigen-validate) with errors as JSON, - and [`cigen explain`](/docs/cli/#cigen-explain), which shows what a job's hash covers, are already built.) + [`cigen explain`](/docs/cli/#cigen-explain), which shows what a job's hash covers, and + [`cigen expand`](/docs/cli/#cigen-expand), which prints the whole config as one file, are already built.) 5. **Prebuilt binaries** and Docker images with cigen installed. Generated workflows already install the release that generated them; v0.2.0 will be the first release with the archives they download. 6. **Hosted pass records on VisualCI.** A free account, signed in with GitHub, with records visible across branches, diff --git a/docs/src/pages/index.astro b/docs/src/pages/index.astro index 3c2b7ed..e52b2dc 100644 --- a/docs/src/pages/index.astro +++ b/docs/src/pages/index.astro @@ -193,7 +193,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
  • cigen validate, with errors as JSON for agents
  • GitHub Actions and CircleCI generation
  • cigen hash with derived inputs
  • -
  • cigen explain: what a job's hash covers
  • +
  • cigen explain and expand for agents
  • Skipping on GitHub Actions, with markers in its cache
  • Generated workflows install a checksum-checked cigen release
  • Byte-for-byte deterministic output
  • @@ -206,7 +206,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
  • The v0.2.0 release, the first those workflows can install
  • A planner job, so skipped jobs never start a runner
  • More modules: Node, Playwright, services and caches
  • -
  • cigen plan and expand for agents
  • +
  • cigen plan: which jobs will run or skip, and why
  • Free hosted pass records on VisualCI, shared across branches
  • Skipping on CircleCI