Skip to content

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

@tkaitchuck

Description

@tkaitchuck

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.S-needs-team-inputStatus: Needs input from team on whether/how to proceed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions