Skip to content
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

Confusing import errors for build.rs script #11598

Closed
kraktus opened this issue Jan 19, 2023 · 3 comments
Closed

Confusing import errors for build.rs script #11598

kraktus opened this issue Jan 19, 2023 · 3 comments
Labels
A-build-scripts Area: build.rs scripts A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug

Comments

@kraktus
Copy link

kraktus commented Jan 19, 2023

Code

# Cargo.toml
build = "src/build.rs"
// src/build.rs
mod foo;

use crate::foo::Foo;

fn main() -> std::io::Result<()> {
	Foo;
	Ok(())
}
// src/foo.rs
use crate::Lib;

pub struct Foo;
// src/lib.rs
pub mod foo;

pub use foo::Foo;

pub enum Lib {
    A,
    B,
}

fn main() {
    println!("Hello, world!");
}

Current output

error[E0432]: unresolved import `crate::Lib`
 --> src/foo.rs:1:5
  |
1 | use crate::Lib;
  |     ^^^^^^^^^^ no `Lib` in the root

For more information about this error, try `rustc --explain E0432`.

Desired output

I am not sure, but at least should say the error comes from the build script

+ When compiling the build script `src/build.rs`
error[E0432]: unresolved import `crate::Lib`
 --> src/foo.rs:1:5
  |
1 | use crate::Lib;
  |     ^^^^^^^^^^ no `Lib` in the root

For more information about this error, try `rustc --explain E0432`.
+ Help: you may want to declare that module in the build script

Rationale and extra context

This error got me puzzled for a while before I had the thought to look at the build script.

@kraktus kraktus changed the title Confusing error with import errors for build.rs script Confusing import errors for build.rs script Jan 19, 2023
@weihanglo
Copy link
Member

I don't think rustc knows things about build scripts. Let me transfer this to rust-lang/cargo.

@weihanglo weihanglo transferred this issue from rust-lang/rust Jan 19, 2023
@weihanglo weihanglo added C-bug Category: bug A-diagnostics Area: Error and warning messages generated by Cargo itself. A-build-scripts Area: build.rs scripts labels Jan 20, 2023
@weihanglo
Copy link
Member

AFAIK a build script is expected to be built before its package being built1. I usually debug with cargo build --verbose so that I can clearly see it's a crate named build_script_build, which compiles src/build.rs, that failed.

   Compiling issue-11598 v0.1.0 (/home/user/projects/issue-11598)
     Running `rustc --crate-name build_script_build --edition=2021 src/build.rs ...`
error[E0432]: unresolved import `crate::Lib`
 --> src/foo.rs:2:5
  |
2 | use crate::Lib;
  |     ^^^^^^^^^^ no `Lib` in the root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `issue-11598` due to previous error

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_build --edition=2021 src/build.rs ...` (exit status: 1)

I don't really have a good idea at hand. We could possibly denote which kind of target is failing. Cargo warnings already got that logic here. Could possiblly reuse it a bit with a refined message like:

   Compiling issue-11598 v0.1.0 (/home/user/projects/issue-11598)
error[E0432]: unresolved import `crate::Lib`
 --> src/foo.rs:2:5
  |
2 | use crate::Lib;
  |     ^^^^^^^^^^ no `Lib` in the root

For more information about this error, try `rustc --explain E0432`.
+ error: could not compile `issue-11598` (build-script) due to previous error

Footnotes

  1. (https://doc.rust-lang.org/cargo/reference/build-scripts.html#life-cycle-of-a-build-script

@ehuss
Copy link
Contributor

ehuss commented Jan 20, 2023

I think this is essentially the same as #10834, so closing as a duplicate of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants