Skip to content

Conversation

@ehuss
Copy link
Contributor

@ehuss ehuss commented Jan 25, 2026

This adds the -Z json-target-spec CLI flag to assist with using custom .json target spec files. rustc recently switched so that it requires -Z unstable-options when using custom spec files (rust-lang/rust#151534). This can make it rather awkward to use spec files with cargo because it then requires setting RUSTFLAGS and RUSTDOCFLAGS to pass -Zunstable-options. It also ends up causing some confusing error messages.

See the individual commits for more details.

This ended up being a larger diff than I wanted. I'm not 100% certain this is worth it, but I think it significantly improves the experience using .json files, so I I'm leaning towards it.

How to test and review this PR?

Testing can be done with rustc built from rust-lang/rust#151534 to ensure that everything passes after that PR (including setting CARGO_RUN_BUILD_STD_TESTS=1).

ehuss added 3 commits January 24, 2026 17:30
This changes the index parsing code to pass around CliUnstable instead
of the bindeps bool to make it easier to check for multiple unstable
options.
This changes CompileTarget to an enum to better differentiate when it is
a JSON path versus when it is a target tuple. This should help ensure
that the correct behavior is being used without constantly needing to
check if the suffix is `.json`.

This also adds an `CompileKind::add_target_arg` helper to centralize
where the `--target` arg adding is done.
This adds the `-Z json-target-spec` CLI flag to assist with using custom
`.json` target spec files. `rustc` recently switched so that it requires
`-Z unstable-options` when using custom spec files
(rust-lang/rust#151534). This can make it rather
awkward to use spec files with cargo because it then requires setting
RUSTFLAGS and RUSTDOCFLAGS to pass `-Zunstable-options`. It also ends up
causing some confusing error messages.

Now, using `--target` with `.json` extension files generates an error
that explains you need `-Zjson-target-spec`. The only thing this flag
does is disable that error, and automatically passes
`-Zunstable-options` with the `--target` flag.

This does not 100% cover json target spec files, because they can be
placed in RUST_TARGET_PATH or the sysroot, and `rustc` will
automatically search for them (without the `.json` extension in the
`--target` arg). The user will just need to use RUSTFLAGS/RUSTDOCFLAGS
in that situation (but I expect that to be rare).

The majority of this change is changing `CompileTarget::new` to take a
flag if `-Zjson-target-spec` is enabled, and then threading through all
the places that call it. `CompileTarget::new` is responsible for
generating an error if json is used without the Z flag.
@rustbot
Copy link
Collaborator

rustbot commented Jan 25, 2026

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-crate-dependencies Area: [dependencies] of any kind A-dependency-resolution Area: dependency resolution and the resolver A-documenting-cargo-itself Area: Cargo's documentation A-features2 Area: issues specifically related to the v2 feature resolver A-manifest Area: Cargo.toml issues A-registries Area: registries A-unstable Area: nightly unstable support Command-test S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 25, 2026
@epage epage added this pull request to the merge queue Jan 25, 2026
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weihanglo
Copy link
Member

Ah Ed got it already

Merged via the queue into rust-lang:master with commit 3a1b3fc Jan 25, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 25, 2026
ranger-ross pushed a commit to ranger-ross/cargo that referenced this pull request Jan 25, 2026
This adds the `-Z json-target-spec` CLI flag to assist with using custom
`.json` target spec files. `rustc` recently switched so that it requires
`-Z unstable-options` when using custom spec files
(rust-lang/rust#151534). This can make it rather
awkward to use spec files with cargo because it then requires setting
RUSTFLAGS and RUSTDOCFLAGS to pass `-Zunstable-options`. It also ends up
causing some confusing error messages.

See the individual commits for more details.

This ended up being a larger diff than I wanted. I'm not 100% certain
this is worth it, but I think it significantly improves the experience
using `.json` files, so I I'm leaning towards it.

Testing can be done with `rustc` built from
rust-lang/rust#151534 to ensure that everything
passes after that PR (including setting `CARGO_RUN_BUILD_STD_TESTS=1`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-crate-dependencies Area: [dependencies] of any kind A-dependency-resolution Area: dependency resolution and the resolver A-documenting-cargo-itself Area: Cargo's documentation A-features2 Area: issues specifically related to the v2 feature resolver A-manifest Area: Cargo.toml issues A-registries Area: registries A-unstable Area: nightly unstable support Command-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants