Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/ra_project_model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub type Target = Idx<TargetData>;

#[derive(Debug, Clone)]
pub struct PackageData {
pub id: String,
pub name: String,
pub manifest: PathBuf,
pub targets: Vec<Target>,
Expand Down Expand Up @@ -180,6 +181,7 @@ impl CargoWorkspace {
.with_context(|| format!("Failed to parse edition {}", edition))?;
let pkg = packages.alloc(PackageData {
name,
id: id.to_string(),
manifest: manifest_path,
targets: Vec::new(),
is_member,
Expand Down Expand Up @@ -249,6 +251,18 @@ impl CargoWorkspace {
pub fn workspace_root(&self) -> &Path {
&self.workspace_root
}

pub fn package_flag(&self, package: &PackageData) -> String {
if self.is_unique(&*package.name) {
package.name.clone()
} else {
package.id.clone()
}
}

fn is_unique(&self, name: &str) -> bool {
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
}
}

#[derive(Debug, Clone, Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/cargo_target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl CargoTargetSpec {
ProjectWorkspace::Cargo { cargo, .. } => {
let tgt = cargo.target_by_root(&path)?;
Some(CargoTargetSpec {
package: cargo[cargo[tgt].package].name.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would cargo[cargo[tgt].package].id.clone() work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think it might, for now: https://docs.rs/cargo_metadata/0.9.1/cargo_metadata/struct.PackageId.html

It is possible to inspect the repr field, if the need arises, but its precise format is an implementation detail and is subject to change.

Would you like me to give it a try?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and if it works, I think we need to use this field, as it is the package id spec.

OTOH, it's also useful to keep this command human editable, so I think ideally the logic should be:

  • if there's a single package with this name, use the name
  • if there can be ambiguity, use the fully unabmigious id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great idea, I'm going to give it a go!

package: cargo.package_flag(&cargo[cargo[tgt].package]),
target: cargo[tgt].name.clone(),
target_kind: cargo[tgt].kind,
})
Expand Down