Search code examples
rustclion

Clion - cannot create a new rust file in the same project


I have just started learning rust. I'm using the CLion IDE and this is giving me some issues. Every time I try to create a new rust file I get a popup saying "File is not included in module tree, analysis is not available". This appears to be a known issue in CLion's rust plugin with the normal solution being to invalidate the cache and restart. However, I have tried that multiple times now and it still doesn't work. enter image description here

Does anyone know of a solution to this issue (or if I am doing something wrong)?


Solution

  • “File is not included in module tree” does not just mean that the IDE cannot analyze the file. It also means that the file won't be compiled (unless you use rustc manually, rather than cargo build).

    In normal Rust development, every source file should belong to a Cargo package. If you want to keep them as simple as possible, the minimum contents of a package are

    Cargo.toml
    src/
        main.rs
    

    (You do not need a separate version control repository for each package.)

    If you have a large set of related programs, you can define them as multiple binary crates within a single package. They will share dependencies declared in one Cargo.toml (this does not increase the output code size) and build files (target/).

    Cargo.toml
    src/
        bin/
           prog1.rs
           prog2.rs
    

    The src/bin/ path is where Cargo automatically looks for files that are the crate roots (file that contains fn main()) for additional binaries beyond the default src/main.rs.

    You can choose to develop programs as single files, without using Cargo, by running rustc on them directly. But it will be difficult to use any libraries (and using Rust without libraries at all is not usually a good plan), and your IDE will not be able to help you because IDEs get important context from Cargo.