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

cargo build fails with RUSTFLAGS="--error-format=json" #12486

Closed
sourcefrog opened this issue Aug 12, 2023 · 4 comments
Closed

cargo build fails with RUSTFLAGS="--error-format=json" #12486

sourcefrog opened this issue Aug 12, 2023 · 4 comments
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@sourcefrog
Copy link
Contributor

sourcefrog commented Aug 12, 2023

Problem

If RUSTFLAGS sets --error-format then cargo-build fails.

Steps

; cargo init demo
     Created binary (application) package
; cd demo
; env RUSTFLAGS="--error-format=json" cargo build
   Compiling demo v0.1.0 (/tmp/demo)
error: Option 'error-format' given more than once

error: could not compile `demo` (bin "demo")

Possible Solution(s)

I haven't looked in the code but I guess something else is setting --error-format in the rust argv and that needs to be reconciled with or override RUSTFLAGS.

Notes

This was reported in sourcefrog/cargo-mutants#136 but it turns out the behavior is easily reproduced in cargo alone.

This looks like #11452 (but it's not about --target) and #6375 (but it's not about build.rs or cross compilation), so I think it's a distinct new bug, but perhaps the solution is the same.

Version

; cargo version --verbose
cargo 1.72.0-beta.6 (44b6be4bd 2023-08-03)
release: 1.72.0-beta.6
commit-hash: 44b6be4bdf2cd7d3f4d4cb266bfe428dfc2a7952
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.1.2-DEV (sys:0.4.63+curl-8.1.2 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Pop!_OS 22.04 (jammy) [64-bit]

It fails the same way on nightly:

; cargo +nightly version --verbose
cargo 1.73.0-nightly (d78bbf4bd 2023-08-03)
release: 1.73.0-nightly
commit-hash: d78bbf4bde3c6b95caca7512f537c6f9721426ff
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.2.1-DEV (sys:0.4.65+curl-8.2.1 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Pop!_OS 22.04 (jammy) [64-bit]
bobcat /tmp/demo (main) 08:05
@ehuss
Copy link
Contributor

ehuss commented Aug 12, 2023

If you want JSON output, the correct way to do that is cargo --message-format=json. Generally, using RUSTFLAGS should only be done for very advanced use cases, in situations where it is understood how the flags will interact with cargo (and understand that it may break in the future).

@arlosi
Copy link
Contributor

arlosi commented Aug 13, 2023

Cargo internally sets --error-format when running rustc, so setting it via RUSTFLAGS causing an error is expected.

As ehuss said, the correct way to get JSON output from Cargo is cargo --message-format=json

@arlosi arlosi closed this as completed Aug 13, 2023
@sourcefrog
Copy link
Contributor Author

Should I send a PR updating https://doc.rust-lang.org/cargo/reference/environment-variables.html to add some kind of warning?

@weihanglo
Copy link
Member

I remember we have a such a notice already in https://doc.rust-lang.org/nightly/cargo/reference/config.html#buildrustflags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

4 participants