Skip to content

Commit

Permalink
feat: support workspace lints (ipetkov#481)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Roman Volosatovs <[email protected]>
Co-authored-by: Ivan Petkov <[email protected]>
  • Loading branch information
rvolosatovs and ipetkov authored Dec 13, 2023
1 parent 62fc1a0 commit 33dbb6a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* **Breaking**: dropped compatibility for Nix versions below 2.18.1
* **Breaking**: dropped compatibility for nixpkgs-23.05.

### Fixed
* Workspace inheritance of `lints` in git dependencies is now correctly handled

## [0.15.1] - 2023-11-30

### Changed
Expand Down
52 changes: 45 additions & 7 deletions pkgs/crane-utils/src/bin/crane-resolve-workspace-inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,29 @@ fn merge(cargo_toml: &mut toml_edit::Document, root: &toml_edit::Document) {
};

// https://doc.rust-lang.org/cargo/reference/workspaces.html#workspaces
for (key, ws_key) in [
("package", "package"),
("dependencies", "dependencies"),
("dev-dependencies", "dependencies"),
("build-dependencies", "dependencies"),
for (key, ws_key, inherit) in [
("package", "package", false),
("dependencies", "dependencies", false),
("dev-dependencies", "dependencies", false),
("build-dependencies", "dependencies", false),
("lints", "lints", true),
] {
if let Some((cargo_toml, root)) = cargo_toml.get_mut(key).zip(w.get(ws_key)) {
try_merge_cargo_tables(cargo_toml, root);
if inherit {
try_inherit_cargo_table(cargo_toml, root);
} else {
try_merge_cargo_tables(cargo_toml, root);
}
};

if let Some(targets) = cargo_toml.get_mut("target").and_then(try_as_table_like_mut) {
for (_, tp) in targets.iter_mut() {
if let Some((cargo_toml, root)) = tp.get_mut(key).zip(w.get(ws_key)) {
try_merge_cargo_tables(cargo_toml, root);
if inherit {
try_inherit_cargo_table(cargo_toml, root);
} else {
try_merge_cargo_tables(cargo_toml, root);
}
}
}
}
Expand All @@ -128,6 +137,21 @@ fn try_as_table_like_mut(item: &mut Item) -> Option<&mut dyn toml_edit::TableLik
}
}

/// Inherit the specified `cargo_toml` from workspace `root` if the former is a table
fn try_inherit_cargo_table(cargo_toml: &mut Item, root: &Item) {
let Some(t) = try_as_table_like_mut(cargo_toml) else {
return;
};
if t.get("workspace")
.and_then(Item::as_bool)
.unwrap_or_default()
{
t.remove("workspace");
let orig_val = mem::replace(cargo_toml, root.clone());
merge_items(cargo_toml, orig_val);
}
}

/// Merge the specified `cargo_toml` and workspace `root` if both are tables
fn try_merge_cargo_tables(cargo_toml: &mut Item, root: &Item) {
let cargo_toml = try_as_table_like_mut(cargo_toml);
Expand Down Expand Up @@ -346,6 +370,9 @@ mod tests {
[features]
# this feature is a demonstration that comments are preserved
my_feature = []
[lints]
workspace = true
"#,
)
.unwrap();
Expand Down Expand Up @@ -381,6 +408,11 @@ mod tests {
waldo = { version = "waldo-workspace-vers" }
unix = { version = "unix-vers" }
[workspace.lints.rust]
unused_extern_crates = 'warn'
[workspace.lints.clippy]
all = 'allow'
"#,
)
.unwrap();
Expand Down Expand Up @@ -420,6 +452,9 @@ mod tests {
[target.'cfg(unix)'.dependencies]
unix = { version = "unix-vers" , features = ["some"] }
[lints.rust]
unused_extern_crates = 'warn'
[dev-dependencies]
foo= { version = "foo-vers" }
bar= { version = "bar-vers", default-features = false }
Expand All @@ -430,6 +465,9 @@ mod tests {
garply = "garply-vers"
waldo = "waldo-vers"
[lints.clippy]
all = 'allow'
[build-dependencies]
foo= { version = "foo-vers" }
bar= { version = "bar-vers", default-features = false }
Expand Down

0 comments on commit 33dbb6a

Please sign in to comment.