From 0f3c27ee8e3792d818378dbfee63e34733b0226a Mon Sep 17 00:00:00 2001 From: Waridley Date: Fri, 15 Dec 2023 21:03:20 -0600 Subject: [PATCH 1/2] Support workspace deps in the root crate of a workspace --- src/lib.rs | 23 +++++------------------ tests/workspace_deps/Cargo.toml | 8 ++++++++ tests/workspace_deps/src/lib.rs | 3 +++ 3 files changed, 16 insertions(+), 18 deletions(-) create mode 100644 tests/workspace_deps/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index e4e0043..2fe469f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)?; @@ -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, Error> { Ok(workspace_dep_tables(&workspace_toml) .into_iter() @@ -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); @@ -400,7 +400,7 @@ mod tests { let workspace_cargo_toml = $workspace_toml.parse::() .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) @@ -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" - } } diff --git a/tests/workspace_deps/Cargo.toml b/tests/workspace_deps/Cargo.toml index 7e32377..f7eee0d 100644 --- a/tests/workspace_deps/Cargo.toml +++ b/tests/workspace_deps/Cargo.toml @@ -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" @@ -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 } diff --git a/tests/workspace_deps/src/lib.rs b/tests/workspace_deps/src/lib.rs new file mode 100644 index 0000000..9d97cfd --- /dev/null +++ b/tests/workspace_deps/src/lib.rs @@ -0,0 +1,3 @@ +pub fn use_it() { + my_cool_dep::do_something!() +} From 15534c7610b2e3d4649fd01bf6f8d4f7ea113626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 17 Jan 2024 20:15:00 +0100 Subject: [PATCH 2/2] Update src/lib.rs --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2fe469f..810faaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(dep_name).map(|p| p.as_ref())) + .then(|| workspace_dependencies.get(pkg_name).map(|p| p.as_ref())) .flatten() .unwrap_or(pkg_name);