rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

`#[path = "../XXXXXX"]` attribute on modules gives false `unresolved-module` error

Open WilliamVenner opened this issue 4 years ago • 16 comments

image

image

Example: https://github.com/WilliamVenner/rust-analyzer-broken-example

Code:

./package/src/main.rs

#[path = "../../silly_mod/mod.rs"]
mod silly_mod;

fn main() {
    silly_mod::hello();
}

./silly_mod/mod.rs

pub fn hello() {
	println!("Hello, world!");
}

WilliamVenner avatar Jun 07 '21 21:06 WilliamVenner

If I try the quick fix, I get this error in console:

ERR Unable to create file 'c:\Users\billy\Documents\GitHub\brokey\silly_mod\mod.rs' that already exists when overwrite flag is not set: Error: Unable to create file 'c:\Users\billy\Documents\GitHub\brokey\silly_mod\mod.rs' that already exists when overwrite flag is not set

WilliamVenner avatar Jun 07 '21 22:06 WilliamVenner

This is a known limitation, I'm not sure if there's already an open issue for it.

lnicola avatar Jun 07 '21 22:06 lnicola

Not one that I could find - is there a reason for this limitation? I noticed there are tests which surprisingly pass with this exact scenario, from what I can gather.

WilliamVenner avatar Jun 07 '21 22:06 WilliamVenner

This test

https://github.com/rust-analyzer/rust-analyzer/blob/1f1a1ce4f57076be38d18f0a6defddb9c690bb1c/crates/hir_def/src/nameres/tests/mod_resolution.rs#L322-L347

WilliamVenner avatar Jun 07 '21 22:06 WilliamVenner

I'm not sure, but maybe we don't load the file.

lnicola avatar Jun 07 '21 22:06 lnicola

Yeah, it seems to work for files that are inside the project. It gets loaded by the VFS once you edit the file, but we still don't resolve the module for some reason.

lnicola avatar Jun 08 '21 08:06 lnicola

I believe that module files need to be part of the same source root at the moment

jonas-schievink avatar Jun 08 '21 10:06 jonas-schievink

I've also just run into this issue, I guess the more idiomatic way to handle this situation is to make the referenced module into a full blown library referenced by path = "../path/to/module" in Cargo.toml?

kellpossible avatar Nov 14 '21 17:11 kellpossible

Hi! I'm also hitting this problem on both stable and nightly. Unfortunately, the solution suggested by @kellpossible won't work for me since I include autogenerated modules in such a way, so putting them all into Cargo.toml would be very messy. Interestingly, if I change the path to the faulty module to a non-existent and try "quick fix", a new file actually is being created, but is still being marked as error.

Please let me know if I can provide any more info, and in the meantime - is there any way to silence this error locally?

Thanks!

curldivergence avatar Nov 27 '21 11:11 curldivergence

@curldivergence my use case sounds similar, auto generated module, I still didn't implement this workaround for the same reason.

kellpossible avatar Nov 29 '21 10:11 kellpossible

I see that there is a test which appears to cover this case? https://github.com/rust-lang/rust-analyzer/blob/ba329913fa33c29d4ccabf46998d3a0cfac57b0c/crates/hir-def/src/nameres/tests/mod_resolution.rs#L322-L347

kellpossible avatar Jun 11 '22 02:06 kellpossible

This issue also affects working on the Rust standard library; it complains about this and this.

@Veykril why is this "unactionable"?

RalfJung avatar Jul 12 '22 13:07 RalfJung

To my knowledge this is a known shortcoming of our VFS, so this is "unactionable" in the sense that it needs design decisions on how to get around this shortcoming.

Veykril avatar Jul 12 '22 13:07 Veykril

Setting "rust-analyzer.diagnostics.disabled": [ "unresolved-module" ] makes this mostly tolerable for me.

idigdoug avatar Aug 24 '22 18:08 idigdoug

Is there any way to suppress the error? I can live with "unknown" for the module content, but the red squiggles are distracting. :)

Yeah, I have this in my settings

    "rust-analyzer.diagnostics.disabled": [
        "unlinked-file", // I guess for "normal" projects unlinked files are strange, for me they are common
        "missing-match-arm", // https://github.com/rust-lang/rust-analyzer/issues/4896
        "type-mismatch", // https://github.com/rust-lang/rust-analyzer/issues/1109
        "unresolved-module", // https://github.com/rust-lang/rust-analyzer/issues/9173
        "unresolved-extern-crate", // https://github.com/rust-lang/rust-analyzer/issues/12926
    ],

RalfJung avatar Aug 24 '22 18:08 RalfJung

Encoutered this same issue because i have a virutal manifest repo because i have a shared folder but when I

#[path = "../../shared/schema_generated.rs"]
mod schema_generated;

for reference this file is at /client/src/main.rs I am annoyed because this is generated and i really would like IntelleSense for the generated code

walksanatora avatar Sep 15 '22 20:09 walksanatora

Maybe the error message could be improved? Currently it says "Cannot find module file", but if the user goes looking then the module file is right there, so that's quite confusing.

FWIW the error that needs to be listed in diagnostics.disabled changed to "E0583".

RalfJung avatar Aug 15 '23 06:08 RalfJung