rust-analyzer: unresolved import: `thiserror::Error`

In the latest version of rust-analyzer (on nvim+coc, but a coworker is reporting the same thing on VSCode), I’m getting the following error from rust-analyzer.

image

[rust-analyzer] [E] unresolved import

This happens even though a regular build of the project finishes just fine.

thiserror::Error is a derive macro without an accompanying trait (c.f. serde::Serialize, which is a derive macro and also a trait, which does not cause an error).

Could there be a regression with importing derive macro traits?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 41 (19 by maintainers)

Most upvoted comments

You can enable proc-macro support using

{
    "rust-analyzer.cargo.loadOutDirsFromCheck": true,
    "rust-analyzer.procMacro.enable": true,
}

They’re all working now 🎉 (the procMacro.enable setting can be kept off, we can now resolve proc macro in rust-analyzer itself without consulting Cargo)

rust-analyzer version: 2020-10-19 (3ca97b0)

This is still happening to me with things like:

use derivative::Derivative;
use strum_macros::EnumIter;

Is that a separate issue or should this be reopened?

Yes. Sorry, I should have posted my config:

{
    "rust-analyzer.updates.channel": "nightly",
    "rust-analyzer.procMacro.enable": true,
    "rust-analyzer.cargo.loadOutDirsFromCheck": true,
    "rust-analyzer.inlayHints.parameterHints": false,
}

This just started happening today when I updated to the latest nightly. (My previous comment has the version)

It can be a security risk (you might load a project with a malicious build scripts)

That already happens with cargo check.

I’m also getting this error in neovim with the built in lsp client but it’s happening on a trait from the std lib rather than an external library. The autocompeltion also seems to not be working for the std::os module.

screenshot

My nvim_lsp config:

local nvim_lsp = require('nvim_lsp')
local on_attach = function(client)
    require'completion'.on_attach(client)
    require'diagnostic'.on_attach(client)
end

...

nvim_lsp.rust_analyzer.setup{
    on_attach = on_attach;
    settings = {
        ["rust-analyzer"] = {
            cargo = {
                loadOutDirsFromCheck = true;
            }
        }
    }
}

You can enable proc-macro support using

{
    "rust-analyzer.cargo.loadOutDirsFromCheck": true,
    "rust-analyzer.procMacro.enable": true,
}

This worked for me! Is there any reason this isn’t enabled by default? It seems like this is a fairly common use case/language feature.

@Folaht your rustprocMacro key is misspelled (see the manual), but these should be enabled by default in recent versions.

I worked around this issue thus: use thiserror::*; // should be thiserror::Error, but https://github.com/rust-analyzer/rust-analyzer/issues/6053

You can use "rust-analyzer.diagnostics.disabled": ["unresolved-import"] to disable the errors for this. It is already disabled in the latest version by default I think. (https://github.com/rust-analyzer/rust-analyzer/pull/6085)