Problem
When a project has a binary target, and in the same file declares a module, the compilation will fail with a error[E0583]: file not found for module even though the file exists.
Steps
- Create a package with the following Cargo.toml:
[package]
name = "example"
version = "0.1.0"
edition = "2018"
[dependencies]
[[bin]]
name = "foo"
path = "src/foo.rs"
-
In src/lib.rs add mod foo;
-
In the file src/foo.rs add:
mod bar;
pub fn main() {
dbg!("Hello");
}
- Create the file
src/foo/bar.rs
- Run
cargo build
This results in the following error:
error[E0583]: file not found for module `bar`
--> src/foo.rs:1:5
|
1 | mod bar;
| ^^^
|
= help: name the file either bar.rs or bar/mod.rs inside the directory "src"
Which is very confusing because such a file exists.
Notes
Using stable channel.
Output of cargo version: cargo 1.40.0 (bc8e4c8 2019-11-22)
Problem
When a project has a binary target, and in the same file declares a module, the compilation will fail with a
error[E0583]: file not found for moduleeven though the file exists.Steps
In
src/lib.rsaddmod foo;In the file
src/foo.rsadd:src/foo/bar.rscargo buildThis results in the following error:
Which is very confusing because such a file exists.
Notes
Using stable channel.
Output of
cargo version: cargo 1.40.0 (bc8e4c8 2019-11-22)