Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dohse committed Aug 29, 2024
1 parent 9c4c4d1 commit 1569c49
Show file tree
Hide file tree
Showing 50 changed files with 158 additions and 11 deletions.
43 changes: 41 additions & 2 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ fn resolve_dependency(
};
selected_dep = populate_dependency(selected_dep, arg);

let old_dep = get_existing_dependency(manifest, selected_dep.toml_key(), section)?;
let lookup = |dep_key: &_| get_existing_dependency(manifest, dep_key, section);
let old_dep = fuzzy_lookup(&mut selected_dep, lookup, gctx)?;

let mut dependency = if let Some(mut old_dep) = old_dep.clone() {
if old_dep.name != selected_dep.name {
// Assuming most existing keys are not relevant when the package changes
Expand All @@ -383,7 +385,8 @@ fn resolve_dependency(
if dependency.source().is_none() {
// Checking for a workspace dependency happens first since a member could be specified
// in the workspace dependencies table as a dependency
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() {
let lookup = |toml_key: &_| Ok(find_workspace_dep(toml_key, ws.root_manifest()).ok());
if let Some(_dep) = fuzzy_lookup(&mut dependency, lookup, gctx)? {
dependency = dependency.set_source(WorkspaceSource::new());
} else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
// Only special-case workspaces when the user doesn't provide any extra
Expand Down Expand Up @@ -449,6 +452,42 @@ fn resolve_dependency(
Ok(dependency)
}

fn fuzzy_lookup(
dependency: &mut Dependency,
lookup: impl Fn(&str) -> CargoResult<Option<Dependency>>,
gctx: &GlobalContext,
) -> CargoResult<Option<Dependency>> {
if let Some(rename) = dependency.rename() {
// If it has a rename only lookup that name - analogue to Dependency::toml_key()
return lookup(rename);
}

for name_permutation in [
dependency.name.clone(),
dependency.name.replace('-', "_"),
dependency.name.replace('_', "-"),
].into_iter().dedup() {
let Some(dep) = lookup(&name_permutation)? else {
continue;
};

if dependency.name != name_permutation {
// Ignore fuzzy matches with non-registry sources
if !matches!(dep.source, Some(Source::Registry(_))) {
continue;
}
gctx.shell().warn(format!(
"translating `{}` to `{}`",
dependency.name, &name_permutation,
))?;
dependency.name = name_permutation;
}
return Ok(Some(dep));
}

Ok(None)
}

/// When { workspace = true } you cannot define other keys that configure
/// the source of the dependency such as `version`, `registry`, `registry-index`,
/// `path`, `git`, `branch`, `tag`, `rev`, or `package`. You can also not define
Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["primary", "fuzzy_name"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "fuzzy_name"
version = "0.0.0"
edition = "2015"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bar"
version = "0.0.0"
edition = "2015"

[dependencies]
fuzzy_name = { path = "../fuzzy_name" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +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":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
797f27d3ab66d7a9
Original file line number Diff line number Diff line change
@@ -0,0 +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}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cfeaa437555c3caa
Original file line number Diff line number Diff line change
@@ -0,0 +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}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/bar-e091c933de3c7a25: primary/src/lib.rs

/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/bar-e091c933de3c7a25.d: primary/src/lib.rs

primary/src/lib.rs:
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/home/I563472/dev/cargo/tests/testsuite/cargo_add/add_workspace_non_fuzzy/in/target/debug/deps/fuzzy_name-356131cb66cbe913: fuzzy_name/src/lib.rs

/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

fuzzy_name/src/lib.rs:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_add/add_workspace_non_fuzzy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
let project = Project::from_template(current_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("add")
.args(["fuzzy-name", "-p", "bar"])
.current_dir(cwd)
.assert()
.code(101)
.stdout_eq(str![""])
.stderr_eq(file!["stderr.term.svg"]);

assert_ui().subset_matches(current_dir!().join("out"), &project_root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["primary", "fuzzy_name"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bar"
version = "0.0.0"
edition = "2015"

[dependencies]
fuzzy_name = { path = "../fuzzy_name" }
Empty file.
30 changes: 30 additions & 0 deletions tests/testsuite/cargo_add/add_workspace_non_fuzzy/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "0.0.0"
edition = "2015"

[dependencies]
fuzzy_dependency = "1.0.0"
fuzzy_dependency.workspace = true
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_add/features_fuzzy/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ version = "0.0.0"
edition = "2015"

[dependencies]
your_face = { version = "99999.0.0" }
your_face = { version = "99999.0.0", features = ["eyes"] }
8 changes: 4 additions & 4 deletions tests/testsuite/cargo_add/features_fuzzy/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod add_no_vendored_package_with_alter_registry;
mod add_no_vendored_package_with_vendor;
mod add_normalized_name_external;
mod add_toolchain;
mod add_workspace_non_fuzzy;
mod build;
mod build_prefer_existing_version;
mod change_rename_target;
Expand Down

0 comments on commit 1569c49

Please sign in to comment.