Skip to content
Draft
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
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>/SKILL.md`, its frontmatter's `name` is `<name>`, 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.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
8 changes: 8 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -290,6 +295,8 @@ struct Repository {
metadata: Metadata,
/// The root manifest.
root_manifest: TomlFile<RootManifest>,
/// `rust-toolchain.toml`.
toolchain: Option<TomlFile<ToolchainFile>>,
/// `.cargo/config.toml`.
cargo_config: Option<TomlFile<CargoConfig>>,
/// The manifests of the workspace's crates.
Expand Down Expand Up @@ -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)?,
Expand Down
15 changes: 11 additions & 4 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod framework;
mod generated_lints;
mod layout;
mod report;
mod toolchain;
mod unsafe_modules;
mod workspace_lints;
mod workspace_manifest;
Expand Down Expand Up @@ -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";
38 changes: 38 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/toolchain.rs
Original file line number Diff line number Diff line change
@@ -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::<String>::new());
}
97 changes: 97 additions & 0 deletions crates/sourceorigin-standard/src/rules/toolchain.rs
Original file line number Diff line number Diff line change
@@ -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<Spanned<Toolchain>>,
}

/// Its `[toolchain]`.
#[derive(Deserialize)]
struct Toolchain {
channel: Option<Spanned<String>>,
components: Option<Spanned<Vec<String>>>,
}

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::<toml_edit::Array>());
},
)],
};
};
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()
}
}