Skip to content

Indicate when build errors occur in build.rs, rather than in main compilation #10834

Closed
@andrewring

Description

@andrewring

When compilation errors occur during build.rs execution, there is no indication in the error output that the error occurred at that stage. This can cause confusing behavior that is difficult to debug.

I encountered this issue when adding serde serialization to an enum which was also imported in build.rs to generate tab completions for clap. Despite using serde in another module, I was seeing missing crate errors. This turned out to be very time consuming to track down, and wasn't found until I tried to make a sanitized reproduction of the problem to share with others after initial consultation in the rust Discord was unsuccessful in debugging the issue.

To avoid time lost due to similar issues in the future, the compiler should clearly indicate if the errors were produced while compiling build.rs, rather than the main [[bin]] or [[lib]].

Given the attached code:
example.zip

The current output is:

   Compiling example v0.1.0 (/home/andrewring/CLionProjects/serde_error)
error[E0433]: failed to resolve: use of undeclared crate or module `serde`
 --> src/sometimes_working/mod.rs:1:30
  |
1 | #[derive(Clone, Debug, Hash, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
  |                              ^^^^^ use of undeclared crate or module `serde`

error[E0433]: failed to resolve: use of undeclared crate or module `serde`
 --> src/sometimes_working/mod.rs:1:50
  |
1 | #[derive(Clone, Debug, Hash, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
  |                                                  ^^^^^ use of undeclared crate or module `serde`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example` due to 2 previous errors

Ideally the output should look like:

   Compiling example v0.1.0 (/home/andrewring/CLionProjects/serde_error)
encountered errors when compiling `build.rs`:
error[E0433]: failed to resolve: use of undeclared crate or module `serde`
 --> src/sometimes_working/mod.rs:1:30
  |
1 | #[derive(Clone, Debug, Hash, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
  |                              ^^^^^ use of undeclared crate or module `serde`

error[E0433]: failed to resolve: use of undeclared crate or module `serde`
 --> src/sometimes_working/mod.rs:1:50
  |
1 | #[derive(Clone, Debug, Hash, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
  |                                                  ^^^^^ use of undeclared crate or module `serde`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example` due to 2 previous errors while compiling `build.rs`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.E-easyExperience: Easy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions