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

Lib crate isn't recognized

Open Joelius300 opened this issue 5 years ago • 21 comments

I'm currently working through the book and found what seems to be a bug in the VS Code extension. I'm in part 3 of building this cli-application and have just moved some logic from main.rs to lib.rs. Just like it says in the book, I used use minigrep::Config and minigrep::run to refer to the stuff defined in lib.rs. It works perfectly fine if I build/run it with cargo but VS Code shows the following errors:

For use minigrep::Config;

unresolved import `minigrep`

use of undeclared type or module `minigrep` rustc(E0432)
main.rs(4, 5): use of undeclared type or module `minigrep`

For minigrep::run(config):

failed to resolve: use of undeclared type or module `minigrep`

use of undeclared type or module `minigrep` rustc(E0433)
main.rs(16, 21): use of undeclared type or module `minigrep`

Here's the code in main.rs:

use std::env;
use std::process;

use minigrep::Config;

fn main() {
    let args: Vec<String> = env::args().collect();
    let config = Config::new(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });

    println!("Searching for {}", config.query);
    println!("In file {}", config.filename);

    if let Err(e) = minigrep::run(config) {
        println!("Application error: {}", e);

        process::exit(1);
    }
}

Probably unnecessary but here's lib.rs:

use std::fs;
use std::error::Error;

pub struct Config {
    pub query: String,
    pub filename: String
}

impl Config {
    pub fn new(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("Not enough arguments")
        }

        let query = args[1].clone();
        let filename = args[2].clone();

        Ok(Config { query, filename })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.filename)?;

    println!("With text:\n{}", contents);

    Ok(())
}

Again, building and running it with cargo works perfectly fine and the package name defined in Cargo.toml is minigrep, just like it's supposed to be. In case it's not obvious, it was all done step-by-step following the book.

Joelius300 avatar Oct 21 '19 08:10 Joelius300

It would be nice to know how I can work around this issue because VS Code will stop displaying errors for new code as long as there are still old ones. This means I have to manually build the project to see if it compiles. VS Code thinks there are still unfixed errors and doesn't even attempt to compile it again (that's what I'm assuming).

Joelius300 avatar Oct 21 '19 08:10 Joelius300

Same issue here. Running cargo run arg1 arg1 works perfectly but the vscode editor shows errors.

thobens avatar Nov 08 '19 10:11 thobens

Until this issue is fixed in rls-vscode, here is a temporary workaround:

mod lib; //.. instead of 'use minigrep'
use lib::Config; //.. instead of 'use minigrep::Config'

Source: https://stackoverflow.com/a/54789830

ghost avatar Nov 25 '19 22:11 ghost

An alternative solution is to reload as per https://github.com/rust-lang/rls-vscode/issues/715

Newer versions may not suffer from this, although I've not tested it

cortopy avatar Feb 19 '20 17:02 cortopy

Reloading VSCode worked for me

theomeuh avatar Apr 04 '20 21:04 theomeuh

i get these private field errors even after making the respective changesa as mentioned above: Compiling minigrep v0.1.0 (C:\Users\veeks\USERPROFILES\projects\minigrep) error[E0616]: field query of struct minigrep::Config is private --> src\main.rs:13:41 | 13 | println!("Searching for {}", config.query); | ^^^^^ private field

error[E0616]: field filename of struct minigrep::Config is private --> src\main.rs:14:35 | 14 | println!("In file {}", config.filename); | ^^^^^^^^ private field

error: aborting due to 2 previous errors

For more information about this error, try rustc --explain E0616. error: could not compile minigrep.

Veekshith-Thoram avatar Jul 05 '20 17:07 Veekshith-Thoram

i get these private field errors even after making the respective changesa as mentioned above: Compiling minigrep v0.1.0 (C:\Users\veeks\USERPROFILES\projects\minigrep) error[E0616]: field query of struct minigrep::Config is private --> src\main.rs:13:41 | 13 | println!("Searching for {}", config.query); | ^^^^^ private field

error[E0616]: field filename of struct minigrep::Config is private --> src\main.rs:14:35 | 14 | println!("In file {}", config.filename); | ^^^^^^^^ private field

error: aborting due to 2 previous errors

For more information about this error, try rustc --explain E0616. error: could not compile minigrep.

Remember to modify the file lib.rs, designating the variables and functions used in main.rs to pub after spliting that part of code into a seperate file lib.rs.

kongyanye avatar Jul 16 '20 09:07 kongyanye

It is still happening.

sagudev avatar Oct 31 '20 06:10 sagudev

Still a bug, and also discovered whilst working through the book :(

Bizzarely, re-loading VS code also fixed the issue for me.

cythrauL avatar Jan 20 '21 19:01 cythrauL

Seems like most of the people who came here for this bug is doing the RUST BOOK studies 😄 (including me) Hope this bug get fixed in next update

MRGRAVITY817 avatar Feb 28 '21 07:02 MRGRAVITY817

I am getting this as well. Reloading helps.

billtlee avatar Mar 08 '21 09:03 billtlee

Reloading helps for me but i noticed vscode cant "see" the types. I know the intellisense is not that important, but its a really convinient way to look for my modules. For example the vscode does not know the query is a string. Not showing any error but it feels wrong a little. :)

devmetal avatar Mar 22 '21 14:03 devmetal

this is still an issue. however, it's not necessary to reload the full vscode window. press ctrl+shift+p and type "rust", select "Rust: Restart the Rust server".

i feel like this is definitely an issue with the language server not recognizing newly created files whatsoever. this happens all the time whenever you create a new rs file.

DerGernTod avatar Mar 25 '21 12:03 DerGernTod

It is still happening.

ldebello avatar May 03 '21 12:05 ldebello

@DerGernTod I agree, I just hit this and reloading the server seems to pick up new files.

kyle-rader avatar Jul 09 '21 02:07 kyle-rader

It is still happening.

Ted-Jiang avatar Aug 13 '21 11:08 Ted-Jiang

Seems to be a bug with some sort of caching on rust server. It can't detect the creation of a new lib inside de package, thus not finding the new lib as a valid import.

That's the reason why restarting code's window or restarting rust server works, it goes through the files and dependencies to get all valid imports.

Now, this is just a guess, I'm doing the tutorial same as everyone here, but it's something around what I would implement in order to improve performance.

So it's a bug, but can easily be worked around and most likely improves performance by a lot. Since the only way I can personally see to fix this would be to watch the root directory for changes and rebuild de package tree every time a file is changed (or created, but I don't think it's possible do differentiate between the two), I don't see this getting fixed anytime soon (most days you wont be creating new libs anyway).

ABruel avatar Aug 14 '21 13:08 ABruel

You could give rust-analyzer a try instead.

lnicola avatar Aug 14 '21 13:08 lnicola

Cargo.toml add [workspace]

[package] name = "minigrep" version = "0.1.0" edition = "2021"

[workspace] minigrep = ["lib"]

[dependencies]

gjf450005950 avatar Oct 28 '21 15:10 gjf450005950

Still happening, and reloading fix it

pangon avatar Jan 01 '22 15:01 pangon

邮件已经收到,我会尽快回复。

gjf450005950 avatar Jan 01 '22 15:01 gjf450005950