Skip to content

fix: Build non-ambiguous monikers in crates with multiple targets - #23173

Open
nicolas-guichard wants to merge 5 commits into
rust-lang:masterfrom
nicolas-guichard:push-xuslvlqqmyoq
Open

fix: Build non-ambiguous monikers in crates with multiple targets#23173
nicolas-guichard wants to merge 5 commits into
rust-lang:masterfrom
nicolas-guichard:push-xuslvlqqmyoq

Conversation

@nicolas-guichard

Copy link
Copy Markdown
Contributor

Previously, a crate which defined multiple targets would cause ambiguous monikers to be built.

For instance when generating an SCIP index in a simple project with a lib and a bin target:

scip-collision/
├── Cargo.toml
└── src/
    ├── lib.rs
    └── main.rs

main.rs:

struct S;

fn main() {}

lib.rs:

struct S;

rust-analyzer scip scip-collision complained with:

Duplicate symbols encountered:

src/lib.rs:0:0-1:0
  Duplicate symbol: rust-analyzer cargo scip-collision 0.1.0 crate/

src/lib.rs:0:7-0:8
  Duplicate symbol: rust-analyzer cargo scip-collision 0.1.0 S#

This fixes the ambiguity by adding the target type and name° to each symbol's moniker. For the example above we now get these symbols:

  • rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/
  • rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/S#
  • rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/main().
  • rust-analyzer cargo scip-collision 0.1.0 lib/
  • rust-analyzer cargo scip-collision 0.1.0 lib/S#

°: this doesn't add the name for lib, proc-macro and build script targets because there's only one of each per Cargo package

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 17, 2026
@nicolas-guichard
nicolas-guichard force-pushed the push-xuslvlqqmyoq branch 4 times, most recently from 9e255bf to f23bf7e Compare August 17, 2026 17:10
We need the target kind to be accessible from the ide crate to be able
to build non-ambiguous monikers in `StaticIndex`.

This only moves the enum definition, actually passing the data down
happens in the next commit.
Now that the type is accessible in base_db, this actually stores it in
`CrateData` and replaces every use of `CrateData::is_proc_macro` with
`CrateData::target_kind::is_proc_macro`.

The `TargetKind` used in tests is always `Lib` for now.
I want to add an extra field to this tuple, let's make it a struct for
readability.
When writing a test, it's now possible possible to set a custom target
kind per crate, by prepending the target kind to the crate name.

For instance writing `crate:bin:main` will set `CrateData::target_kind`
to `TargetKind::Bin`.
Previously, a crate which defined multiple targets would cause
ambiguous monikers to be built.

For instance when generating an SCIP index in a simple project with a
lib and a bin target:
```
scip-collision/
├── Cargo.toml
└── src/
    ├── lib.rs
    └── main.rs
```

main.rs:
```
struct S;

fn main() {}
```

lib.rs:
```
struct S;
```

`rust-analyzer scip scip-collision` complained with:
```
Duplicate symbols encountered:

src/lib.rs:0:0-1:0
  Duplicate symbol: rust-analyzer cargo scip-collision 0.1.0 crate/

src/lib.rs:0:7-0:8
  Duplicate symbol: rust-analyzer cargo scip-collision 0.1.0 S#
```

This fixes the ambiguity by adding the target type and name° to each
symbol's moniker. For the example above we now get these symbols:
- `rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/`
- `rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/S#`
- `rust-analyzer cargo scip-collision 0.1.0 bin/scip_collision/main().`
- `rust-analyzer cargo scip-collision 0.1.0 lib/`
- `rust-analyzer cargo scip-collision 0.1.0 lib/S#`

°: this doesn't add the name for lib, proc-macro and build script
targets because there's only one of each per Cargo package.
@rustbot

rustbot commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants