Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support workspace dependencies in the root crate of a workspace #46

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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()))
bkchr marked this conversation as resolved.
Show resolved Hide resolved
.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!()
}
Loading