Skip to content

Commit 5ba4ef3

Browse files
committed
Auto merge of #6791 - ehuss:unstable-help, r=Eh2406
Add some help and documentation for unstable flags. This is intended to give a little extra information about unstable flags. - `-Z help` tells you if you can't use it on stable. - `-Z help` includes link to the unstable chapter. - `--out-dir` and `--build-plan` on stable give more information about what channels are and a link to their respective tracking issues. Add links in the man pages, too.
2 parents 038edb6 + 35b843e commit 5ba4ef3

File tree

10 files changed

+128
-34
lines changed

10 files changed

+128
-34
lines changed

src/bin/cargo/cli.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clap;
22

33
use clap::{AppSettings, Arg, ArgMatches};
44

5+
use cargo::core::features;
56
use cargo::{self, CliResult, Config};
67

78
use super::commands;
@@ -37,6 +38,19 @@ Available unstable (nightly-only) flags:
3738
3839
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
3940
);
41+
if !features::nightly_features_allowed() {
42+
println!(
43+
"\nUnstable flags are only available on the nightly channel \
44+
of Cargo, but this is the `{}` channel.\n\
45+
{}",
46+
features::channel(),
47+
features::SEE_CHANNELS
48+
);
49+
}
50+
println!(
51+
"\nSee https://doc.rust-lang.org/nightly/cargo/reference/unstable.html \
52+
for more information about these flags."
53+
);
4054
return Ok(());
4155
}
4256

@@ -236,4 +250,3 @@ See 'cargo help <command>' for more information on a specific command.\n",
236250
)
237251
.subcommands(commands::builtin())
238252
}
239-

src/bin/cargo/commands/build.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ pub fn cli() -> App {
3030
.arg_features()
3131
.arg_target_triple("Build for the target triple")
3232
.arg_target_dir()
33-
.arg(opt("out-dir", "Copy final artifacts to this directory").value_name("PATH"))
33+
.arg(
34+
opt(
35+
"out-dir",
36+
"Copy final artifacts to this directory (unstable)",
37+
)
38+
.value_name("PATH"),
39+
)
3440
.arg_manifest_path()
3541
.arg_message_format()
3642
.arg_build_plan()
@@ -52,11 +58,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5258
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?;
5359

5460
compile_opts.export_dir = args.value_of_path("out-dir", config);
55-
if compile_opts.export_dir.is_some() && !config.cli_unstable().unstable_options {
56-
Err(failure::format_err!(
57-
"`--out-dir` flag is unstable, pass `-Z unstable-options` to enable it"
58-
))?;
59-
};
61+
if compile_opts.export_dir.is_some() {
62+
config
63+
.cli_unstable()
64+
.fail_if_stable_opt("--out-dir", 6790)?;
65+
}
6066
ops::compile(&ws, &compile_opts)?;
6167
Ok(())
6268
}

src/cargo/core/features.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ use serde::{Deserialize, Serialize};
5555

5656
use crate::util::errors::CargoResult;
5757

58+
pub const SEE_CHANNELS: &str =
59+
"See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information \
60+
about Rust release channels.";
61+
5862
/// The edition of the compiler (RFC 2052)
5963
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
6064
pub enum Edition {
@@ -235,9 +239,11 @@ impl Features {
235239
}
236240
Status::Unstable if !nightly_features_allowed() => failure::bail!(
237241
"the cargo feature `{}` requires a nightly version of \
238-
Cargo, but this is the `{}` channel",
242+
Cargo, but this is the `{}` channel\n\
243+
{}",
239244
feature,
240-
channel()
245+
channel(),
246+
SEE_CHANNELS
241247
),
242248
Status::Unstable => {}
243249
}
@@ -327,7 +333,13 @@ pub struct CliUnstable {
327333
impl CliUnstable {
328334
pub fn parse(&mut self, flags: &[String]) -> CargoResult<()> {
329335
if !flags.is_empty() && !nightly_features_allowed() {
330-
failure::bail!("the `-Z` flag is only accepted on the nightly channel of Cargo")
336+
failure::bail!(
337+
"the `-Z` flag is only accepted on the nightly channel of Cargo, \
338+
but this is the `{}` channel\n\
339+
{}",
340+
channel(),
341+
SEE_CHANNELS
342+
);
331343
}
332344
for flag in flags {
333345
self.add(flag)?;
@@ -365,9 +377,43 @@ impl CliUnstable {
365377

366378
Ok(())
367379
}
380+
381+
/// Generates an error if `-Z unstable-options` was not used.
382+
/// Intended to be used when a user passes a command-line flag that
383+
/// requires `-Z unstable-options`.
384+
pub fn fail_if_stable_opt(&self, flag: &str, issue: u32) -> CargoResult<()> {
385+
if !self.unstable_options {
386+
let see = format!(
387+
"See https://github.com/rust-lang/cargo/issues/{} for more \
388+
information about the `{}` flag.",
389+
issue, flag
390+
);
391+
if nightly_features_allowed() {
392+
failure::bail!(
393+
"the `{}` flag is unstable, pass `-Z unstable-options` to enable it\n\
394+
{}",
395+
flag,
396+
see
397+
);
398+
} else {
399+
failure::bail!(
400+
"the `{}` flag is unstable, and only available on the nightly channel \
401+
of Cargo, but this is the `{}` channel\n\
402+
{}\n\
403+
{}",
404+
flag,
405+
channel(),
406+
SEE_CHANNELS,
407+
see
408+
);
409+
}
410+
}
411+
Ok(())
412+
}
368413
}
369414

370-
fn channel() -> String {
415+
/// Returns the current release channel ("stable", "beta", "nightly", "dev").
416+
pub fn channel() -> String {
371417
if let Ok(override_channel) = env::var("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS") {
372418
return override_channel;
373419
}

src/cargo/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::workspace::{Members, Workspace, WorkspaceConfig, WorkspaceRootConf
1717

1818
pub mod compiler;
1919
pub mod dependency;
20-
mod features;
20+
pub mod features;
2121
mod interning;
2222
pub mod manifest;
2323
pub mod package;

src/cargo/util/command_prelude.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ pub trait AppExt: Sized {
141141
}
142142

143143
fn arg_build_plan(self) -> Self {
144-
self._arg(opt("build-plan", "Output the build plan in JSON"))
144+
self._arg(opt(
145+
"build-plan",
146+
"Output the build plan in JSON (unstable)",
147+
))
145148
}
146149

147150
fn arg_new_opts(self) -> Self {
@@ -315,10 +318,10 @@ pub trait ArgMatchesExt {
315318
build_config.message_format = message_format;
316319
build_config.release = self._is_present("release");
317320
build_config.build_plan = self._is_present("build-plan");
318-
if build_config.build_plan && !config.cli_unstable().unstable_options {
319-
Err(failure::format_err!(
320-
"`--build-plan` flag is unstable, pass `-Z unstable-options` to enable it"
321-
))?;
321+
if build_config.build_plan {
322+
config
323+
.cli_unstable()
324+
.fail_if_stable_opt("--build-plan", 5579)?;
322325
};
323326

324327
let opts = CompileOptions {

src/doc/man/cargo-build.adoc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ include::options-target-dir.adoc[]
4444
*--out-dir* _DIRECTORY_::
4545
Copy final artifacts to this directory.
4646
+
47-
This option is unstable and available only on the nightly channel and requires
48-
the `-Z unstable-options` flag to enable.
47+
This option is unstable and available only on the
48+
link:https://doc.rust-lang.org/book/appendix-07-nightly-rust.html[nightly channel]
49+
and requires the `-Z unstable-options` flag to enable.
50+
See https://github.com/rust-lang/cargo/issues/6790 for more information.
4951

5052
=== Display Options
5153

@@ -57,8 +59,10 @@ include::options-message-format.adoc[]
5759
Outputs a series of JSON messages to stdout that indicate the commands to
5860
run the build.
5961
+
60-
This option is unstable and available only on the nightly channel and requires
61-
the `-Z unstable-options` flag to enable.
62+
This option is unstable and available only on the
63+
link:https://doc.rust-lang.org/book/appendix-07-nightly-rust.html[nightly channel]
64+
and requires the `-Z unstable-options` flag to enable.
65+
See https://github.com/rust-lang/cargo/issues/5579 for more information.
6266

6367
=== Manifest Options
6468

src/doc/man/generated/cargo-build.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,10 @@ <h3 id="cargo_build_output_options">Output Options</h3>
185185
<dd>
186186
<p>Copy final artifacts to this directory.</p>
187187
<div class="paragraph">
188-
<p>This option is unstable and available only on the nightly channel and requires
189-
the <code>-Z unstable-options</code> flag to enable.</p>
188+
<p>This option is unstable and available only on the
189+
<a href="https://doc.rust-lang.org/book/appendix-07-nightly-rust.html">nightly channel</a>
190+
and requires the <code>-Z unstable-options</code> flag to enable.
191+
See <a href="https://github.com/rust-lang/cargo/issues/6790" class="bare">https://github.com/rust-lang/cargo/issues/6790</a> for more information.</p>
190192
</div>
191193
</dd>
192194
</dl>
@@ -253,8 +255,10 @@ <h3 id="cargo_build_display_options">Display Options</h3>
253255
<p>Outputs a series of JSON messages to stdout that indicate the commands to
254256
run the build.</p>
255257
<div class="paragraph">
256-
<p>This option is unstable and available only on the nightly channel and requires
257-
the <code>-Z unstable-options</code> flag to enable.</p>
258+
<p>This option is unstable and available only on the
259+
<a href="https://doc.rust-lang.org/book/appendix-07-nightly-rust.html">nightly channel</a>
260+
and requires the <code>-Z unstable-options</code> flag to enable.
261+
See <a href="https://github.com/rust-lang/cargo/issues/5579" class="bare">https://github.com/rust-lang/cargo/issues/5579</a> for more information.</p>
258262
</div>
259263
</dd>
260264
</dl>

src/doc/src/reference/unstable.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ minimum versions that you are actually using. That is, if Cargo.toml says
7777

7878
### out-dir
7979
* Original Issue: [#4875](https://github.com/rust-lang/cargo/issues/4875)
80+
* Tracking Issue: [#6790](https://github.com/rust-lang/cargo/issues/6790)
8081

8182
This feature allows you to specify the directory where artifacts will be
8283
copied to after they are built. Typically artifacts are only written to the
@@ -148,7 +149,7 @@ cargo +nightly build -Z config-profile
148149

149150
### Namespaced features
150151
* Original issue: [#1286](https://github.com/rust-lang/cargo/issues/1286)
151-
* Tracking Issue: [rust-lang/cargo#5565](https://github.com/rust-lang/cargo/issues/5565)
152+
* Tracking Issue: [#5565](https://github.com/rust-lang/cargo/issues/5565)
152153

153154
Currently, it is not possible to have a feature and a dependency with the same
154155
name in the manifest. If you set `namespaced-features` to `true`, the namespaces
@@ -175,7 +176,7 @@ include the dependency as a requirement, as `foo = ["crate:foo"]`.
175176

176177

177178
### Build-plan
178-
* Tracking Issue: [rust-lang/cargo#5579](https://github.com/rust-lang/cargo/issues/5579)
179+
* Tracking Issue: [#5579](https://github.com/rust-lang/cargo/issues/5579)
179180

180181
The `--build-plan` argument for the `build` command will output JSON with
181182
information about which commands would be run without actually executing

src/etc/man/cargo-build.1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
.\" Title: cargo-build
33
.\" Author: [see the "AUTHOR(S)" section]
44
.\" Generator: Asciidoctor 1.5.8
5-
.\" Date: 2018-12-20
5+
.\" Date: 2019-03-28
66
.\" Manual: \ \&
77
.\" Source: \ \&
88
.\" Language: English
99
.\"
10-
.TH "CARGO\-BUILD" "1" "2018-12-20" "\ \&" "\ \&"
10+
.TH "CARGO\-BUILD" "1" "2019-03-28" "\ \&" "\ \&"
1111
.ie \n(.g .ds Aq \(aq
1212
.el .ds Aq '
1313
.ss \n[.ss] 0
@@ -189,8 +189,13 @@ to \fBtarget\fP in the root of the workspace.
189189
.RS 4
190190
Copy final artifacts to this directory.
191191
.sp
192-
This option is unstable and available only on the nightly channel and requires
193-
the \fB\-Z unstable\-options\fP flag to enable.
192+
This option is unstable and available only on the
193+
\c
194+
.URL "https://doc.rust\-lang.org/book/appendix\-07\-nightly\-rust.html" "nightly channel"
195+
and requires the \fB\-Z unstable\-options\fP flag to enable.
196+
See \c
197+
.URL "https://github.com/rust\-lang/cargo/issues/6790" "" " "
198+
for more information.
194199
.RE
195200
.SS "Display Options"
196201
.sp
@@ -292,8 +297,13 @@ The output format for diagnostic messages. Valid values:
292297
Outputs a series of JSON messages to stdout that indicate the commands to
293298
run the build.
294299
.sp
295-
This option is unstable and available only on the nightly channel and requires
296-
the \fB\-Z unstable\-options\fP flag to enable.
300+
This option is unstable and available only on the
301+
\c
302+
.URL "https://doc.rust\-lang.org/book/appendix\-07\-nightly\-rust.html" "nightly channel"
303+
and requires the \fB\-Z unstable\-options\fP flag to enable.
304+
See \c
305+
.URL "https://github.com/rust\-lang/cargo/issues/5579" "" " "
306+
for more information.
297307
.RE
298308
.SS "Manifest Options"
299309
.sp

tests/testsuite/cargo_features.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ error: failed to parse manifest at `[..]`
146146
Caused by:
147147
the cargo feature `test-dummy-unstable` requires a nightly version of Cargo, \
148148
but this is the `stable` channel
149+
See [..]
149150
",
150151
)
151152
.run();
@@ -207,6 +208,7 @@ Caused by:
207208
Caused by:
208209
the cargo feature `test-dummy-unstable` requires a nightly version of Cargo, \
209210
but this is the `stable` channel
211+
See [..]
210212
",
211213
)
212214
.run();
@@ -248,6 +250,7 @@ error: failed to parse manifest at `[..]`
248250
Caused by:
249251
the cargo feature `test-dummy-unstable` requires a nightly version of Cargo, \
250252
but this is the `stable` channel
253+
See [..]
251254
",
252255
)
253256
.run();
@@ -272,7 +275,11 @@ fn z_flags_rejected() {
272275
.build();
273276
p.cargo("build -Zprint-im-a-teapot")
274277
.with_status(101)
275-
.with_stderr("error: the `-Z` flag is only accepted on the nightly channel of Cargo")
278+
.with_stderr(
279+
"error: the `-Z` flag is only accepted on the nightly \
280+
channel of Cargo, but this is the `stable` channel\n\
281+
See [..]",
282+
)
276283
.run();
277284

278285
p.cargo("build -Zarg")

0 commit comments

Comments
 (0)