Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Lib crate isn't recognized #686

Open
Joelius300 opened this issue Oct 21, 2019 · 21 comments
Open

Lib crate isn't recognized #686

Joelius300 opened this issue Oct 21, 2019 · 21 comments

Comments

@Joelius300
Copy link

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
Copy link
Author

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

@thobens
Copy link

thobens commented Nov 8, 2019

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

@ghost
Copy link

ghost commented Nov 25, 2019

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

JChanut pushed a commit to JChanut/minigrep-rust that referenced this issue Jan 23, 2020
@cortopy
Copy link

cortopy commented Feb 19, 2020

An alternative solution is to reload as per #715

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

@theomeuh
Copy link

theomeuh commented Apr 4, 2020

Reloading VSCode worked for me

@Veekshith-Thoram
Copy link

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.

@kongyanye
Copy link

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.

@sagudev
Copy link

sagudev commented Oct 31, 2020

It is still happening.

@cythrauL
Copy link

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

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

@MRGRAVITY817
Copy link

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

@billtlee
Copy link

billtlee commented Mar 8, 2021

I am getting this as well. Reloading helps.

@devmetal
Copy link

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

@DerGernTod
Copy link

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.

@ldebello
Copy link

ldebello commented May 3, 2021

It is still happening.

@kyle-rader
Copy link

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

@Ted-Jiang
Copy link

It is still happening.

@ABruel
Copy link

ABruel commented Aug 14, 2021

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

@lnicola
Copy link
Member

lnicola commented Aug 14, 2021

You could give rust-analyzer a try instead.

@gjf450005950
Copy link

gjf450005950 commented Oct 28, 2021

Cargo.toml add [workspace]

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

[workspace]
minigrep = ["lib"]

[dependencies]

@pangon
Copy link

pangon commented Jan 1, 2022

Still happening, and reloading fix it

@gjf450005950
Copy link

gjf450005950 commented Jan 1, 2022 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests