Skip to content

Commit d63bb85

Browse files
Merge #3781
3781: Add crate versions when running cargo -p commands. r=matklad a=o0Ignition0o If someone (unfortunately) creates a project that happens to have the same name as one of its (future) dependencies, there is [a way for them to change the dependency's alias in the Cargo.toml file](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml), to mitigate the name conflict. Unfortunately cargo -p commands don't seem to pick it up, which seems to put rust-analyzer run commands in a tough situation: ``` > Executing task: cargo test --package config --example default -- tests --nocapture < error: There are multiple `config` packages in your project, and the specification `config` is ambiguous. Please re-run this command with `-p <spec>` where `<spec>` is one of the following: config:0.1.0 config:0.9.3 The terminal process terminated with exit code: 101 ``` cargo suggests us to be more specific and refer to a package by its name and version, which this PR achieves. I passed the version as a String because I don't really understand how the ra_db types work, but I would love to switch it to [a fully fledged Version type](https://steveklabnik.github.io/semver/semver/index.html) if you guide me towards that :) Co-authored-by: o0Ignition0o <[email protected]>
2 parents a932ccd + 331d1db commit d63bb85

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub type Target = Idx<TargetData>;
7575

7676
#[derive(Debug, Clone)]
7777
pub struct PackageData {
78+
pub id: String,
7879
pub name: String,
7980
pub manifest: PathBuf,
8081
pub targets: Vec<Target>,
@@ -180,6 +181,7 @@ impl CargoWorkspace {
180181
.with_context(|| format!("Failed to parse edition {}", edition))?;
181182
let pkg = packages.alloc(PackageData {
182183
name,
184+
id: id.to_string(),
183185
manifest: manifest_path,
184186
targets: Vec::new(),
185187
is_member,
@@ -249,6 +251,18 @@ impl CargoWorkspace {
249251
pub fn workspace_root(&self) -> &Path {
250252
&self.workspace_root
251253
}
254+
255+
pub fn package_flag(&self, package: &PackageData) -> String {
256+
if self.is_unique(&*package.name) {
257+
package.name.clone()
258+
} else {
259+
package.id.clone()
260+
}
261+
}
262+
263+
fn is_unique(&self, name: &str) -> bool {
264+
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
265+
}
252266
}
253267

254268
#[derive(Debug, Clone, Default)]

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl CargoTargetSpec {
7777
ProjectWorkspace::Cargo { cargo, .. } => {
7878
let tgt = cargo.target_by_root(&path)?;
7979
Some(CargoTargetSpec {
80-
package: cargo[cargo[tgt].package].name.clone(),
80+
package: cargo.package_flag(&cargo[cargo[tgt].package]),
8181
target: cargo[tgt].name.clone(),
8282
target_kind: cargo[tgt].kind,
8383
})

0 commit comments

Comments
 (0)