From 519236282a4d01b24ebd7295835941bdeebaae2c Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:37:50 +0100 Subject: [PATCH] feat: recipes-only makes T3 Code and CI run recipes rather than raw cargo --- crates/sourceorigin-standard/README.md | 5 + .../sourceorigin-standard/src/config/mod.rs | 5 + crates/sourceorigin-standard/src/rules/mod.rs | 35 ++++ .../src/rules/recipes_only.rs | 68 +++++++ crates/sourceorigin-standard/src/rules/t3.rs | 62 +++++++ .../src/rules/tests/mod.rs | 1 + .../src/rules/tests/recipes_only.rs | 42 +++++ .../src/rules/workflows.rs | 59 +++++++ .../sourceorigin-standard/src/rules/yaml.rs | 167 ++++++++++++++++++ 9 files changed, 444 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/recipes_only.rs create mode 100644 crates/sourceorigin-standard/src/rules/t3.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/recipes_only.rs create mode 100644 crates/sourceorigin-standard/src/rules/workflows.rs create mode 100644 crates/sourceorigin-standard/src/rules/yaml.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 8d33d41..9890810 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -50,6 +50,7 @@ A repository adopts the standard in four steps: - `deny-config`: `deny.toml` sets `unknown-registry` and `unknown-git` under `[sources]`, and `wildcards` under `[bans]`, to `"deny"`, allows at least one license, and gives every ignored advisory a `reason`: `{ id = "…", reason = "…" }`. - `anyhow-edges`: No library depends on `anyhow` (dev-dependencies aside), unless `standard.toml`'s `anyhow-crates` lists it as a binary's edge, such as a crate holding a CLI's commands. Each listed name is a workspace crate. - `recipes`: The justfile has the recipes `validate`, `fmt`, `fmt-check`, `lint`, `standard`, `test`, `doc`, `doc-test`, `audit`, `unused` and `check-changed`. `just` alone runs `validate` or `check-changed`; `validate` runs `fmt` first, then at least `lint`, `standard`, `test`, `doc`, `doc-test`, `audit` and `unused`, and never `fmt-check`; `test` takes `*ARGS`, defaulting to `--workspace`. +- `recipes-only`: Every command of `t3.json` runs `just`, and no line of a workflow's or composite action's `run:` starts with `cargo` unless `standard.toml`'s `cargo-in-ci` lists it; each listed line is one a `run:` holds. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and @@ -71,6 +72,10 @@ unsafe-modules = ["my_crate::ffi"] # a crate holding a CLI's commands. A crate with no library may anyway. anyhow-crates = ["my-cli"] +# The lines of workflows' `run:` that run Cargo rather than a recipe, each +# whole, such as a check on the minimum supported Rust version. +cargo-in-ci = ["cargo +1.85 check --locked --workspace"] + # Rules the repository departs from, each with its reason: everywhere, or # only in the files `paths` matches and the crates `crates` names. A # deviation, or an entry of its scope, that excuses no violation is one. diff --git a/crates/sourceorigin-standard/src/config/mod.rs b/crates/sourceorigin-standard/src/config/mod.rs index e837c13..ae52bd4 100644 --- a/crates/sourceorigin-standard/src/config/mod.rs +++ b/crates/sourceorigin-standard/src/config/mod.rs @@ -78,6 +78,9 @@ pub(crate) struct Config { pub(crate) comments: Comments, /// The libraries that may depend on `anyhow`, as the edge of a binary. pub(crate) anyhow_crates: Vec, + /// The lines of workflows' `run:` that may run Cargo rather than a + /// recipe. + pub(crate) cargo_in_ci: Vec, } /// The `[comments]` table: what comments the rules allow. @@ -227,6 +230,7 @@ struct File { unsafe_modules: Vec>, comments: Comments, anyhow_crates: Vec>, + cargo_in_ci: Vec>, } /// A deviation as written: its reason alone, for everywhere, or a table @@ -356,6 +360,7 @@ impl Config { unsafe_modules: listed(file.unsafe_modules), comments: file.comments, anyhow_crates: listed(file.anyhow_crates), + cargo_in_ci: listed(file.cargo_in_ci), }) } diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 9c68635..1b5b306 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -22,19 +22,23 @@ mod manifest; mod modules; mod nextest_config; mod recipes; +mod recipes_only; mod report; mod rustfmt_config; mod settings; mod source; +mod t3; #[cfg(test)] mod tests; mod text_file; mod toml_file; mod toolchain; mod unsafe_modules; +mod workflows; mod workspace_dependencies; mod workspace_lints; mod workspace_manifest; +mod yaml; use std::fmt; use std::path::{Path, PathBuf}; @@ -56,9 +60,11 @@ use nextest_config::NextestConfig; pub use report::{Report, RuleReport}; use settings::Settings; use source::{Source, Unparsed}; +use t3::T3; use text_file::TextFile; use toml_file::TomlFile; use toolchain::ToolchainFile; +use workflows::Workflow; use workspace_lints::LintLevel; /// Declares the rules: each variant's doc comment is the doc [`Rule::doc`] @@ -197,6 +203,11 @@ rules! { /// `doc`, `doc-test`, `audit` and `unused`, and never `fmt-check`; `test` /// takes `*ARGS`, defaulting to `--workspace`. Recipes = "recipes" => recipes, + /// Every command of `t3.json` runs `just`, and no line of a workflow's + /// or composite action's `run:` starts with `cargo` unless + /// `standard.toml`'s `cargo-in-ci` lists it; each listed line is one a + /// `run:` holds. + RecipesOnly = "recipes-only" => recipes_only, } /// Why the rules couldn't be checked. @@ -232,6 +243,24 @@ pub enum Error { /// `just --dump` printed what isn't a justfile's description. #[error("parse `just --dump --dump-format json`")] Justfile(#[source] serde_json::Error), + /// A workflow isn't valid YAML. + #[error("parse {}", path.display())] + Yaml { + /// The file, from the repository's root. + path: PathBuf, + /// Where the parser stopped, and why. + #[source] + source: yaml_rust2::ScanError, + }, + /// A JSON file, such as `t3.json`, isn't valid or has the wrong shape. + #[error("parse {}", path.display())] + Json { + /// The file, from the repository's root. + path: PathBuf, + /// Why it couldn't be parsed. + #[source] + source: serde_json::Error, + }, /// A manifest isn't valid TOML. #[error("{}: {message}", path.display())] Toml { @@ -377,6 +406,10 @@ struct Repository { deny: Option>, /// The justfile, as `just` describes it. justfile: Option, + /// `t3.json`. + t3: Option, + /// The workflows and composite actions. + workflows: Vec, } impl Repository { @@ -403,6 +436,8 @@ impl Repository { nextest: TomlFile::load_or_empty(root, nextest_config::PATH)?, deny: TomlFile::load(root, deny_config::PATH)?, justfile: Justfile::load(root)?, + t3: T3::load(root)?, + workflows: Workflow::load_all(root)?, metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/recipes_only.rs b/crates/sourceorigin-standard/src/rules/recipes_only.rs new file mode 100644 index 0000000..a67a34b --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/recipes_only.rs @@ -0,0 +1,68 @@ +//! `recipes-only`: T3 Code and CI run the justfile's recipes rather than +//! repeating their commands. + +use super::t3; +use super::{Finding, Repository}; +use crate::config; + +impl Repository { + /// Each `t3.json` command that isn't a recipe, each line of a workflow's + /// or action's `run:` starting with `cargo` that `cargo-in-ci` doesn't + /// list, and each listed line no `run:` holds. + pub(super) fn recipes_only(&self) -> Vec { + let mut findings = Vec::new(); + if let Some(t3) = &self.t3 { + findings.extend( + t3.scripts + .iter() + .filter(|script| script.command.split_whitespace().next() != Some("just")) + .map(|script| Finding { + path: t3::PATH.into(), + line: t3.line(script), + message: format!( + "`{}` isn't a recipe: add it to the justfile, and run it with `just`", + script.command + ), + }), + ); + } + let allowed = &self.config.cargo_in_ci; + let mut used = vec![false; allowed.len()]; + for workflow in &self.workflows { + let runs = workflow.steps().filter_map(|step| step.get("run")); + for (line, command) in runs.flat_map(|run| run.lines()) { + let command = command.trim(); + if command.split_whitespace().next() != Some("cargo") { + continue; + } + match allowed.iter().position(|listed| listed.value == command) { + Some(index) => used[index] = true, + None => findings.push(Finding { + path: workflow.path.clone(), + line, + message: format!( + "`{command}` runs Cargo rather than a recipe: run a recipe of the \ + justfile, or list the line in {}'s `cargo-in-ci` if no recipe fits", + config::PATH + ), + }), + } + } + } + findings.extend( + allowed + .iter() + .zip(used) + .filter(|(_, used)| !used) + .map(|(listed, _)| Finding { + path: config::PATH.into(), + line: listed.line, + message: format!( + "`cargo-in-ci` lists `{}`, which no workflow runs: remove it", + listed.value + ), + }), + ); + findings + } +} diff --git a/crates/sourceorigin-standard/src/rules/t3.rs b/crates/sourceorigin-standard/src/rules/t3.rs new file mode 100644 index 0000000..d198f52 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/t3.rs @@ -0,0 +1,62 @@ +//! `t3.json`: the scripts T3 Code runs in a worktree. + +use std::path::Path; + +use serde::Deserialize; + +use super::Error; + +/// The file, from the repository's root. +pub(super) const PATH: &str = "t3.json"; + +/// `t3.json`, with its text for the lines of its scripts. +pub(super) struct T3 { + text: String, + pub(super) scripts: Vec