|
6 | 6 | //! unstable features are tracked in this file.
|
7 | 7 | //!
|
8 | 8 | //! If you're reading this then you're likely interested in adding a feature to
|
9 |
| -//! Cargo, and the good news is that it shouldn't be too hard! To do this you'll |
10 |
| -//! want to follow these steps: |
| 9 | +//! Cargo, and the good news is that it shouldn't be too hard! First determine |
| 10 | +//! how the feature should be gated: |
11 | 11 | //!
|
12 |
| -//! 1. Add your feature. Do this by searching for "look here" in this file and |
13 |
| -//! expanding the macro invocation that lists all features with your new |
14 |
| -//! feature. |
| 12 | +//! * New syntax in Cargo.toml should use `cargo-features`. |
| 13 | +//! * New CLI options should use `-Z unstable-options`. |
| 14 | +//! * New functionality that may not have an interface, or the interface has |
| 15 | +//! not yet been designed, or for more complex features that affect multiple |
| 16 | +//! parts of Cargo should use a new `-Z` flag. |
| 17 | +//! |
| 18 | +//! See below for more details. |
| 19 | +//! |
| 20 | +//! When adding new tests for your feature, usually the tests should go into a |
| 21 | +//! new module of the testsuite. See |
| 22 | +//! <https://doc.crates.io/contrib/tests/writing.html> for more information on |
| 23 | +//! writing tests. Particularly, check out the "Testing Nightly Features" |
| 24 | +//! section for testing unstable features. |
| 25 | +//! |
| 26 | +//! After you have added your feature, be sure to update the unstable |
| 27 | +//! documentation at `src/doc/src/reference/unstable.md` to include a short |
| 28 | +//! description of how to use your new feature. |
| 29 | +//! |
| 30 | +//! And hopefully that's it! |
| 31 | +//! |
| 32 | +//! ## New Cargo.toml syntax |
| 33 | +//! |
| 34 | +//! The steps for adding new Cargo.toml syntax are: |
| 35 | +//! |
| 36 | +//! 1. Add the cargo-features unstable gate. Search below for "look here" to |
| 37 | +//! find the `features!` macro and add your feature to the list. |
| 38 | +//! |
| 39 | +//! 2. Update the Cargo.toml parsing code to handle your new feature. |
| 40 | +//! |
| 41 | +//! 3. Wherever you added the new parsing code, call |
| 42 | +//! `features.require(Feature::my_feature_name())?` if the new syntax is |
| 43 | +//! used. This will return an error if the user hasn't listed the feature |
| 44 | +//! in `cargo-features` or this is not the nightly channel. |
| 45 | +//! |
| 46 | +//! ## `-Z unstable-options` |
15 | 47 | //!
|
16 |
| -//! 2. Find the appropriate place to place the feature gate in Cargo itself. If |
17 |
| -//! you're extending the manifest format you'll likely just want to modify |
18 |
| -//! the `Manifest::feature_gate` function, but otherwise you may wish to |
19 |
| -//! place the feature gate elsewhere in Cargo. |
| 48 | +//! `-Z unstable-options` is intended to force the user to opt-in to new CLI |
| 49 | +//! flags, options, and new subcommands. |
20 | 50 | //!
|
21 |
| -//! 3. To actually perform the feature gate, you'll want to have code that looks |
22 |
| -//! like: |
| 51 | +//! The steps to add a new command-line option are: |
23 | 52 | //!
|
24 |
| -//! ```rust,compile_fail |
25 |
| -//! use core::{Feature, Features}; |
| 53 | +//! 1. Add the option to the CLI parsing code. In the help text, be sure to |
| 54 | +//! include `(unstable)` to note that this is an unstable option. |
| 55 | +//! 2. Where the CLI option is loaded, be sure to call |
| 56 | +//! [`CliUnstable::fail_if_stable_opt`]. This will return an error if `-Z |
| 57 | +//! unstable options` was not passed. |
26 | 58 | //!
|
27 |
| -//! let feature = Feature::launch_into_space(); |
28 |
| -//! package.manifest().unstable_features().require(feature).chain_err(|| { |
29 |
| -//! "launching Cargo into space right now is unstable and may result in \ |
30 |
| -//! unintended damage to your codebase, use with caution" |
31 |
| -//! })?; |
32 |
| -//! ``` |
| 59 | +//! ## `-Z` options |
33 | 60 | //!
|
34 |
| -//! Notably you'll notice the `require` function called with your `Feature`, and |
35 |
| -//! then you use `chain_err` to tack on more context for why the feature was |
36 |
| -//! required when the feature isn't activated. |
| 61 | +//! The steps to add a new `-Z` option are: |
37 | 62 | //!
|
38 |
| -//! 4. Update the unstable documentation at |
39 |
| -//! `src/doc/src/reference/unstable.md` to include a short description of |
40 |
| -//! how to use your new feature. When the feature is stabilized, be sure |
41 |
| -//! that the Cargo Guide or Reference is updated to fully document the |
42 |
| -//! feature and remove the entry from the Unstable section. |
| 63 | +//! 1. Add the option to the [`CliUnstable`] struct below. Flags can take an |
| 64 | +//! optional value if you want. |
| 65 | +//! 2. Update the [`CliUnstable::add`] function to parse the flag. |
| 66 | +//! 3. Wherever the new functionality is implemented, call |
| 67 | +//! [`Config::cli_unstable`][crate::util::config::Config::cli_unstable] to |
| 68 | +//! get an instance of `CliUnstable` and check if the option has been |
| 69 | +//! enabled on the `CliUnstable` instance. Nightly gating is already |
| 70 | +//! handled, so no need to worry about that. |
| 71 | +//! 4. Update the `-Z help` documentation in the `main` function. |
43 | 72 | //!
|
44 |
| -//! And hopefully that's it! Bear with us though that this is, at the time of |
45 |
| -//! this writing, a very new feature in Cargo. If the process differs from this |
46 |
| -//! we'll be sure to update this documentation! |
| 73 | +//! ## Stabilization |
| 74 | +//! |
| 75 | +//! For the stabilization process, see |
| 76 | +//! <https://doc.crates.io/contrib/process/unstable.html#stabilization>. |
| 77 | +//! |
| 78 | +//! The steps for stabilizing are roughly: |
| 79 | +//! |
| 80 | +//! 1. Update the feature to be stable, based on the kind of feature: |
| 81 | +//! 1. `cargo-features`: Change the feature to `stable` in the `features!` |
| 82 | +//! macro below. |
| 83 | +//! 2. `-Z unstable-options`: Find the call to `fail_if_stable_opt` and |
| 84 | +//! remove it. Be sure to update the man pages if necessary. |
| 85 | +//! 3. `-Z` flag: Change the parsing code in [`CliUnstable::add`] to call |
| 86 | +//! `stabilized_warn` or `stabilized_err`. Remove it from the `-Z help` |
| 87 | +//! docs in the `main` function. Remove the `(unstable)` note in the |
| 88 | +//! clap help text if necessary. |
| 89 | +//! 2. Remove `masquerade_as_nightly_cargo` from any tests, and remove |
| 90 | +//! `cargo-features` from `Cargo.toml` test files if any. |
| 91 | +//! 3. Remove the docs from unstable.md and update the redirect at the bottom |
| 92 | +//! of that page. Update the rest of the documentation to add the new |
| 93 | +//! feature. |
47 | 94 |
|
48 | 95 | use std::cell::Cell;
|
49 | 96 | use std::env;
|
@@ -367,26 +414,6 @@ impl Features {
|
367 | 414 | /// Cargo, like `rustc`, accepts a suite of `-Z` flags which are intended for
|
368 | 415 | /// gating unstable functionality to Cargo. These flags are only available on
|
369 | 416 | /// the nightly channel of Cargo.
|
370 |
| -/// |
371 |
| -/// This struct doesn't have quite the same convenience macro that the features |
372 |
| -/// have above, but the procedure should still be relatively stable for adding a |
373 |
| -/// new unstable flag: |
374 |
| -/// |
375 |
| -/// 1. First, add a field to this `CliUnstable` structure. All flags are allowed |
376 |
| -/// to have a value as the `-Z` flags are either of the form `-Z foo` or |
377 |
| -/// `-Z foo=bar`, and it's up to you how to parse `bar`. |
378 |
| -/// |
379 |
| -/// 2. Add an arm to the match statement in `CliUnstable::add` below to match on |
380 |
| -/// your new flag. The key (`k`) is what you're matching on and the value is |
381 |
| -/// in `v`. |
382 |
| -/// |
383 |
| -/// 3. (optional) Add a new parsing function to parse your datatype. As of now |
384 |
| -/// there's an example for `bool`, but more can be added! |
385 |
| -/// |
386 |
| -/// 4. In Cargo use `config.cli_unstable()` to get a reference to this structure |
387 |
| -/// and then test for your flag or your value and act accordingly. |
388 |
| -/// |
389 |
| -/// If you have any trouble with this, please let us know! |
390 | 417 | #[derive(Default, Debug, Deserialize)]
|
391 | 418 | #[serde(default, rename_all = "kebab-case")]
|
392 | 419 | pub struct CliUnstable {
|
|
0 commit comments