Skip to content

Commit b95e755

Browse files
epagestupendoussuperpowers
authored andcommitted
doc(features): Highlight the non-blocking feature gating technique
We already discussed non-blocking gates but the language makes it sound like it was limited to `config.toml`. Since I haven't been touching that, I had always overlooked that section. This change brings the blocking / non-blocking decision front and center. To support this, the later sections focus more on mechanisms (the gate) rather than on what is being done (new syntax for `cargo-features`). I also feel this makes the content more scannable. This is adapted from what I did for `[lints]` (see rust-lang#12148).
1 parent 9323938 commit b95e755

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

src/cargo/core/features.rs

+28-46
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,43 @@
99
//! Cargo, and the good news is that it shouldn't be too hard! First determine
1010
//! how the feature should be gated:
1111
//!
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.
12+
//! * Error when the feature is used without the gate
13+
//! * Required if ignoring the feature violates the users intent in non-superficial ways
14+
//! * A low-effort / safe way to protect the user from being broken if the format of the feature changes in
15+
//! incompatible was (can be worked around)
16+
//! * Good for: CLI (gate: `-Zunstable-options` or `-Z` if combined with other changes), `Cargo.toml` (gate: `cargo-features`)
17+
//! * Warn that the feature is ignored due to lack of the gate
18+
//! * For if you could opt-in to the unimplemented feature on Cargo today and Cargo would
19+
//! operate just fine
20+
//! * If gate is not enabled, prefer to warn if the format of the feature is incompatible
21+
//! (instead of error or ignore)
22+
//! * Good for: `Cargo.toml`, `.cargo/config.toml`, `config.json` index file (gate: `-Z`)
23+
//! * Ignore the feature that is used without a gate
24+
//! * For when ignoring the feature has so little impact that annoying the user is not worth it
25+
//! (e.g. a config field that changes Cargo's terminal output)
26+
//! * For behavior changes without an interface (e.g. the resolver)
27+
//! * Good for: `.cargo/config.toml`, `config.json` index file (gate: `-Z`)
28+
//!
29+
//! For features that touch multiple parts of Cargo, multiple feature gating strategies (error,
30+
//! warn, ignore) and mechnisms (`-Z`, `cargo-features`) may be used.
1731
//!
1832
//! When adding new tests for your feature, usually the tests should go into a
19-
//! new module of the testsuite. See
33+
//! new module of the testsuite named after the feature. See
2034
//! <https://doc.crates.io/contrib/tests/writing.html> for more information on
2135
//! writing tests. Particularly, check out the "Testing Nightly Features"
22-
//! section for testing unstable features.
36+
//! section for testing unstable features. Be sure to test the feature gate itself.
2337
//!
2438
//! After you have added your feature, be sure to update the unstable
2539
//! documentation at `src/doc/src/reference/unstable.md` to include a short
2640
//! description of how to use your new feature.
2741
//!
2842
//! And hopefully that's it!
2943
//!
30-
//! ## New Cargo.toml syntax
44+
//! ## `cargo-features`
3145
//!
3246
//! The steps for adding new Cargo.toml syntax are:
3347
//!
34-
//! 1. Add the cargo-features unstable gate. Search below for "look here" to
48+
//! 1. Add the cargo-features unstable gate. Search the code below for "look here" to
3549
//! find the [`features!`] macro invocation and add your feature to the list.
3650
//!
3751
//! 2. Update the Cargo.toml parsing code to handle your new feature.
@@ -68,43 +82,10 @@
6882
//! [`Config::cli_unstable`] to get an instance of [`CliUnstable`]
6983
//! and check if the option has been enabled on the [`CliUnstable`] instance.
7084
//! Nightly gating is already handled, so no need to worry about that.
71-
//!
72-
//! ### `-Z` vs `cargo-features`
73-
//!
74-
//! In some cases there might be some changes that `cargo-features` is unable
75-
//! to sufficiently encompass. An example would be a syntax change in
76-
//! `Cargo.toml` that also impacts the index or resolver. The resolver doesn't
77-
//! know about `cargo-features`, so it needs a `-Z` flag to enable the
78-
//! experimental functionality.
79-
//!
80-
//! In those cases, you usually should introduce both a `-Z` flag (to enable
81-
//! the changes outside of the manifest) and a `cargo-features` entry (to
82-
//! enable the new syntax in `Cargo.toml`). The `cargo-features` entry ensures
83-
//! that any experimental syntax that gets uploaded to crates.io is clearly
84-
//! intended for nightly-only builds. Otherwise, users accessing those crates
85-
//! may get confusing errors, particularly if the syntax changes during the
86-
//! development cycle, and the user tries to access it with a stable release.
87-
//!
88-
//! ### `-Z` with external files
89-
//!
90-
//! Some files, such as `config.toml` config files, or the `config.json` index
91-
//! file, are used in a global location which can make interaction with stable
92-
//! releases problematic. In general, before the feature is stabilized, stable
93-
//! Cargo should behave roughly similar to how it behaved *before* the
94-
//! unstable feature was introduced. If Cargo would normally have ignored or
95-
//! warned about the introduction of something, then it probably should
96-
//! continue to do so.
97-
//!
98-
//! For example, Cargo generally ignores (or warns) about `config.toml`
99-
//! entries it doesn't know about. This allows a limited degree of
100-
//! forwards-compatibility with future versions of Cargo that add new entries.
101-
//!
102-
//! Whether or not to warn on stable may need to be decided on a case-by-case
103-
//! basis. For example, you may want to avoid generating a warning for options
104-
//! that are not critical to Cargo's operation in order to reduce the
105-
//! annoyance of constant warnings. However, ignoring some options may prevent
106-
//! proper operation, so a warning may be valuable for a user trying to
107-
//! diagnose why it isn't working correctly.
85+
//! If warning when feature is used without the gate, be sure to gracefully degrade (with a
86+
//! warning) when the `Cargo.toml` / `.cargo/config.toml` field usage doesn't match the
87+
//! schema.
88+
//! 4. For any `Cargo.toml` fields, strip them in [`prepare_for_publish`] if the gate isn't set
10889
//!
10990
//! ## Stabilization
11091
//!
@@ -135,6 +116,7 @@
135116
//! [`fail_if_stable_opt`]: CliUnstable::fail_if_stable_opt
136117
//! [`features!`]: macro.features.html
137118
//! [`unstable_cli_options!`]: macro.unstable_cli_options.html
119+
//! [`prepare_for_publish`]: crate::util::toml::prepare_for_publish
138120
139121
use std::collections::BTreeSet;
140122
use std::env;

0 commit comments

Comments
 (0)