rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
13.68k stars 1.5k forks source link

"rust-analyzer failed to discover workspace" could be so much more helpful #13226

Open matklad opened 1 year ago

matklad commented 1 year ago

Today I've got to help a new Rust user to start their Rust project, and our logic to discovering Cargo.tomls was a huge roadblock. Comically gigantic I would say even :)

Basically, the project was in the subdir of the subdir, so nothing worked. But I wasn't able to diagnose on the spot! I had to do the "ok, let's create a new hello-world project and see if it works there" dance to debug this (and it was actually the person whom I was trying to help who said "huh, maybe the problem is that Cargo.toml is in a subdirectory?")

Here's a bunch of ways we can improve this:

DrChat commented 1 year ago

On this line of thought - I had some prototype work that added logic to search for all Cargo.toml manifests in the workspace folder tree and then filter them out using cargo metadata so that ProjectManifest::discover returned all workspace root manifests. Would something like that be useful for including upstream? Is it too costly to call cargo manifest when scanning for projects?

Also related to #9661

plwalsh commented 1 year ago

@matklad Did something in the extension change recently that now prevents it from auto-discovering projects that aren't at the root of the vscode workspace? I could've sworn it was working fine before the last week or two. But now I see the "failed to discover workspace" message every time I open vscode (with my Rust project is two levels down). I'm on the latest insiders release, with the extension set to the pre-release channel.

matklad commented 1 year ago

We only auto-discover Rust projects one-level deep.

plwalsh commented 1 year ago

@matklad Interesting -- thanks!

zirconium-n commented 1 year ago

We only auto-discover Rust projects one-level deep.

Could we change this to a configurable depth?

plwalsh commented 1 year ago

Could we change this to a configurable depth?

Yes, please. Something like the recently implemented git.repositoryScanMaxDepth would be great (https://github.com/microsoft/vscode/issues/37947).

Most often, my vscode workspace is a collection of clones, with several of the nested clones being Rust projects. Previously with RLS (before switching to rust-analyzer), all the nested Rust projects would automatically be "discovered", and I'd have access to all the RLS tools any time a Rust file was opened. Now with RA, I have no features available in my opened Rust files. Unfortunately, all my workspaces are different/dynamic, so keeping track of them all as rust-analyzer.linkedProjects isn't really tenable.

bjorn3 commented 1 year ago

For activating the extension, I don't think so. It isn't possible to change the activationEvents of an extension: https://github.com/rust-lang/rust-analyzer/blob/61504c8d951c566eb03037dcb300c96f4bd9a8b6/editors/code/package.json#L63 When you open a rust file it should still activate though. For once the extension is activated, possibly: https://github.com/rust-lang/rust-analyzer/blob/61504c8d951c566eb03037dcb300c96f4bd9a8b6/crates/project-model/src/lib.rs#L122-L131

samgqroberts commented 1 year ago

Is there a way to manually resolve this? I have my workspace Cargo.toml a few directories deep in my project. I've scanned the extension settings and can't find a place to manually point rust-analyzer to a Cargo.toml. It seems my only option is to open the subfolder as the root of my VSCode project when I'm working in the Rust portion of it, and switch back to a VSCode window that's at the actual root of my repository for the rest. I've confirmed this works, but it's certainly not ideal.

matklad commented 1 year ago

https://rust-analyzer.github.io/manual.html#rust-analyzer.linkedProjects

samgqroberts commented 1 year ago

That was it! Thank you @matklad

Astlaan commented 1 year ago

I was struggling with strange behaviours from rust-analyzer (sometimes working, others not) for some days, and only now found out this issue of rust projects only being automatically detected on one-level-deep directories.

I think this should be more clear, at least with an error message when no projects are being discovered. But ideally it would probably be useful to allow automatically detecting projects more levels deep by defaulf. If the workspace of the user is too cluttered, he can always exclude folders (such as target etc) from the workspace, or set the depth-level of automatic discovery manually, or set the linkedProjects variable, for example.

ltfschoen commented 1 year ago

I had a similar situation where I had multiple projects in subdirectories that each had their own Cargo.toml file. I got overcame the error rust-analyzer failed to discover workspace by following the steps that i published here: https://stackoverflow.com/a/75649115/3208553, so now rust-analyser works even though the root directory I have open in VSCode is parent directory two levels above the Cargo.toml file of each of the multiple projects

shepazon commented 4 months ago

Being able co configure the depth of auto-discovery would be so very helpful. Having to configure my VS Code to include specific directories for all my projects sounds like tedium. That would be a lot of unnecessary manual setting maintenance.