Skip to content

Commit

Permalink
Support workspace deps in the root crate of a workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Dec 19, 2023
1 parent 555274d commit 0f3c27e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ fn read_cargo_toml(

let workspace_dependencies = if manifest_path != workspace_manifest_path {
let workspace_manifest = open_cargo_toml(workspace_manifest_path)?;
extract_workspace_dependencies(workspace_manifest)?
extract_workspace_dependencies(&workspace_manifest)?
} else {
Default::default()
extract_workspace_dependencies(&manifest)?
};

let crate_names = extract_crate_names(&manifest, workspace_dependencies)?;
Expand All @@ -289,7 +289,7 @@ fn read_cargo_toml(
/// Returns a hash map that maps from dep name to the package name. Dep name
/// and package name can be the same if there doesn't exist any rename.
fn extract_workspace_dependencies(
workspace_toml: Document,
workspace_toml: &Document,
) -> Result<BTreeMap<String, String>, Error> {
Ok(workspace_dep_tables(&workspace_toml)
.into_iter()
Expand Down Expand Up @@ -352,7 +352,7 @@ fn extract_crate_names(
let workspace = dep_value.get("workspace").and_then(|w| w.as_bool()).unwrap_or_default();

let pkg_name = workspace
.then(|| workspace_dependencies.get(pkg_name).map(|p| p.as_ref()))
.then(|| workspace_dependencies.get(dep_name).map(|p| p.as_ref()))
.flatten()
.unwrap_or(pkg_name);

Expand Down Expand Up @@ -400,7 +400,7 @@ mod tests {
let workspace_cargo_toml = $workspace_toml.parse::<Document>()
.expect("Parses workspace `Cargo.toml`");

let workspace_deps = extract_workspace_dependencies(workspace_cargo_toml)
let workspace_deps = extract_workspace_dependencies(&workspace_cargo_toml)
.expect("Extracts workspace dependencies");

match extract_crate_names(&cargo_toml, workspace_deps)
Expand Down Expand Up @@ -539,17 +539,4 @@ mod tests {
"#,
Ok(Some(FoundCrate::Name(name))) if name == "my_crate_cool"
}

create_test! {
workspace_deps_twice_renamed,
r#"
[dependencies]
my_crate_cool_renamed = { package = "my-crate-cool", workspace = true }
"#,
r#"
[workspace.dependencies]
my-crate-cool = { package = "my_crate" }
"#,
Ok(Some(FoundCrate::Name(name))) if name == "my_crate_cool_renamed"
}
}
8 changes: 8 additions & 0 deletions tests/workspace_deps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[package]
name = "test-workspace-root-crate"
version = "0.1.0"
edition = "2021"

[workspace]
members = ["my-cool-dep", "test-crate"]
resolver = "2"
Expand All @@ -6,3 +11,6 @@ resolver = "2"
[workspace.dependencies]
my-cool-dep = { package = "my-cool-dep-real-name", path = "my-cool-dep" }
proc-macro-crate = { path = "../.." }

[dependencies]
my-cool-dep = { workspace = true }
3 changes: 3 additions & 0 deletions tests/workspace_deps/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn use_it() {
my_cool_dep::do_something!()
}

0 comments on commit 0f3c27e

Please sign in to comment.