Skip to content

Commit 1569c49

Browse files
committed
Fix #10680, Fix #13702
1 parent 9c4c4d1 commit 1569c49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+158
-11
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ fn resolve_dependency(
361361
};
362362
selected_dep = populate_dependency(selected_dep, arg);
363363

364-
let old_dep = get_existing_dependency(manifest, selected_dep.toml_key(), section)?;
364+
let lookup = |dep_key: &_| get_existing_dependency(manifest, dep_key, section);
365+
let old_dep = fuzzy_lookup(&mut selected_dep, lookup, gctx)?;
366+
365367
let mut dependency = if let Some(mut old_dep) = old_dep.clone() {
366368
if old_dep.name != selected_dep.name {
367369
// Assuming most existing keys are not relevant when the package changes
@@ -383,7 +385,8 @@ fn resolve_dependency(
383385
if dependency.source().is_none() {
384386
// Checking for a workspace dependency happens first since a member could be specified
385387
// in the workspace dependencies table as a dependency
386-
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() {
388+
let lookup = |toml_key: &_| Ok(find_workspace_dep(toml_key, ws.root_manifest()).ok());
389+
if let Some(_dep) = fuzzy_lookup(&mut dependency, lookup, gctx)? {
387390
dependency = dependency.set_source(WorkspaceSource::new());
388391
} else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
389392
// Only special-case workspaces when the user doesn't provide any extra
@@ -449,6 +452,42 @@ fn resolve_dependency(
449452
Ok(dependency)
450453
}
451454

455+
fn fuzzy_lookup(
456+
dependency: &mut Dependency,
457+
lookup: impl Fn(&str) -> CargoResult<Option<Dependency>>,
458+
gctx: &GlobalContext,
459+
) -> CargoResult<Option<Dependency>> {
460+
if let Some(rename) = dependency.rename() {
461+
// If it has a rename only lookup that name - analogue to Dependency::toml_key()
462+
return lookup(rename);
463+
}
464+
465+
for name_permutation in [
466+
dependency.name.clone(),
467+
dependency.name.replace('-', "_"),
468+
dependency.name.replace('_', "-"),
469+
].into_iter().dedup() {
470+
let Some(dep) = lookup(&name_permutation)? else {
471+
continue;
472+
};
473+
474+
if dependency.name != name_permutation {
475+
// Ignore fuzzy matches with non-registry sources
476+
if !matches!(dep.source, Some(Source::Registry(_))) {
477+
continue;
478+
}
479+
gctx.shell().warn(format!(
480+
"translating `{}` to `{}`",
481+
dependency.name, &name_permutation,
482+
))?;
483+
dependency.name = name_permutation;
484+
}
485+
return Ok(Some(dep));
486+
}
487+
488+
Ok(None)
489+
}
490+
452491
/// When { workspace = true } you cannot define other keys that configure
453492
/// the source of the dependency such as `version`, `registry`, `registry-index`,
454493
/// `path`, `git`, `branch`, `tag`, `rev`, or `package`. You can also not define

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["primary", "fuzzy_name"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "fuzzy_name"
3+
version = "0.0.0"
4+
edition = "2015"

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/fuzzy_name/src/lib.rs

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "bar"
3+
version = "0.0.0"
4+
edition = "2015"
5+
6+
[dependencies]
7+
fuzzy_name = { path = "../fuzzy_name" }

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/primary/src/lib.rs

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_fingerprint":406425066802700236,"outputs":{"16968705982730379203":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/I563472/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"adx\"\ntarget_feature=\"aes\"\ntarget_feature=\"avx\"\ntarget_feature=\"avx2\"\ntarget_feature=\"avx512bitalg\"\ntarget_feature=\"avx512bw\"\ntarget_feature=\"avx512cd\"\ntarget_feature=\"avx512dq\"\ntarget_feature=\"avx512f\"\ntarget_feature=\"avx512ifma\"\ntarget_feature=\"avx512vbmi\"\ntarget_feature=\"avx512vbmi2\"\ntarget_feature=\"avx512vl\"\ntarget_feature=\"avx512vnni\"\ntarget_feature=\"avx512vp2intersect\"\ntarget_feature=\"avx512vpopcntdq\"\ntarget_feature=\"bmi1\"\ntarget_feature=\"bmi2\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"ermsb\"\ntarget_feature=\"f16c\"\ntarget_feature=\"fma\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"gfni\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"lzcnt\"\ntarget_feature=\"movbe\"\ntarget_feature=\"pclmulqdq\"\ntarget_feature=\"popcnt\"\ntarget_feature=\"prfchw\"\ntarget_feature=\"rdrand\"\ntarget_feature=\"rdseed\"\ntarget_feature=\"sha\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"sse4.1\"\ntarget_feature=\"sse4.2\"\ntarget_feature=\"ssse3\"\ntarget_feature=\"vaes\"\ntarget_feature=\"vpclmulqdq\"\ntarget_feature=\"xsave\"\ntarget_feature=\"xsavec\"\ntarget_feature=\"xsaveopt\"\ntarget_feature=\"xsaves\"\ntarget_has_atomic\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"unknown\"\nub_checks\nunix\n","stderr":""},"11510009828276671329":{"success":true,"status":"","code":0,"stdout":"rustc 1.82.0-nightly (100fde524 2024-08-28)\nbinary: rustc\ncommit-hash: 100fde5246bf56f22fb5cc85374dd841296fce0e\ncommit-date: 2024-08-28\nhost: x86_64-unknown-linux-gnu\nrelease: 1.82.0-nightly\nLLVM version: 19.1.0\n","stderr":""}},"successes":{}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Signature: 8a477f597d28d172789f06886806bc55
2+
# This file is a cache directory tag created by cargo.
3+
# For information about cache directory tags see https://bford.info/cachedir/

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/.cargo-lock

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
797f27d3ab66d7a9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":648913621610942305,"features":"[]","declared_features":"[]","target":12909100764264976914,"profile":15008931449961837482,"path":3602296894043704370,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/bar-e091c933de3c7a25/dep-test-lib-bar"}}],"rustflags":["-C","link-arg=--ld-path=/home/I563472/mold/bin/mold","-C","target-cpu=native"],"metadata":7797948686568424061,"config":9480595155961943319,"compile_kind":0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cfeaa437555c3caa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":648913621610942305,"features":"[]","declared_features":"[]","target":13111326501108914580,"profile":15008931449961837482,"path":2893638655806778878,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/fuzzy_name-356131cb66cbe913/dep-test-lib-fuzzy_name"}}],"rustflags":["-C","link-arg=--ld-path=/home/I563472/mold/bin/mold","-C","target-cpu=native"],"metadata":7797948686568424061,"config":9480595155961943319,"compile_kind":0}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/bar-e091c933de3c7a25: primary/src/lib.rs
2+
3+
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/bar-e091c933de3c7a25.d: primary/src/lib.rs
4+
5+
primary/src/lib.rs:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/fuzzy_name-356131cb66cbe913: fuzzy_name/src/lib.rs
2+
3+
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/fuzzy_name-356131cb66cbe913.d: fuzzy_name/src/lib.rs
4+
5+
fuzzy_name/src/lib.rs:

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/incremental/bar-1ibgclpi7ewxf/s-gzfu67o1sf-1blry1l.lock

Whitespace-only changes.

tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/incremental/fuzzy_name-0ufq3vhns8klu/s-gzfu67o1rv-0pi2879.lock

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::current_dir;
3+
use cargo_test_support::file;
4+
use cargo_test_support::prelude::*;
5+
use cargo_test_support::str;
6+
use cargo_test_support::Project;
7+
8+
#[cargo_test]
9+
fn case() {
10+
let project = Project::from_template(current_dir!().join("in"));
11+
let project_root = project.root();
12+
let cwd = &project_root;
13+
14+
snapbox::cmd::Command::cargo_ui()
15+
.arg("add")
16+
.args(["fuzzy-name", "-p", "bar"])
17+
.current_dir(cwd)
18+
.assert()
19+
.code(101)
20+
.stdout_eq(str![""])
21+
.stderr_eq(file!["stderr.term.svg"]);
22+
23+
assert_ui().subset_matches(current_dir!().join("out"), &project_root);
24+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["primary", "fuzzy_name"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "bar"
3+
version = "0.0.0"
4+
edition = "2015"
5+
6+
[dependencies]
7+
fuzzy_name = { path = "../fuzzy_name" }

tests/testsuite/cargo_add/add_workspace_non_fuzzy/out/primary/src/lib.rs

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions

tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/out/primary/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version = "0.0.0"
44
edition = "2015"
55

66
[dependencies]
7-
fuzzy_dependency = "1.0.0"
7+
fuzzy_dependency.workspace = true

tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/stderr.term.svg

Lines changed: 3 additions & 3 deletions

tests/testsuite/cargo_add/features_fuzzy/out/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ version = "0.0.0"
66
edition = "2015"
77

88
[dependencies]
9-
your_face = { version = "99999.0.0" }
9+
your_face = { version = "99999.0.0", features = ["eyes"] }

tests/testsuite/cargo_add/features_fuzzy/stderr.term.svg

Lines changed: 4 additions & 4 deletions

tests/testsuite/cargo_add/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod add_no_vendored_package_with_alter_registry;
44
mod add_no_vendored_package_with_vendor;
55
mod add_normalized_name_external;
66
mod add_toolchain;
7+
mod add_workspace_non_fuzzy;
78
mod build;
89
mod build_prefer_existing_version;
910
mod change_rename_target;

0 commit comments

Comments
 (0)