Expand description
Support for nightly features in Cargo itself.
This file is the version of feature_gate.rs
in upstream Rust for Cargo
itself and is intended to be the avenue for which new features in Cargo are
gated by default and then eventually stabilized. All known stable and
unstable features are tracked in this file.
If you’re reading this then you’re likely interested in adding a feature to Cargo, and the good news is that it shouldn’t be too hard! First determine how the feature should be gated:
- Error when the feature is used without the gate
- Required if ignoring the feature violates the users intent in non-superficial ways
- A low-effort / safe way to protect the user from being broken if the format of the feature changes in incompatible was (can be worked around)
- Good for: CLI (gate:
-Zunstable-options
or-Z
if combined with other changes),Cargo.toml
(gate:cargo-features
)
- Warn that the feature is ignored due to lack of the gate
- For if you could opt-in to the unimplemented feature on Cargo today and Cargo would operate just fine
- If gate is not enabled, prefer to warn if the format of the feature is incompatible (instead of error or ignore)
- Good for:
Cargo.toml
,.cargo/config.toml
,config.json
index file (gate:-Z
)
- Ignore the feature that is used without a gate
- For when ignoring the feature has so little impact that annoying the user is not worth it (e.g. a config field that changes Cargo’s terminal output)
- For behavior changes without an interface (e.g. the resolver)
- Good for:
.cargo/config.toml
,config.json
index file (gate:-Z
)
For features that touch multiple parts of Cargo, multiple feature gating strategies (error,
warn, ignore) and mechanisms (-Z
, cargo-features
) may be used.
When adding new tests for your feature, usually the tests should go into a new module of the testsuite named after the feature. See https://doc.crates.io/contrib/tests/writing.html for more information on writing tests. Particularly, check out the “Testing Nightly Features” section for testing unstable features. Be sure to test the feature gate itself.
After you have added your feature, be sure to update the unstable
documentation at src/doc/src/reference/unstable.md
to include a short
description of how to use your new feature.
And hopefully that’s it!
§cargo-features
The steps for adding new Cargo.toml syntax are:
-
Add the cargo-features unstable gate. Search the code below for “look here” to find the
features!
macro invocation and add your feature to the list. -
Update the Cargo.toml parsing code to handle your new feature.
-
Wherever you added the new parsing code, call
features.require(Feature::my_feature_name())?
if the new syntax is used. This will return an error if the user hasn’t listed the feature incargo-features
or this is not the nightly channel.
§-Z unstable-options
-Z unstable-options
is intended to force the user to opt-in to new CLI
flags, options, and new subcommands.
The steps to add a new command-line option are:
- Add the option to the CLI parsing code. In the help text, be sure to
include
(unstable)
to note that this is an unstable option. - Where the CLI option is loaded, be sure to call
CliUnstable::fail_if_stable_opt
. This will return an error if-Z unstable options
was not passed.
§-Z
options
New -Z
options cover all other functionality that isn’t covered with
cargo-features
or -Z unstable-options
.
The steps to add a new -Z
option are:
- Add the option to the
CliUnstable
struct in the macro invocation ofunstable_cli_options!
. Flags can take an optional value if you want. - Update the
CliUnstable::add
function to parse the flag. - Wherever the new functionality is implemented, call
GlobalContext::cli_unstable
to get an instance ofCliUnstable
and check if the option has been enabled on theCliUnstable
instance. Nightly gating is already handled, so no need to worry about that. If warning when feature is used without the gate, be sure to gracefully degrade (with a warning) when theCargo.toml
/.cargo/config.toml
field usage doesn’t match the schema. - For any
Cargo.toml
fields, strip them inprepare_for_publish
if the gate isn’t set
§Stabilization
For the stabilization process, see https://doc.crates.io/contrib/process/unstable.html#stabilization.
The steps for stabilizing are roughly:
- Update the feature to be stable, based on the kind of feature:
cargo-features
: Change the feature tostable
in thefeatures!
macro invocation below, and include the version and a URL for the documentation.-Z unstable-options
: Find the call tofail_if_stable_opt
and remove it. Be sure to update the man pages if necessary.-Z
flag: Change the parsing code inCliUnstable::add
to callstabilized_warn
orstabilized_err
and remove the field fromCliUnstable
. Remove the(unstable)
note in the clap help text if necessary.- Remove
masquerade_as_nightly_cargo
from any tests, and removecargo-features
fromCargo.toml
test files if any. You can quickly find what needs to be removed by searching for the name of the feature, e.g.print_im_a_teapot
- Update the docs in unstable.md to move the section to the bottom and summarize it similar to the other entries. Update the rest of the documentation to add the new feature.
Macros§
- features 🔒A listing of stable and unstable new syntax in Cargo.toml.
- stab 🔒
- Generates
-Z
flags as fields ofCliUnstable
.
Structs§
- A parsed representation of all unstable flags that Cargo accepts.
- Status and metadata for a single unstable feature.
- Unstable feature context for querying if a new Cargo.toml syntax is allowed to use.
Enums§
Constants§
Functions§
- Generate a link to Cargo documentation for the current release channel
path
is the URL component afterhttps://doc.rust-lang.org/{channel}/cargo/
- Only for testing and developing. See “Running with gitoxide as default git backend in tests”.
- Returns the current release channel (“stable”, “beta”, “nightly”, “dev”).
Type Aliases§
- Value of
allow-features