Skip to content

Spurious "error[E0583]: file not found for module" error. #7730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tkaitchuck opened this issue Dec 20, 2019 · 6 comments
Open

Spurious "error[E0583]: file not found for module" error. #7730

tkaitchuck opened this issue Dec 20, 2019 · 6 comments
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@tkaitchuck
Copy link

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

  1. 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"
  1. In src/lib.rs add mod foo;

  2. In the file src/foo.rs add:

mod bar;

pub fn main() {
    dbg!("Hello");
}
  1. Create the file src/foo/bar.rs
  2. 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)

@tkaitchuck tkaitchuck added the C-bug Category: bug label Dec 20, 2019
@ehuss
Copy link
Contributor

ehuss commented Dec 22, 2019

= help: name the file either bar.rs or bar/mod.rs inside the directory "src"

src/foo/bar.rs does not match either of these (src/bar.rs or src/bar/mod.rs). I'm not sure where the confusion is here. Modules within the root of a crate go into the same directory where the root is defined.

@tkaitchuck
Copy link
Author

tkaitchuck commented Jan 11, 2020

@ehuss It leads to an error reguardless of location.

Let me run through this as a script.
Think: "I want to make a package."
run:

cargo new --lib Example
cd Example/
echo -e "\n[[bin]]\nname = \"foo\"\npath = \"src/foo.rs\"" >> Cargo.toml
echo -e "\nmod foo;" >> src/lib.rs
echo -e 'mod bar;\n\npub fn main() {\n    dbg!("Hello");\n}' >> src/foo.rs
cargo build

Output is:

error[E0583]: file not found for module bar
--> src/foo.rs:1:5
|
1 | mod bar;
| ^^^
|
= help: name the file either foo/bar.rs or foo/bar/mod.rs inside the directory "src"

Think: "Ok great, I'll just create foo/bar.rs because that is missing...
run:

mkdir src/foo
echo -e 'pub fn hi() {\n    dbg!("hi");\n}' > src/foo/bar.rs
cargo build

Output is:

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"

Think: "Humm, I guess bar doesn't go in the foo subdirectory, but rather goes directly in src. It's odd that the earlier error told me to put it in the foo subdirectory."

run:

mv src/foo/bar.rs src/bar.rs
rmdir src/foo/
cargo build

Output is:
error[E0583]: file not found for module bar
--> src/foo.rs:1:5
|
1 | mod bar;
| ^^^
|
= help: name the file either foo/bar.rs or foo/bar/mod.rs inside the directory "src"

Think: "Where the fuck does it go?! I just tried both places!"

@ehuss
Copy link
Contributor

ehuss commented Jan 11, 2020

Ah, I see how that is confusing. I would recommend avoiding mounting the same module between crates and overriding the default project layout (which can help avoid this kind of confusion).

To clarify, there are two crates trying to define a module with the same name in two different places. That is:

library (src/lib.rs) → foo (src/foo.rs) → foo::bar (src/foo/bar.rs)
binary (src/foo.rs) → bar (src/bar.rs)

Typically binaries go in src/bin/ and accessing the library code from the binary should be done via the extern crate (paths prefixed with example:: in this scenario).

@tkaitchuck
Copy link
Author

Is it feasible to detect this situation and give a better error? As it stands results in a very confusing situation, that is easily reachable by starting with a library and then deciding to add a "main" in one of your files and following the instructions for how to add a binary target for cargo.

@ehuss
Copy link
Contributor

ehuss commented Jan 15, 2020

I think it'll be difficult to detect, especially without false positives, since there might be legitimate cases of using the same file in multiple crates. It might be possible to detect if a lib loads the root source file on a binary, and produce a warning.

@ehuss ehuss added A-diagnostics Area: Error and warning messages generated by Cargo itself. and removed C-bug Category: bug labels Feb 2, 2020
@zinking
Copy link

zinking commented Jan 15, 2023

having the mod file to list all is also weird, are there any not to be included? why are they there in the first place.

@epage epage added the S-needs-team-input Status: Needs input from team on whether/how to proceed. label Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

No branches or pull requests

4 participants