Skip to content
Merged
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
52 changes: 51 additions & 1 deletion docs/src/content/docs/cli.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <path>` | The config: a `cigen.yml` file or a `.cigen/` directory. Default: whichever the current directory has. |
| `--format <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 |
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions docs/src/content/docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <job>`. (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,
Expand Down
4 changes: 2 additions & 2 deletions docs/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
<li><code>cigen validate</code>, with errors as JSON for agents</li>
<li>GitHub Actions and CircleCI generation</li>
<li><code>cigen hash</code> with derived inputs</li>
<li><code>cigen explain</code>: what a job's hash covers</li>
<li><code>cigen explain</code> and <code>expand</code> for agents</li>
<li>Skipping on GitHub Actions, with markers in its cache</li>
<li>Generated workflows install a checksum-checked cigen release</li>
<li>Byte-for-byte deterministic output</li>
Expand All @@ -206,7 +206,7 @@ const skipped = run.jobs.filter((job) => job.status === 'skipped').length;
<li>The v0.2.0 release, the first those workflows can install</li>
<li>A planner job, so skipped jobs never start a runner</li>
<li>More modules: Node, Playwright, services and caches</li>
<li><code>cigen plan</code> and <code>expand</code> for agents</li>
<li><code>cigen plan</code>: which jobs will run or skip, and why</li>
<li>Free hosted pass records on VisualCI, shared across branches</li>
<li>Skipping on CircleCI</li>
</ul>
Expand Down
4 changes: 4 additions & 0 deletions internal_docs/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>] [--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/<name>.yml`, `workflows/<name>.yml` or `workflows/<name>/config.yml` for a workflow's settings, and
Expand Down Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions src/commands/expand.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,

/// 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)
}
}
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
17 changes: 15 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CigenConfig, ConfigErrors> {
let document = read(path)?;
lower::lower(&document)
}

/// Reads either layout into one document, refusing a project that has both.
fn read(path: &Path) -> Result<layout::Document, ConfigErrors> {
if !path.exists() {
return Err(ConfigError::new("config not found")
.in_file(path)
Expand All @@ -68,8 +74,15 @@ pub fn load(path: &Path) -> Result<CigenConfig, ConfigErrors> {
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<serde_yaml::Mapping, ConfigErrors> {
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.
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -71,6 +76,7 @@ fn run(cli: Cli) -> Result<bool> {
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 => {
Expand Down
84 changes: 84 additions & 0 deletions tests/expand.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
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}");
}
Loading