VSCode and rust-analyzer: rust-analyzer features (such as code completion or highlighting code issues ) doesn't work anymore

Hi!
I noticed that rust-analyzer features (such as code completion or highlighting code issues ) doesn't work anymore in my VSCode. Rust-analyzer briefly and intermittently worked at the first VSCode installation but since then, no matter what I tried, it doesn't do anything although it doesn't report a crashing error, either.

I use Ubuntu 22 and VScode 1.91.0.

I tried to clean remove and reinstall VSCode + rust-analyzer and I tried cargo clean.

How can I debug it?

The most common reason for rust-analyzer to not help with a file is that the file is not part of a Cargo workspace.

Does rust-analyzer work if you clone someone else's Rust project and open it?

It doesn't help with the Rustlings examples either. It is not working for any rs file.

I probably know what's going on.
You should open a complete project. It's like this:

├─src
└─target
    ├─debug
    ├─.rustc_info.json
    └─CACHEDIR.TAG
├─.gitignore
├─Cargo.lock
└─Cargo.toml

It recognizes Cargo.toml files in the current directory.
You can go to rust-analyzer, edit setting.json. Check if the Cargo.toml file is in this place:

     "rust-analyzer.linkedProjects": [
    
        "Cargo.toml"
    ],

image
@huongjay I do not know where to find in VSCode, the settings.json file associated with rust-analyzer. Is this one?


After lot of searching, I find this package.json which seems to be the one you named setting.json?

You should likely set this one in the workspace config, rather than the user config. The workspace config can be found at .vscode/settings.json inside the root of the project you are working on. If it doesn't exist yet you can create it.

@bjorn3 So my code is organized in subfolders within a 'parent' directory named RUST. In the parent dir I found a folder .vscode containing only one file: "launch.json". I created here the settings.json file. In my project directory there was no .vscode dir so I created one along with the settings.json file where I put: " ```

 "rust-analyzer.linkedProjects": [

    "Cargo.toml"
],

The problem persists. I was thinking that all there config files should be created automatically by the VSCode and rust-analyzer, not manually by the user?

You need to point to the Cargo.toml of each cargo workspace you want to work with. For example foo/Cargo.toml and bar/Cargo.toml if you want to work on fok and bar.

@bjorn3 The directory structure looks like this:
image

From the .vscode/settings.json I pointed toward my project's Cargo.toml:

     "rust-analyzer.linkedProjects": [
    
        "HDD/WORK/PROJECTS/RUST/udemy_learn_rust_real_app/server"
    ],

No coding assistance is being made:

  • The compiler is not complaining for errors during code writing
  • It is not pointing toward documentations
    ....

I am not sure what I am missing.

Each path you put in linkedProjects should end in Cargo.toml, not just a directory.

Thanks, also the absolute path is needed. Nevertheless it is strange that after I put only one path in linkedProjects, rust-anayzer works again for all projects. I was expecting that by default rust-analyzed should look for all Cargo.toml files from subdirectories

My understanding is that rust-analyzer's default behavior is to search at most one directory deep for Cargo.toml files, to avoid the potentially large cost of scanning the entire workspace file tree. I don't know why adding linkedProjects would change anything not explicitly listed, though.

Is it possible that the server crate depends on your other crates? All dependencies of a crate you mention in rust-analyzer.linkedProjects will be analyzed too and get ide functionality as a result.

There are small independent projects, not relying on each other. All of them are within a parent directory. I think I know what it happens, after I did a clean uninstall of VSCode. If the Cargo.toml of a project is not specified in the settings.json file it is not checked anymore (behavior changed after clean reinstall). If no project is registered, I get the error "failed to discover a workspace". My understanding is that when I open a parent folder in VSCode, the program should scan all its subfoders for Cargo.toml files. Apparently it is not doing this.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.