Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust-analyzer not working in vscode #4894

Closed
Edu4rdSHL opened this issue Jun 15, 2020 · 31 comments
Closed

rust-analyzer not working in vscode #4894

Edu4rdSHL opened this issue Jun 15, 2020 · 31 comments

Comments

@Edu4rdSHL
Copy link

Edu4rdSHL commented Jun 15, 2020

Hello, I'm having the problem that rust-analyzer doesn't show errors regarding to nonexistent or wrong typed variables, bad number of function parameters and more, while RLS works fine. Tried with the @matklad rust-analyzer extension and the official Rust extension changing the engine to rust-analyzer, using one extension at time.

I also switched to nightly builds but nothing changed. How can I debug that? rust-analyzer was working but after an update just stopping working.

Edit: I also tried resetting all extensions settings to default regarding to rust-analyzer, nothing changed as well.

@bjorn3
Copy link
Member

bjorn3 commented Jun 15, 2020

Try the rust-analyzer extension. The rust-analyzer support in the official Rust extension is prematurely as far as I am aware.

@Edu4rdSHL
Copy link
Author

Tried with the @matklad rust-analyzer extension and the official Rust extension changing the engine to rust-analyzer, using one extension at time.

Already tried that.

@bjorn3
Copy link
Member

bjorn3 commented Jun 15, 2020

Can you post the logs for the rust-analyzer extension? (output pane and then "Rust Analyzer Language Server")

@Edu4rdSHL
Copy link
Author

I just detected that it's working fine for a project but not for another, what should I check? I haven't changed anything as far as I remember.

@bjorn3
Copy link
Member

bjorn3 commented Jun 15, 2020

For which project does it not work? Is there a Cargo.toml in the root of the project or is it part of a sub directory?

@Edu4rdSHL
Copy link
Author

cargo clean fixed the problem, no idea what was it. Thank you.

@Edu4rdSHL Edu4rdSHL reopened this Jun 15, 2020
@Edu4rdSHL
Copy link
Author

Edu4rdSHL commented Jun 15, 2020

Sorry, this not fixed the issue, I was still looking the other project.

For which project does it not work? Is there a Cargo.toml in the root of the project or is it part of a sub directory?

Yes, there's a Cargo.toml in the root folder. Here are the logs where I changed resolv_data to this_doesnot_exist:

https://gist.github.com/Edu4rdSHL/763a4f0774af6bf32257d83f5de1c720

@flodiebold
Copy link
Member

Does cargo check inside the repository work? Do you use any cargo features?

@Edu4rdSHL
Copy link
Author

Edu4rdSHL commented Jun 15, 2020

Yes, this work:

└─ ▶ cargo check
    Checking findomain-plus v6.4.0 (/home/sechacklabs/Projects/Rust/Development/findomain-plus)
error[E0425]: cannot find value `resolv_data` in this scope
  --> src/database.rs:61:42
   |
61 |                 &logic::null_ip_checker(&resolv_data.ip),
   |                                          ^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `resolv_data` in this scope
  --> src/database.rs:62:18
   |
62 |                 &resolv_data.http_status.http_status,
   |                  ^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `resolv_data` in this scope
  --> src/database.rs:63:46
   |
63 |                 &logic::return_ports_string(&resolv_data.open_ports, args),
   |                                              ^^^^^^^^^^^ not found in this scope

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `findomain-plus`.

To learn more, run the command again with --verbose.

Same happens calling a nonexistent field inside a struct, rust-analyzer == no errors, cargo check:

└─ ▶ cargo check
    Checking findomain-plus v6.4.0 (/home/sechacklabs/Projects/Rust/Development/findomain-plus)
error[E0609]: no field `this_field_no_exist` on type `&structs::ResolvData`
  --> src/database.rs:61:54
   |
61 |                 &logic::null_ip_checker(&resolv_data.this_field_no_exist),
   |                                                      ^^^^^^^^^^^^^^^^^^^ unknown field
   |
   = note: available fields are: `ip`, `http_status`, `open_ports`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0609`.
error: could not compile `findomain-plus`.

To learn more, run the command again with --verbose.

Edit:

Does cargo check inside the repository work? Do you use any cargo features?

I don't use any cargo features.

@Edu4rdSHL
Copy link
Author

Edu4rdSHL commented Jun 15, 2020

Moving the same src and Cargo.* files to another new folder works... weird thing, I will just move this.

@rucoder
Copy link

rucoder commented Jul 28, 2020

I have the same issue when upgrading rust analyzer. I think there is some incompatibility in database files between versions. only 'cargo clean' helps

@bjorn3
Copy link
Member

bjorn3 commented Jul 28, 2020

Rust-analyzer currently doesn't persist anything on the disk.

@pete-eiger
Copy link

Yep, cargo clean did the trick, thanks OP 🚀

@caldevcani
Copy link

I am having the same issue that Rust Analyzer does not work for me at all. I use Cargo to create a project but while writing code in main.rs I don't get any help from Rust Analyzer. No code completion no hints nothing. Any help to make this work is greatly appreciated

@Mirch
Copy link

Mirch commented Jan 30, 2022

I have the same issue: it works in ./cargo_project/src/main.rs, but not in ./some_folder/cargo_project/src/main.rs.
cargo clean did not do the trick.

@alexfromapex
Copy link

For some reason, the config for pointing to the server seems to be failing on fresh installs. I was able to fix mine by finding the path to my rust-analyzer server and putting it in the settings.json for VS Code:

You will need to look in your ~/.vscode/extensions/ folder and find the proper path as this is architecture and version dependent.

{
    "rust-analyzer.server.path": "~/.vscode/extensions/matklad.rust-analyzer-0.2.1040-darwin-arm64/server/rust-analyzer"
}

@shinmao
Copy link

shinmao commented Jul 2, 2022

One another thing you might need to check is that,
In VScode, you need to stay in same level as Cargo.toml!
Rust-analyzer will not be able to detect whether parent or child dir is rust project or not. This is how I solve my problem :)

@Edu4rdSHL
Copy link
Author

This issue has been fixed a long time ago, if you have problems please use another issue.

I'm unsubscribing from it.

@gRoussac
Copy link

One another thing you might need to check is that, In VScode, you need to stay in same level as Cargo.toml! Rust-analyzer will not be able to detect whether parent or child dir is rust project or not. This is how I solve my problem :)

It's a bit strange because I would expect at least having child folders. It seems it can be worked around by creating a VsCode Workspace with the exact same folders, then rust-analyser seems to find the nested Cargo.toml...

@TBytes404
Copy link

I was using rustlings. rustlings lsp does the trick.

@jeffs
Copy link

jeffs commented Aug 21, 2023

cargo clean worked for me, even though I had already blown away target. Bizarrely, clicking the little Run button (action? lens? inlay?) above main would run the program, but the rust-analyzer: Run command didn't show an option for running main. It does after cargo clean.

@kodmanyagha
Copy link

cargo clean and after that reload the window.

@mendozajp
Copy link

mendozajp commented Dec 2, 2023

cargo clean wasn't doing it for me in the beginning. After addressing a couple of other random things on my machine, updating vs code, running cargo clean in the relevant project dir and then closing vs code and opening it again did it actually work.

It only works when your working top dir in vs code is the project though, so I have to do code ~/projects/rust/current_project instead of code ~/projects

@temannin
Copy link

temannin commented Dec 31, 2023

If you're at your wits end and nothing else is working, try deleting your .vscode directory, closing out of VSCode and reloading the project. It worked for me.

(I had a messed up setting and I didn't know it)

@miiiiiYT
Copy link

miiiiiYT commented Feb 8, 2024

if you're having trouble to get rust-analyzer to detect a file that's directly horizontal to main.rs, try importing that file using mod file_name;. for some reason rust-analyzer only works on main.rs and its imports.

@Hitisha-G
Copy link

if you're having trouble to get rust-analyzer to detect a file that's directly horizontal to main.rs, try importing that file using mod file_name;. for some reason rust-analyzer only works on main.rs and its imports.

it isn't even working on main.rs and even cargo clean doesn't help

@dcodesdev
Copy link

Had the same issue, I had 2 rust workspaces in my project, one of them in the root and the other in a sub-directory.

It was working for both workspaces but for the workspace in the sub-directory, it was only working for a few packages which was a weird behaviour.

Adding this to my .vscode/settings.json did the trick

{
  "rust-analyzer.linkedProjects": [
    "./Cargo.toml",
    "packages/data/challenge/Cargo.toml"
  ]
}

@TaladaJaswanth
Copy link

Moving the same src and Cargo.* files to another new folder works... weird thing, I will just move this.

great opening the file in a new window with just only one file that you are working then it worked
Screenshot 2024-07-24 192410
rust analyzer if not showing errors use above method i do not know why it did not worked

@ch3ll0v3k
Copy link

For anyone else, you have to open root directory in VS, not file.
And it will work.
Its very stupid if you ask me, but whatever...

@davidbarsky
Copy link
Contributor

For anyone else, you have to open root directory in VS, not file. And it will work. Its very stupid if you ask me, but whatever...

This will be change in the future: #17537

@TomzBench
Copy link

I've used cargo clean. Im in the root directory. I have this issue where it fails to emit diagnostics only for examples. Library code works as expected. I tried to make a minimal example, but this minimal example works as expected. so im not sure whats unique about this non working repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests