diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 4fd0e5a..ff76ab8 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -42,6 +42,7 @@ A repository adopts the standard in four steps: - `cargo-locked`: Every `cargo build`, `check`, `clippy`, `doc`, `llvm-cov`, `nextest`, `publish`, `run` and `test` in a justfile (and `*.just`), `.github` YAML file or `t3.json` passes `--locked` (or `--frozen`). - `agent-files`: `AGENTS.md` sits at the root, and no `CLAUDE.md` exists. Each skill is `.agents/skills//SKILL.md`, its frontmatter's `name` is ``, and `.claude/skills` is a symlink to `../.agents/skills`. - `agents-md`: AGENTS.md names every workspace crate, by its name in backticks or its directory, and holds at most 150 lines. +- `toolchain`: `rust-toolchain.toml` pins `channel = "stable"` and installs at least the `rustfmt` and `clippy` components. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index aad4ffe..2a703b5 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -22,6 +22,7 @@ mod source; mod tests; mod text_file; mod toml_file; +mod toolchain; mod unsafe_modules; mod workspace_lints; mod workspace_manifest; @@ -44,6 +45,7 @@ pub use report::{Report, RuleReport}; use source::{Source, Unparsed}; use text_file::TextFile; use toml_file::TomlFile; +use toolchain::ToolchainFile; use workspace_lints::LintLevel; /// Declares the rules: each variant's doc comment is the doc [`Rule::doc`] @@ -146,6 +148,9 @@ rules! { /// AGENTS.md names every workspace crate, by its name in backticks or /// its directory, and holds at most 150 lines. AgentsMd = "agents-md" => agents_md, + /// `rust-toolchain.toml` pins `channel = "stable"` and installs at + /// least the `rustfmt` and `clippy` components. + Toolchain = "toolchain" => toolchain, } /// Why the rules couldn't be checked. @@ -290,6 +295,8 @@ struct Repository { metadata: Metadata, /// The root manifest. root_manifest: TomlFile, + /// `rust-toolchain.toml`. + toolchain: Option>, /// `.cargo/config.toml`. cargo_config: Option>, /// The manifests of the workspace's crates. @@ -326,6 +333,7 @@ impl Repository { lint_levels: LintLevel::all(&root_manifest), root_manifest, cargo_config: TomlFile::load(root, layout::CARGO_CONFIG)?, + toolchain: TomlFile::load(root, toolchain::PATH)?, modules: Modules::of(&metadata, &sources), budget: Budget::load(root)?, commented: Commented::load_all(root)?, diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index a99256d..a1f95e8 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -11,6 +11,7 @@ mod framework; mod generated_lints; mod layout; mod report; +mod toolchain; mod unsafe_modules; mod workspace_lints; mod workspace_manifest; @@ -105,10 +106,16 @@ self_named_module_files = \"warn\" /// The configuration files every fixture holds, as the standard has them, /// so a rule's fixes touch only what a test writes. -const CONFIG: [(&str, &str); 1] = [( - ".cargo/config.toml", - "[alias]\nxtask = \"run --locked --package xtask --\"\n", -)]; +const CONFIG: [(&str, &str); 2] = [ + ( + ".cargo/config.toml", + "[alias]\nxtask = \"run --locked --package xtask --\"\n", + ), + ( + "rust-toolchain.toml", + "[toolchain]\nchannel = \"stable\"\ncomponents = [\"rustfmt\", \"clippy\"]\n", + ), +]; /// A manifest tail inheriting the workspace's lints. const INHERITS: &str = "[lints]\nworkspace = true\n"; diff --git a/crates/sourceorigin-standard/src/rules/tests/toolchain.rs b/crates/sourceorigin-standard/src/rules/tests/toolchain.rs new file mode 100644 index 0000000..432f1f9 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/toolchain.rs @@ -0,0 +1,38 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn the_toolchain_is_stable_with_rustfmt_and_clippy() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "rust-toolchain.toml", + "[toolchain]\nchannel = \"1.85\"\ncomponents = [\"clippy\", \"llvm-tools\"]\n\ + targets = [\"wasm32-unknown-unknown\"]\n", + ); + assert_eq!( + fixture.violations(Rule::Toolchain), + [ + "rust-toolchain.toml:2: the toolchain isn't the `stable` channel: set `channel = \ + \"stable\"`, which rustup keeps current (toolchain)", + "rust-toolchain.toml:3: the toolchain lacks the `rustfmt` component: add it to \ + `components` (toolchain)", + ] + ); +} + +#[test] +fn fix_writes_a_missing_toolchain_file() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .remove("rust-toolchain.toml"); + assert_eq!( + fixture.violations(Rule::Toolchain), + [ + "rust-toolchain.toml:1: no rust-toolchain.toml: pin the `stable` channel there \ + (toolchain)" + ] + ); + fixture.fix_rule(Rule::Toolchain); + assert_eq!(fixture.violations(Rule::Toolchain), Vec::::new()); +} diff --git a/crates/sourceorigin-standard/src/rules/toolchain.rs b/crates/sourceorigin-standard/src/rules/toolchain.rs new file mode 100644 index 0000000..773384a --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/toolchain.rs @@ -0,0 +1,97 @@ +//! `toolchain`: every repository builds with stable Rust, formatted and +//! linted. + +use serde::Deserialize; +use toml::Spanned; + +use super::fix::{Checked, Fix}; +use super::{Finding, Repository}; + +/// The toolchain file, from the repository's root. +pub(super) const PATH: &str = "rust-toolchain.toml"; + +/// The channel it pins. +const CHANNEL: &str = "stable"; + +/// The components it installs, among others. +const COMPONENTS: [&str; 2] = ["rustfmt", "clippy"]; + +/// `rust-toolchain.toml`. +#[derive(Deserialize)] +pub(super) struct ToolchainFile { + toolchain: Option>, +} + +/// Its `[toolchain]`. +#[derive(Deserialize)] +struct Toolchain { + channel: Option>, + components: Option>>, +} + +impl Repository { + /// A missing toolchain file, which the fix writes, a channel other than + /// stable, and each required component it doesn't install. + pub(super) fn toolchain(&self) -> Checked { + let Some(file) = &self.toolchain else { + return Checked { + findings: vec![Finding { + path: PATH.into(), + line: 1, + message: format!("no {PATH}: pin the `{CHANNEL}` channel there"), + }], + fixes: vec![Fix::new( + PATH, + format!("pin the `{CHANNEL}` toolchain"), + |document| { + document["toolchain"]["channel"] = toml_edit::value(CHANNEL); + document["toolchain"]["components"] = + toml_edit::value(COMPONENTS.into_iter().collect::()); + }, + )], + }; + }; + let finding = |line, message| Finding { + path: PATH.into(), + line, + message, + }; + let Some(toolchain) = &file.value.toolchain else { + return vec![finding( + 1, + format!("no `[toolchain]`: add one with `channel = \"{CHANNEL}\"`"), + )] + .into(); + }; + let mut findings = Vec::new(); + let channel = &toolchain.get_ref().channel; + match channel { + Some(channel) if channel.get_ref() == CHANNEL => {} + _ => findings.push(finding( + channel + .as_ref() + .map_or_else(|| file.line(toolchain), |channel| file.line(channel)), + format!( + "the toolchain isn't the `{CHANNEL}` channel: set `channel = \"{CHANNEL}\"`, \ + which rustup keeps current" + ), + )), + } + let components = toolchain.get_ref().components.as_ref(); + let line = components.map_or_else(|| file.line(toolchain), |list| file.line(list)); + findings.extend( + COMPONENTS + .iter() + .filter(|&&component| { + !components.is_some_and(|list| list.get_ref().iter().any(|c| c == component)) + }) + .map(|component| { + finding( + line, + format!("the toolchain lacks the `{component}` component: add it to `components`"), + ) + }), + ); + findings.into() + } +}