From 12ea53c23f41ae9e9be5d5845ca628bc13d7fef7 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:31:27 +0100 Subject: [PATCH] feat: workspace-dependencies declares each dependency once, for the workspace --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/manifest.rs | 48 +++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 6 +++ .../src/rules/tests/mod.rs | 1 + .../src/rules/tests/workspace_dependencies.rs | 33 ++++++++++++ .../src/rules/workspace_dependencies.rs | 53 +++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/tests/workspace_dependencies.rs create mode 100644 crates/sourceorigin-standard/src/rules/workspace_dependencies.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 4e9ad2d..8accb85 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -46,6 +46,7 @@ A repository adopts the standard in four steps: - `rustfmt-config`: `rustfmt.toml` sets `edition = "2024"`, `newline_style = "Unix"` and `use_field_init_shorthand = true`. - `clippy-config`: `clippy.toml` sets `allow-expect-in-tests`, `allow-panic-in-tests` and `allow-dbg-in-tests` to `true`, and never `allow-unwrap-in-tests`. - `nextest-config`: `.config/nextest.toml`'s default profile sets a `slow-timeout`, every `slow-timeout` in it has a `terminate-after`, and no profile or override sets `retries`. +- `workspace-dependencies`: Every dependency of a workspace crate, of every kind and target, is inherited with `workspace = true` from the root's `[workspace.dependencies]`, where each `path` dependency also has a `version`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/manifest.rs b/crates/sourceorigin-standard/src/rules/manifest.rs index fbd40d1..fe77782 100644 --- a/crates/sourceorigin-standard/src/rules/manifest.rs +++ b/crates/sourceorigin-standard/src/rules/manifest.rs @@ -26,6 +26,9 @@ pub(super) struct RootManifest { #[derive(Default, Deserialize)] pub(super) struct Workspace { pub(super) resolver: Option>, + /// The dependencies crates inherit, each with its requirement. + #[serde(default)] + pub(super) dependencies: DependencyTable, pub(super) package: Option>, /// Each tool's lints, such as `clippy`'s, each with its setting. #[serde(default)] @@ -47,11 +50,56 @@ pub(super) struct WorkspacePackage { pub(super) edition: Option>, } +/// A table of dependencies, each a version or a table. +pub(super) type DependencyTable = BTreeMap, toml::Value>; + /// A workspace crate's manifest. #[derive(Deserialize)] pub(super) struct Manifest { pub(super) package: Option>, pub(super) lints: Option>, + #[serde(default)] + dependencies: DependencyTable, + #[serde(default, rename = "dev-dependencies")] + dev_dependencies: DependencyTable, + #[serde(default, rename = "build-dependencies")] + build_dependencies: DependencyTable, + /// Each target's own dependencies, such as `cfg(unix)`'s. + #[serde(default)] + target: BTreeMap, +} + +/// A target's tables of dependencies, of every kind. +#[derive(Default, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub(super) struct DependencyTables { + #[serde(default)] + dependencies: DependencyTable, + #[serde(default)] + dev_dependencies: DependencyTable, + #[serde(default)] + build_dependencies: DependencyTable, +} + +impl Manifest { + /// Every dependency it declares, of every kind and target. + pub(super) fn all_dependencies( + &self, + ) -> impl Iterator, &toml::Value)> { + let own = [ + &self.dependencies, + &self.dev_dependencies, + &self.build_dependencies, + ]; + let targets = self.target.values().flat_map(|target| { + [ + &target.dependencies, + &target.dev_dependencies, + &target.build_dependencies, + ] + }); + own.into_iter().chain(targets).flatten() + } } /// A crate's `[package]`. diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index b089499..5cf4e82 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -28,6 +28,7 @@ mod text_file; mod toml_file; mod toolchain; mod unsafe_modules; +mod workspace_dependencies; mod workspace_lints; mod workspace_manifest; @@ -168,6 +169,11 @@ rules! { /// `slow-timeout` in it has a `terminate-after`, and no profile or /// override sets `retries`. NextestConfig = "nextest-config" => nextest_config, + /// Every dependency of a workspace crate, of every kind and target, is + /// inherited with `workspace = true` from the root's + /// `[workspace.dependencies]`, where each `path` dependency also has a + /// `version`. + WorkspaceDependencies = "workspace-dependencies" => workspace_dependencies, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 76bf1ee..4f2f5ff 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -16,6 +16,7 @@ mod report; mod rustfmt_config; mod toolchain; mod unsafe_modules; +mod workspace_dependencies; mod workspace_lints; mod workspace_manifest; diff --git a/crates/sourceorigin-standard/src/rules/tests/workspace_dependencies.rs b/crates/sourceorigin-standard/src/rules/tests/workspace_dependencies.rs new file mode 100644 index 0000000..6416ff9 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/workspace_dependencies.rs @@ -0,0 +1,33 @@ +use super::Fixture; +use crate::rules::Rule; + +#[test] +fn dependencies_are_declared_once_for_the_workspace() { + let fixture = Fixture::new(); + fixture + .write( + "Cargo.toml", + "[workspace]\nmembers = [\"crates/*\"]\n\n[workspace.dependencies]\n\ + core = { path = \"crates/core\" }\nserde = \"1\"\n", + ) + .add_crate("core", "") + .add_crate( + "app", + "\n[dependencies]\ncore.workspace = true\nserde = { workspace = true, features \ + = [\"derive\"] }\n\n[dev-dependencies]\ntempfile = \"3\"\n\n\ + [target.'cfg(unix)'.dependencies]\nlibc = { version = \"0.2\" }\n", + ); + assert_eq!( + fixture.violations(Rule::WorkspaceDependencies), + [ + "Cargo.toml:5: `core` has a `path` and no `version`: add the crate's version, so a \ + dependent can be published (workspace-dependencies)", + "crates/app/Cargo.toml:10: `tempfile` is declared here: declare it once in the root's \ + `[workspace.dependencies]`, and inherit it with `tempfile.workspace = true` \ + (workspace-dependencies)", + "crates/app/Cargo.toml:13: `libc` is declared here: declare it once in the root's \ + `[workspace.dependencies]`, and inherit it with `libc.workspace = true` \ + (workspace-dependencies)", + ] + ); +} diff --git a/crates/sourceorigin-standard/src/rules/workspace_dependencies.rs b/crates/sourceorigin-standard/src/rules/workspace_dependencies.rs new file mode 100644 index 0000000..6db1e79 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/workspace_dependencies.rs @@ -0,0 +1,53 @@ +//! `workspace-dependencies`: each dependency is declared once, for the +//! whole workspace. + +use super::manifest::ROOT; +use super::{Finding, Repository}; + +impl Repository { + /// Each dependency a crate declares without `workspace = true`, and + /// each path dependency of `[workspace.dependencies]` without a + /// `version`. + pub(super) fn workspace_dependencies(&self) -> Vec { + let mut findings: Vec = self + .manifests + .iter() + .flat_map(|manifest| { + manifest + .value + .all_dependencies() + .filter(|(_, value)| { + value.get("workspace").and_then(toml::Value::as_bool) != Some(true) + }) + .map(move |(name, _)| Finding { + path: manifest.path.clone(), + line: manifest.line(name), + message: format!( + "`{name}` is declared here: declare it once in the root's \ + `[workspace.dependencies]`, and inherit it with \ + `{name}.workspace = true`", + name = name.get_ref() + ), + }) + }) + .collect(); + let root = &self.root_manifest; + findings.extend( + root.value + .workspace + .dependencies + .iter() + .filter(|(_, value)| value.get("path").is_some() && value.get("version").is_none()) + .map(|(name, _)| Finding { + path: ROOT.into(), + line: root.line(name), + message: format!( + "`{}` has a `path` and no `version`: add the crate's version, so a \ + dependent can be published", + name.get_ref() + ), + }), + ); + findings + } +}