Skip to content

Commit 58919a5

Browse files
committed
Match dependency kind in find_resolved_dep to avoid inlining wrong spec
find_resolved_dep previously matched only by name/target, ignoring DependencyKind. When a crate declares the same dep in multiple tables (e.g. [dependencies] and [dev-dependencies]) with different settings, the wrong entry could be returned. Add a kind parameter to find_resolved_dep and pass the appropriate DependencyKind (derived from the dep_kind table name) from both resolve_workspace_deps and the target-specific loop. Also add a workspace dev-dependency (log) to the test crate to cover the dev-dependencies path, and update pyo3 to 0.27.0 to match other test crates.
1 parent 0ba82d3 commit 58919a5

6 files changed

Lines changed: 44 additions & 16 deletions

File tree

src/source_distribution/cargo_toml_rewrite.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::PathDependency;
22
use super::utils::{normalize_path, relative_path};
33
use anyhow::{Context, Result};
4+
use cargo_metadata::DependencyKind;
45
use fs_err as fs;
56
use path_slash::PathExt as _;
67
use std::collections::{HashMap, HashSet};
@@ -226,9 +227,12 @@ pub(super) fn resolve_workspace_inheritance(
226227
if !is_workspace {
227228
continue;
228229
}
229-
if let Some(resolved_dep) =
230-
find_resolved_dep(resolved, &dep_name, Some(&target_key))
231-
{
230+
if let Some(resolved_dep) = find_resolved_dep(
231+
resolved,
232+
&dep_name,
233+
Some(&target_key),
234+
dep_kind_from_str(dep_kind),
235+
) {
232236
deps.insert(&dep_name, resolved_dep_to_toml(&resolved_dep));
233237
} else {
234238
debug!(
@@ -277,7 +281,9 @@ fn resolve_workspace_deps(
277281
continue;
278282
}
279283

280-
if let Some(resolved_dep) = find_resolved_dep(resolved, &dep_name, None) {
284+
if let Some(resolved_dep) =
285+
find_resolved_dep(resolved, &dep_name, None, dep_kind_from_str(dep_kind))
286+
{
281287
let new_entry = resolved_dep_to_toml(&resolved_dep);
282288
if let Some(deps_table) = document.get_mut(dep_kind).and_then(|t| t.as_table_mut()) {
283289
deps_table.insert(&dep_name, new_entry);
@@ -288,18 +294,32 @@ fn resolve_workspace_deps(
288294
}
289295
}
290296

297+
/// Maps a Cargo.toml dependency table name to the corresponding `DependencyKind`.
298+
fn dep_kind_from_str(dep_kind: &str) -> DependencyKind {
299+
match dep_kind {
300+
"dev-dependencies" => DependencyKind::Development,
301+
"build-dependencies" => DependencyKind::Build,
302+
_ => DependencyKind::Normal,
303+
}
304+
}
305+
291306
/// Finds a resolved dependency by name in the package metadata.
292307
fn find_resolved_dep(
293308
resolved: &cargo_metadata::Package,
294309
name: &str,
295310
target: Option<&str>,
311+
kind: DependencyKind,
296312
) -> Option<ResolvedDep> {
297313
resolved.dependencies.iter().find_map(|d| {
298314
// Match by name, considering renames
299315
let matches_name = d.rename.as_deref() == Some(name) || d.name == name;
300316
if !matches_name {
301317
return None;
302318
}
319+
// Match by dependency kind
320+
if d.kind != kind {
321+
return None;
322+
}
303323
// If a target is specified, match against it
304324
if let Some(target_str) = target
305325
&& d.target.as_ref().map(|t| t.to_string()).as_deref() != Some(target_str)

test-crates/parent_workspace_sdist/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ resolver = "2"
66
[workspace.package]
77
edition = "2021"
88
readme = "README.md"
9+
10+
[workspace.dependencies]
11+
log = "0.4"

test-crates/parent_workspace_sdist/crates/pysof/Cargo.lock

Lines changed: 10 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-crates/parent_workspace_sdist/crates/pysof/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ edition = "2021"
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
pyo3 = { version = "0.25.0", features = ["extension-module"] }
14+
pyo3 = { version = "0.27.0", features = ["extension-module"] }
1515
shared_crate = { path = "../shared_crate" }

test-crates/parent_workspace_sdist/crates/shared_crate/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ edition.workspace = true
55
readme.workspace = true
66

77
[lib]
8+
9+
[dev-dependencies]
10+
log = { workspace = true }

tests/run.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,9 @@ fn lib_with_parent_workspace_path_dep_sdist() {
909909
readme = "README.md"
910910
911911
[lib]
912+
913+
[dev-dependencies]
914+
log = "^0.4"
912915
"#]];
913916
handle_result(other::test_source_distribution(
914917
"test-crates/parent_workspace_sdist/crates/pysof",

0 commit comments

Comments
 (0)