In VSCode, rust-analyzer features (such as code completion) doesn't work in certain conditions

There's an issue with the rust-analyzer extension in vscode: the features, especially code completion, in the extension doesn't work if I start vscode and open the folder to a directory and then go to sub-directories to open a rust file. The features only work when I open a rust file via vscode. Whenever I type something in, the vscode auto-completion only appears. When I install other extensions for other languages that includes coding completion such as java, it is guaranteed to work, unlike for rust-analyzer. I'm fairly new to rust, and I need help.

I don't know why it is like that, and I need help fixing it. I use Linux as my operating system and vscode as my editor. Any help would be appreciated

I don't use vscode. But I know that a common vscode problem is neglecting to create a Cargo.toml file using, for example, cargo new.

1 Like

Could you clarify exactly what isn't working for you? I agree that I have seen that rust-analyzer doesn't start running checks until the first time you open a Rust .rs file. But what is this preventing you from doing? You need to open a Rust file to work on Rust code, no? Is it just the time that you have to wait?

By the latter I believe they mean it only works when they run code SOURCE_FILE at the command line.

1 Like

I'm sorry that I couldn't really word it. It's whenever I open a folder with vscode and then open the source file when my cargo project is the sub-directory, rust-analyzer works. If I put the cargo project in another folder and then opening the source file, rust-analyzer doesn't work. This also happens when I create a new rust file without using cargo. The below images are here to prove it

One additional thing: while I was testing, a notification popped up saying the document highlighting failed for some reason
and this was the output

Panic context:
version: 0.3.1860-standalone
request: textDocument/documentHighlight DocumentHighlightParams {
    text_document_position_params: TextDocumentPositionParams {
        text_document: TextDocumentIdentifier {
            uri: Url {
                scheme: "file",
                cannot_be_a_base: false,
                username: "",
                password: None,
                host: None,
                port: None,
                path: "/run/media/tbf96/CODE/Coding/Rust/test/subdirectory/testing_twi/src/",
                query: None,
                fragment: None,
        position: Position {
            line: 2,
            character: 9,
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    partial_result_params: PartialResultParams {
        partial_result_token: None,

thread 'Worker' panicked at /github/home/.cargo/registry/src/
Bad offset: range 0..45 offset 52
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/
   2: rowan::cursor::SyntaxNode::token_at_offset
   3: ide::highlight_related::highlight_related
   4: std::panicking::try
   5: ide::Analysis::highlight_related
   6: rust_analyzer::handlers::request::handle_document_highlight
   7: std::panicking::try
   8: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[Error - 9:52:11 AM] Request textDocument/documentHighlight failed.
  Message: request handler panicked: Bad offset: range 0..45 offset 52
  Code: -32603 

Overall, it was pretty confusing for me to explain because it was so complicated to explain.
I just want to use rust-analyzer for convenience, and this issue is just annoying. Also, waiting by itself doesn't seem to fix the issue.

I understand. Yes. This is a limitation of rust-analyzer's project auto-discovery: it only looks at most one subdirectory deep. (I I think I heard that this is to avoid spending lots of time searching the file system in case the project isn't Rust.) Here is what you can do about that:

  • The normal way to use rust-analyzer is that you should open, in VS Code, the directory that contains the project's Cargo.toml file — which can be a workspace Cargo.toml if you want to work on multiple packages together.

  • If you intend to set up a project with deeply located Cargo packages, you'll have to use the rust-analyzer.linkedProjects setting to give explicit paths to each Cargo.toml file. I don't recommend doing this.

This also happens when I create a new rust file without using cargo.

That's more inherent. In order to know how to assist with a file, rust-analyzer has to be able to compile it, and Cargo.toml is necessary to know how it should be compiled. This applies to most other tools too; Rust code should always be accompanied by a Cargo.toml unless you're intentionally using a different build system than Cargo (this is rare and done for special purposes).

while I was testing, a notification popped up saying the document highlighting failed for some reason

rust-analyzer is not completely free of bugs. The rust-analyzer maintainers would like to hear your bug reports, and they're not going to automatically send logs (that would be a violation of privacy) so they rely on you to share them. It's possible to turn off those notifications, though; the setting is rust-analyzer.showRequestFailedErrorNotification.

Yes please create a bug report with rust-analyzer and include at least the panic context and stack trace. The team will be happy to help.

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.