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

unresolved import: thiserror::Error #6053

Closed
bryanburgers opened this issue Sep 21, 2020 · 41 comments
Closed

unresolved import: thiserror::Error #6053

bryanburgers opened this issue Sep 21, 2020 · 41 comments
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in S-actionable Someone could pick this issue up and work on it right now

Comments

@bryanburgers
Copy link

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?

@kbleeke
Copy link

kbleeke commented Sep 21, 2020

I'm having a similar issue with snafu::Snafu (another error-handling derive macro)

Screenshot from 2020-09-21 18-41-15

@bjorn3
Copy link
Member

bjorn3 commented Sep 21, 2020

You can enable proc-macro support using

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

@Systemcluster
Copy link

Systemcluster commented Sep 21, 2020

I had the same issue with derive macro imports after the latest update with an existing project, but cleaning the target, rebuilding the project and reloading the editor solved it. I still don't get any information on hover (it's stuck at "Loading..."), but I don't know if that was already the case in the previous version.

@kbleeke
Copy link

kbleeke commented Sep 21, 2020

You can enable proc-macro support using

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

I thought, I had this enabled but apparently it was only in workspace settings of another workspace 🤦 . Fixed for me at least

@EverlastingBugstopper
Copy link

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.

@lnicola
Copy link
Member

lnicola commented Sep 25, 2020

It can be a security risk (you might load a project with a malicious build scripts), it slows down the first load of the project (because we need to compile them), it can slow down the analysis, it doesn't always work (because of ABI differences, e.g. proc macros don't work in nightly and beta right now and we can't fix them without breaking stable users they work since yesterday).

PS: cargo check is enabled by default, so maybe the security aspect is moot.

@bjorn3
Copy link
Member

bjorn3 commented Sep 25, 2020

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

That already happens with cargo check.

@kellpossible
Copy link

kellpossible commented Sep 29, 2020

You can enable proc-macro support using

Enabling those options worked for me but they made RA super slow to the point where I can't use it, and have to put up with the errors for now.

@bjorn3
Copy link
Member

bjorn3 commented Sep 29, 2020

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. (#6085)

@sunjay
Copy link
Member

sunjay commented Sep 29, 2020

I seem to be getting the unresolved import error for thiserror::Error regardless of whether I have procMacro enabled. I am on rust-analyzer version: nightly (7b674f9).

@bjorn3
Copy link
Member

bjorn3 commented Sep 29, 2020

Do you also have "rust-analyzer.cargo.loadOutDirsFromCheck": true?

@sunjay
Copy link
Member

sunjay commented Sep 29, 2020

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)

@Ten0
Copy link

Ten0 commented Oct 1, 2020

Same here. Additionally, it seems that cargo will stop checking at all if "rust-analyzer.cargo.loadOutDirsFromCheck": true,. (Though that may be more related to the fact my project is large?)
Note I didn't have these options enabled before, but enabling them did not fix this issue for me, it still shows the unresolved imports.

@lnicola
Copy link
Member

lnicola commented Oct 1, 2020

@sunjay

I seem to be getting the unresolved import error for thiserror::Error regardless of whether I have procMacro enabled. I am on rust-analyzer version: nightly (7b674f9).

#5651

@Count-Count
Copy link

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

@0x8f701
Copy link

0x8f701 commented Oct 3, 2020

image
I'm having similar issue

@matklad matklad added the E-unknown It's unclear if the issue is E-hard or E-easy without digging in label Oct 14, 2020
@matklad
Copy link
Member

matklad commented Oct 14, 2020

cc @jonas-schievink

@matklad matklad added the S-actionable Someone could pick this issue up and work on it right now label Oct 14, 2020
@jonas-schievink
Copy link
Contributor

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)

@teohhanhui
Copy link

Now we only need a stable release.

@flodiebold
Copy link
Member

The fix for this has been included in the last two stable releases, as far as I can see.

@teohhanhui
Copy link

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?

@lnicola
Copy link
Member

lnicola commented Oct 25, 2020

@teohhanhui did you enable build script and proc macro support as in #6053 (comment)?

@flodiebold
Copy link
Member

strum_macros was mentioned here as well and seems to be related to cfg_attr support, which we already have #5548 for. It'd be worth verifying that for both of these though.

@teohhanhui
Copy link

@lnicola:

did you enable build script and proc macro support as in #6053 (comment)?

#6053 (comment)

the procMacro.enable setting can be kept off, we can now resolve proc macro in rust-analyzer itself without consulting Cargo

rust-analyzer.cargo.loadOutDirsFromCheck is already set to true.

@itmecho
Copy link

itmecho commented Oct 25, 2020

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;
            }
        }
    }
}

@bjorn3
Copy link
Member

bjorn3 commented Oct 25, 2020

That is a duplicate of #6038.

@itmecho
Copy link

itmecho commented Oct 25, 2020

Ah thanks, I'll follow that issue as well

@wpbrown
Copy link

wpbrown commented Oct 26, 2020

strum_macros was mentioned here as well and seems to be related to cfg_attr support, which we already have #5548 for. It'd be worth verifying that for both of these though.

Indeed derivative is the same issue. Thanks

#[cfg_attr(not(test), proc_macro_derive(Derivative, attributes(derivative)))]
pub fn derivative(input: TokenStream) -> TokenStream {

@hammypants
Copy link

hammypants commented Dec 4, 2020

This is still happening for the original issue on latest nightly.

Enabling procMacro and cargo.loadOutDirsFromCheck resolves the import, but then all proc macros (that I have tried) fail with proc macro returned error: Server closed.

use thiserror::Error;
..
#[derive(Error, Debug)] // <-- on 'Error': proc macro returned error: Server closed
#[error("{0}")]
struct Foo(String);

@bjorn3
Copy link
Member

bjorn3 commented Dec 4, 2020

Which rustc version are you using?

@hammypants
Copy link

hammypants commented Dec 5, 2020

Which rustc version are you using?

rustc 1.46.0 (04488afe3 2020-08-24)

I'll try updating to latest.

EDIT:
Works on latest (and nightly) only with both of the mentioned options above set:

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

@aaomidi
Copy link

aaomidi commented Jan 26, 2021

image
Not working for me unfortunately.

image

Settings are on true

➜ rustc --version
rustc 1.51.0-nightly (da305a2b0 2021-01-05)

@bjorn3
Copy link
Member

bjorn3 commented Jan 27, 2021

I think there was an abi breaking change on rustc nightly. Either switch to stable or wait for this change to land on stable such that rust-analyzer can adapt to it.

@edwin0cheng
Copy link
Member

edwin0cheng commented Jan 27, 2021

@aaomidi Confirm your bug in rustc-stable,

maybe related to cfg_attr(not(test)...) ?

https://github.com/mcarton/rust-derivative/blob/770c67484a872470ca782202e13fc70a35e2695b/src/lib.rs#L76-L80

#[cfg_attr(not(test), proc_macro_derive(Derivative, attributes(derivative)))]
pub fn derivative(input: TokenStream) -> TokenStream {
    let mut errors = proc_macro2::TokenStream::new();

    let mut output = match syn::parse::<syn::DeriveInput>(input) {
        Ok(parsed) => {
            ast::Input::from_ast(&parsed, &mut errors)
                .map(|mut input| derive_impls(&mut input, &mut errors))
                .unwrap_or_default()
        },
        Err(error) => {
            errors.extend(error.to_compile_error());
            Default::default()
        }
    };

    output.extend(errors);
    output.into()
}

@Folaht
Copy link

Folaht commented Jun 4, 2021

Using LunarVim

rust-ls.lua

require'lspconfig'.rust_analyzer.setup{
    cmd = {DATA_PATH .. "/lspinstall/rust/rust-analyzer"},
    on_attach = require'lsp'.common_on_attach,
    settings = {
        ["rust-analyzer"] = {
	    procMacro = {
	        enable = true
	    },
	    cargo = {
		loadOutDirsFromCheck = true
	    },
        }
    }
}

@lnicola
Copy link
Member

lnicola commented Jun 8, 2021

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

@Folaht
Copy link

Folaht commented Jun 8, 2021

I changed it accordingly. It should be okay now.

@OfekShochat
Copy link

OfekShochat commented Sep 2, 2021

Im getting this currently with ed25519_dalek

@lnicola
Copy link
Member

lnicola commented Sep 10, 2021

@OfekShochat that's probably #7243

@OfekShochat
Copy link

yes it is, looked at it

@aranw
Copy link

aranw commented Jul 10, 2023

I tried to add the suggested fixes above in this issue but I'm still getting the same issue. Is there something else that I am missing?

❯ rustc --version
rustc 1.70.0-nightly (39f2657d1 2023-03-09)

I'm also using rust-analyzer v0.4.1582

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests