Skip to content

Commit 99b69ec

Browse files
authored
bug: Use manifest path as reported buildfile (#38)
Previously, we were reporting the workspace Cargo.toml as the buildfile for a `discover` invocation. This led to incorrect behavior where RA would "unindex" any crates that were included in the first `discover` invocation but not in subsequent invocations (reporting the buildfile as the workspace Cargo.toml implies that we are telling RA about all of the crates in the workspace, but we are actually only telling it about a subset). Consider the following dependency graph: ``` A: C B: C C: ``` If we're invoked with a file from crate `A`, we would tell RA about crates `A` and `C` with the workspace manifest as the buildfile. If we are subsequently invoked with a file from crate `B`, we'd still report the buildfile as the workspace manifest, but now we'd only tell RA about crates `B` and `C`. This causes RA to "unindex" `A`, since we're telling RA that the workspace doesn't include it. This commit rectifies the issue by reporting the current crate's Cargo.toml as the buildfile in the `discover` output. However, this changes the semantics of how and when `check` is invoked. In the above example, we'd now report two separate "projects" after being invoked with files from crates `A` and `B`. Because `A` and `B` do not depend on each other, RA will invoke `check` separately for each of them. This is rather annoying if they share many dependencies (e.g. if `C` has a giant dependency tree), since those `check` invocations will be duplicating a lot of work. This behavior can be adjusted by setting RA's `check.invocationStrategy` setting to `"once"`, which tells it just to invoke check for the current project instead of all the projects we've told it about. This commit also updates the README to reflect this change. Fixes #34
1 parent f827ba7 commit 99b69ec

2 files changed

Lines changed: 4 additions & 16 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Add the following to your `settings.json`:
9898
"Cargo.toml"
9999
]
100100
},
101+
"rust-analyzer.check.invocationStrategy": "once",
101102
"rust-analyzer.check.overrideCommand": [
102103
"cargo-subspace",
103104
"check", // You can also use "clippy" here
@@ -115,6 +116,7 @@ also be set via lspconfig.
115116
```lua
116117
["rust-analyzer"] = {
117118
check = {
119+
invocationStrategy = "once",
118120
overrideCommand = {
119121
"cargo-subspace",
120122
"clippy",
@@ -149,6 +151,7 @@ These settings should be specified in `Settings --> LSP Client --> User Server S
149151
"useWorkspace": false,
150152
"initializationOptions": {
151153
"check": {
154+
"invocationStrategy": "once",
152155
"overrideCommand": [
153156
"cargo-subspace",
154157
"clippy",

src/main.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,8 @@ fn discover(ctx: &Context, discover_args: DiscoverArgs, manifest_path: FilePath<
217217
let metadata = cmd.exec()?;
218218
let project = compute_project_json(ctx, discover_args, metadata, manifest_path)?;
219219

220-
let root = ctx
221-
.cargo()
222-
.arg("locate-project")
223-
.arg("--workspace")
224-
.arg("--manifest-path")
225-
.arg(manifest_path)
226-
.arg("--message-format")
227-
.arg("plain")
228-
.output()?;
229-
let buildfile: PathBuf = String::from_utf8(root.stdout)?.trim().into();
230220
let output = DiscoverProjectData::Finished {
231-
buildfile: Utf8PathBuf::from_path_buf(buildfile).map_err(|e| {
232-
anyhow!(
233-
"Manifest path `{}` contains non-UTF-8 characters",
234-
e.display()
235-
)
236-
})?,
221+
buildfile: manifest_path.to_path_buf(),
237222
project,
238223
};
239224
let json = if ctx.is_tty {

0 commit comments

Comments
 (0)