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 @@ -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`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
48 changes: 48 additions & 0 deletions crates/sourceorigin-standard/src/rules/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub(super) struct RootManifest {
#[derive(Default, Deserialize)]
pub(super) struct Workspace {
pub(super) resolver: Option<Spanned<String>>,
/// The dependencies crates inherit, each with its requirement.
#[serde(default)]
pub(super) dependencies: DependencyTable,
pub(super) package: Option<Spanned<WorkspacePackage>>,
/// Each tool's lints, such as `clippy`'s, each with its setting.
#[serde(default)]
Expand All @@ -47,11 +50,56 @@ pub(super) struct WorkspacePackage {
pub(super) edition: Option<Spanned<String>>,
}

/// A table of dependencies, each a version or a table.
pub(super) type DependencyTable = BTreeMap<Spanned<String>, toml::Value>;

/// A workspace crate's manifest.
#[derive(Deserialize)]
pub(super) struct Manifest {
pub(super) package: Option<Spanned<Package>>,
pub(super) lints: Option<Spanned<toml::Table>>,
#[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<String, DependencyTables>,
}

/// 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<Item = (&Spanned<String>, &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]`.
Expand Down
6 changes: 6 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod text_file;
mod toml_file;
mod toolchain;
mod unsafe_modules;
mod workspace_dependencies;
mod workspace_lints;
mod workspace_manifest;

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod report;
mod rustfmt_config;
mod toolchain;
mod unsafe_modules;
mod workspace_dependencies;
mod workspace_lints;
mod workspace_manifest;

Expand Down
Original file line number Diff line number Diff line change
@@ -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)",
]
);
}
53 changes: 53 additions & 0 deletions crates/sourceorigin-standard/src/rules/workspace_dependencies.rs
Original file line number Diff line number Diff line change
@@ -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<Finding> {
let mut findings: Vec<Finding> = 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
}
}
Loading