cargo
cargo copied to clipboard
Indicate when build errors occur in `build.rs`, rather than in main compilation
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`
Transferred to rust-lang/cargo as this is likely something cargo would need to handle.
Tip: You can run cargo build --verbose
to help you trace the error, though interpreting them need a certain level of familiarity with rustc
.
Generally, when seeing rustc --crate-name build_script_xxxxx
, it implies that you're building a build script. The "Caused by" message told that it failed to compile build script, so diagnostics above probably belong to it.
$ cargo b --verbose
Fresh unicode-ident v1.0.1
Compiling example v0.1.0 (/projects/example)
Compiling ...
Compiling serde v1.0.139
+ Running `rustc --crate-name build_script_build --edition=2021 build.rs --error-format=json
+ --json=diagnostic-rendered-ansi,artifacts,fu ture-incompat --crate-type bin
+ --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2
+ -C metadata=39a8aabaeae1d ec5 -C extra-filename=-39a8aabaeae1dec5 --out-dir
+ /projects/example/target/debug/build/example-39a8aabaeae1dec5 -C
+ incremental=/projects/example/target/debug/incremental
+ -L dependency=/projects/example/target/debug/deps`
Running ...
Running ...
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
+ Caused by:
+ process didn't exit successfully: `rustc --crate-name build_script_build --edition=2021
+ build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat
+ --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked
+ -C debuginfo=2 -C metadata=39a8aabaeae1dec5 -C extra-filename=-39a8aabaeae1dec5
+ --out-dir /projects/example/target/debug/build/example-39a8aabaeae1dec5
+ -C incremental=/projects/example/target/debug/incremental
+ -L dependency=/projects/example/target/debug/deps` (exit status: 1)
warning: build failed, waiting for other jobs to finish...
See also #11598 for some discussion.
Coming from https://github.com/rust-lang/cargo/issues/11598, when you hop in a new codebase or one you've not worked for a bit of time, it's very easy not to think about the build
script first, and I don't have the habit of using the verbose command since errors reported are usually enough to understand the situation.
Now I feel like this approach is doable. If anyone is interested in contributing, here is one of the solutions.
Cargo is already more informative when indicating what is being warned. You can read from this piece of code. We can reuse that in the error path here. The error message would look like the following:
Compiling foo v0.1.0 (/home/user/projects/foo)
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 `foo` due to previous error
+ error: could not compile `foo` (build-script) due to previous error
If you have any question, please don't hesitate to ask here or ping me on Zulip.