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

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

Closed
andrewring opened this issue Jul 8, 2022 · 5 comments · Fixed by #11636
Closed

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

andrewring opened this issue Jul 8, 2022 · 5 comments · Fixed by #11636
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. E-easy Experience: Easy

Comments

@andrewring
Copy link

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`
@ehuss ehuss transferred this issue from rust-lang/rust Jul 8, 2022
@ehuss
Copy link
Contributor

ehuss commented Jul 8, 2022

Transferred to rust-lang/cargo as this is likely something cargo would need to handle.

@ehuss ehuss added A-diagnostics Area: Error and warning messages generated by Cargo itself. and removed A-diagnostics labels Jul 8, 2022
@weihanglo
Copy link
Member

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...

@ehuss
Copy link
Contributor

ehuss commented Jan 20, 2023

See also #11598 for some discussion.

@kraktus
Copy link

kraktus commented Jan 20, 2023

Coming from #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.

@weihanglo
Copy link
Member

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.

@weihanglo weihanglo added E-easy Experience: Easy E-mentor labels Jan 23, 2023
aortizpimentel added a commit to aortizpimentel/cargo that referenced this issue Jan 27, 2023
aortizpimentel added a commit to aortizpimentel/cargo that referenced this issue Jan 28, 2023
aortizpimentel added a commit to aortizpimentel/cargo that referenced this issue Mar 7, 2023
- Adding display of which target failed to compile
- Consistent messages for warnings/errors.
- Fixing assertions on related tests.
@bors bors closed this as completed in 66789e5 Mar 7, 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. E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants