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 for proc-macro generated identifier #6054

Closed
ndmitchell opened this issue Sep 21, 2020 · 25 comments · Fixed by #5651
Closed

unresolved import for proc-macro generated identifier #6054

ndmitchell opened this issue Sep 21, 2020 · 25 comments · Fixed by #5651

Comments

@ndmitchell
Copy link

In our project we have a bunch of types generated from proc-macros. Recently (probably since #6016) all such imports are flagged as unresolved import. That seems to be an expected consequence. Is there any way to disable such errors, as it makes it vastly harder to see real errors from Rust Analyzer.

@bjorn3
Copy link
Member

bjorn3 commented Sep 21, 2020

"rust-analyzer.diagnostics.disabled": ["unresolved-import"] should work I think.

@bjorn3
Copy link
Member

bjorn3 commented Sep 21, 2020

Also you can enable proc-macro support using

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

@andrewwhitehead
Copy link

This just started showing up for me when importing types from the typenum crate. Strangely, if I change it to an unused import (use typenum::U16 when only U32 is referenced), that error seems to take priority over the unresolved type error.

@ecton
Copy link

ecton commented Sep 21, 2020

I just started having this show up with use serde_derive::{Serialize, Deserialize}; -- given Serde's popularity, I suspect many others will run into this one, and the main reason for my comment is to try to help make it findable.

@bjorn3's suggestion of enabling procMacro support worked for me to alleviate the issue.

@jonas-schievink
Copy link
Contributor

Is there any way to disable such errors, as it makes it vastly harder to see real errors from Rust Analyzer.

In the settings, add unresolved-import to the list of disabled diagnostics

@Veetaha
Copy link
Contributor

Veetaha commented Oct 1, 2020

I confirm that proc-macro-derives stopped working for me recently, not sure when exactly, but there should be some definite regression. All the necessary configurations are in place (loadOutDirsFromCheck and procMacro.enable):
image

@lnicola
Copy link
Member

lnicola commented Oct 1, 2020

@Veetaha are you using nightly?

@Veetaha
Copy link
Contributor

Veetaha commented Oct 1, 2020

Yes, I always do

@lnicola
Copy link
Member

lnicola commented Oct 1, 2020

#5651

Oh, it looks like I need to rebase it.

@suchapalaver
Copy link

"rust-analyzer.diagnostics.disabled": ["unresolved-import"] should work I think.

Could somebody tell a noob like me where this should go? I've just been working on a beginner project working with JSON files and my structs all have this proc macro unresolved import message.

@lnicola
Copy link
Member

lnicola commented Oct 26, 2021

@suchapalaver which editor are you using? In Code, you can put it in settings.json (there's a command to open it).

And can you maybe share your code (or a reduced version) and compiler version?

@suchapalaver
Copy link

@lnicola, I'm using Emacs.

≥ rustc --version 11:28 rustc 1.54.0 (a178d0322 2021-07-26)

Here's some code:

use serde::{Deserialize, Serialize};
use std::{fs::{self, File}, io::{self, BufReader, Result}, path::Path};

#[derive(Serialize, Deserialize, Debug)]
pub struct Groceries {
    sections: Vec<Section>,
}
impl Groceries {
    fn write_to_file<P: AsRef<Path>>(groceries: Groceries, path: P) -> Result<()> {
        let json: String = serde_json::to_string(&groceries).unwrap();
	fs::write(path, &json).expect("Unable to write file");
	Ok(())
    }
    fn read_file<P: AsRef<Path>>(path: P) -> Result<Groceries> {
        // Open the file in read-only mode with buffer.
        let reader = BufReader::new(File::open(path).expect("file issue"));
        let groceries: Groceries = serde_json::from_reader(reader).expect("reader issue");
        Ok(groceries)
    }
}

The messages say proc macro 'Deserialize'/'Serialize' not expanded [unresolved-proc-macro].
Thanks for getting back to me!

@lnicola
Copy link
Member

lnicola commented Oct 26, 2021

@suchapalaver yeah, that should work, even in 1.54:

image

Are you maybe using an older version of rust-analyzer?

@suchapalaver
Copy link

suchapalaver commented Oct 26, 2021

@lnicola , yeah, I was able to do quite a bit of updating, of rustc, my emacs packages related to LSP, and rust analyzer itself. Thanks for the lesson in keeping track of those.
Unfortunately it didn't change anything in the behavior of these messages about proc macros.

@lnicola
Copy link
Member

lnicola commented Oct 26, 2021

Hmm, do you have any rust-analyzer settings configured? Does it log anything and does the proc macro server start (you should see two rust-analyzer processes)?

@suchapalaver
Copy link

Hmm, do you have any rust-analyzer settings configured? Does it log anything and does the proc macro server start (you should see two rust-analyzer processes)?

I don't have any rust-analyzer settings configured.
Here's what logs when I get started.

LSP :: Connected to [rust-analyzer:124269/starting].
LSP :: rust-analyzer:124269 initialized successfully in folders: (/home/jojo/Rust/projects/groceries)
LSP :: Error from the Language Server: waiting for cargo metadata or cargo check (Unknown error) [6 times]

@lnicola
Copy link
Member

lnicola commented Oct 26, 2021

Summoning our emacs user, @flodiebold 😅.

@flodiebold
Copy link
Member

It works fine for me in Emacs as well. What do *lsp-log* and *rust-analyzer::stderr* contain?

@suchapalaver
Copy link

@flodiebold and @lnicola , I had to look up how to find *lsp-log* and *rust-analyzer::stderr*. But in the process of looking I seem to have fixed it in the lsp-mode settings, following the instructions from here:

"Look for lsp-mode variable to customize server path. Usually, you may find the variable by doing: M-x customize-group RET lsp-LANGUAGE-SERVER-ID", i.e. lsp-rust-analyzer.

In those settings I found Lsp Rust Analyzer Macro Expansion Method and Lsp Rust Analyzer Proc Macro Enable, both of which were toggled to off. Having switched them to on and restarted Emacs, those warnings about proc macros have gone :)

@spikespaz
Copy link

I'm getting this, not sure if it's related but when I search "rust-analyzer unresolved import on macro" this is where I'm sent.

#[cfg(test)]
mod tests {
    use test_case::test_case;

Not breaking anything, but a little annoying. Tips?

@BrandonDyer64
Copy link

Why was this issue closed? It's still very much a problem.

Also, setting loadOutDirsFromCheck and procMacro.enable doesn't help.

@lnicola
Copy link
Member

lnicola commented Mar 18, 2022

@BrandonDyer64 because the root cause here (a change to the proc macro ABI) was fixed in #5651. It affected nightlies from the Rust 1.47. This was not the first proc macro interface change and it wasn't the last, but it makes no sense to track all of them in a single issue.

If macros are not working for you, please file a new one, with more information (compiler version, test case, whether no macros work at all, or some of them do etc.).

@BrandonDyer64
Copy link

@lnicola The trouble is though, that my issue would be literally this issue. It's exactly the same problem. I have an attribute proc macro that goes over a function and generates a struct. The resulting struct is not seen by rust-analyzer and is shown as an unresolved import.

@lnicola
Copy link
Member

lnicola commented Mar 18, 2022

It can't be the same problem unless you're using a post-1.46 toolchain with a two year-old rust-analyzer (in which case you should upgrade).

So you should provide the info I mentioned before.

@BrandonDyer64
Copy link

Just realized I'm stuck on rustc 1.45, so never mind I guess

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

Successfully merging a pull request may close this issue.