rust-lang / rust-lang/cargo

Passing unstable flags only on nightly toolchains

Open
#14,733 5 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-configuration A-rustflags A-rustup A-unstable C-feature-request S-needs-design
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

Problem

I sometimes enable unstable rustc features in my user level config.toml, such as the parallel frontend (-Zthreads).

This works fine when I'm using the nightly toolchain, but when I rustup default stable it causes builds to fail with error: the option Z is only accepted on the nightly compiler until I go comment that flag out in the config.

Proposed Solution

Adding a new target specifier for toolchain name or channel seems like the nicest solution, I'd love to be able to write:

[cfg.nightly]
rustflags = ["-Zthreads=0"]

And have that flag tacked on only when I'm using nightly. I think this works well with the existing behavior for targets and cfgs:

  1. All matching target.<triple>.rustflags and target.<cfg>.rustflags config entries joined together.

Theoretically you could also introduce a cfg type for the current toolchain so you could write:

[target.'cfg(toolchain = "nightly")']
rustflags = ["-Zthreads=0"]

... but I'm not sure that it makes sense given that code generally isn't aware of what toolchain it's built under, not to mention that adding a new cfg type seems like a bigger change than is necessary.

Notes

This question was asked on the rust discord around a year ago (by @RocketPrinter) and @danielhenrymantilla suggested the following workaround:

Something you could do is setup a rustc_wrapper.sh script which would do something along the following lines:

#!/bin/sh
if "$1" -V | grep -q nightly; then
   "$@" -Z ...
else
   "$@"
fi

I'm currently using this, but it feels gross to rely on the path of the running rustc and in the long term it'd be nice to not need a wrapper.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Cargo's config reference, especially target-specific rustflags, and reproduce the reported behavior with a user-level config.toml under stable and nightly toolchains. Compare the proposed [cfg.nightly] entry with the alternative toolchain cfg syntax and the documented rustc_wrapper.sh workaround. Done means unstable flags apply only to nightly without manual config changes or a wrapper.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.