Vscode-rust: Lib crate isn't recognized

Created on 21 Oct 2019  路  14Comments  路  Source: rust-lang/vscode-rust

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_.

Most helpful comment

Reloading VSCode worked for me

All 14 comments

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).

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

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

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

Reloading VSCode worked for me

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

error[E0616]: field filename of struct minigrep::Config is private
--> srcmain.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.

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

error[E0616]: field filename of struct minigrep::Config is private
--> srcmain.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.

It is still happening.

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

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

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

I am getting this as well. Reloading helps.

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. :)

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.

It is still happening.

Was this page helpful?
0 / 5 - 0 ratings