Skip to content

Commit 331d1db

Browse files
committed
Add crate versions when running cargo -p commands.
Until now cargo commands with the -p flag would pass the package name only. It doesn't play super well with the toml Renaming dependencies feature. This commit specifies the package name and version when a cargo command is run with the -p flag, to avoid ambiguities.
1 parent 668980d commit 331d1db

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)