Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #9350 (cargo build -Z help is missing options) #9369

Closed
wants to merge 15 commits into from
42 changes: 28 additions & 14 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use cargo::core::features;
use cargo::core::{features, CliUnstable};
use cargo::{self, drop_print, drop_println, CliResult, Config};
use clap::{AppSettings, Arg, ArgMatches};

use super::commands;
use super::list_commands;
use crate::command_prelude::*;
use cargo::core::features::HIDDEN;

pub fn main(config: &mut Config) -> CliResult {
// CAUTION: Be careful with using `config` until it is configured below.
Expand All @@ -30,25 +31,38 @@ pub fn main(config: &mut Config) -> CliResult {
};

if args.value_of("unstable-features") == Some("help") {
let options = CliUnstable::help();
let non_hidden_options: Vec<(String, String)> = options
.iter()
.filter(|(_, help_message)| *help_message != HIDDEN)
.map(|(name, help)| (name.to_string(), help.to_string()))
.collect();
let longest_option = non_hidden_options
.iter()
.map(|(option_name, _)| option_name.len())
.max()
.unwrap_or(0);
let help_lines: Vec<String> = non_hidden_options
.iter()
.map(|(option_name, option_help_message)| {
let option_name_kebab_case = option_name.replace("_", "-");
let padding = " ".repeat(longest_option - option_name.len()); // safe to substract
format!(
" -Z {}{} -- {}",
option_name_kebab_case, padding, option_help_message
)
})
.collect();
let joined = help_lines.join("\n");
drop_println!(
config,
"
Available unstable (nightly-only) flags:

-Z allow-features -- Allow *only* the listed unstable features
-Z avoid-dev-deps -- Avoid installing dev-dependencies if possible
-Z extra-link-arg -- Allow `cargo:rustc-link-arg` in build scripts
-Z minimal-versions -- Install minimal dependency versions instead of maximum
-Z no-index-update -- Do not update the registry, avoids a network request for benchmarking
-Z unstable-options -- Allow the usage of unstable options
-Z timings -- Display concurrency information
-Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
-Z terminal-width -- Provide a terminal width to rustc for error truncation
-Z namespaced-features -- Allow features with `dep:` prefix
-Z weak-dep-features -- Allow `dep_name?/feature` feature syntax
-Z patch-in-config -- Allow `[patch]` sections in .cargo/config.toml files
{}

Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
joined
);
if !config.nightly_features_allowed {
drop_println!(
Expand Down
107 changes: 67 additions & 40 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ use crate::util::errors::CargoResult;
use crate::util::{indented_lines, iter_join};
use crate::Config;

pub const HIDDEN: &str = "";
pub const SEE_CHANNELS: &str =
"See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information \
about Rust release channels.";
Expand Down Expand Up @@ -538,51 +539,77 @@ impl Features {
}
}

/// A parsed representation of all unstable flags that Cargo accepts.
///
/// Cargo, like `rustc`, accepts a suite of `-Z` flags which are intended for
/// gating unstable functionality to Cargo. These flags are only available on
/// the nightly channel of Cargo.
#[derive(Default, Debug, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct CliUnstable {
macro_rules! cli_options {
(
$(#[$struct_meta:meta])*
$name: ident,
$(
$(#[$meta:meta])?
$visibility: vis $element: ident: $ty: ty = ($help: expr )
),*
) => {
$(#[$struct_meta])*
pub struct $name {
$(
$(#[$meta])?
$visibility $element: $ty
),*
}
impl $name {
pub fn help() -> Vec<(&'static str, &'static str)> {
let fields = vec![$((stringify!($element), $help)),*];
fields
}
}
}
}

cli_options!(
/// A parsed representation of all unstable flags that Cargo accepts.
///
/// Cargo, like `rustc`, accepts a suite of `-Z` flags which are intended for
/// gating unstable functionality to Cargo. These flags are only available on
/// the nightly channel of Cargo.
#[derive(Default, Debug, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
CliUnstable,
// Permanently unstable features:
pub allow_features: Option<BTreeSet<String>>,
pub print_im_a_teapot: bool,
pub allow_features: Option<BTreeSet<String>> = ("Allow *only* the listed unstable features"),
pub print_im_a_teapot: bool= (HIDDEN),

// All other unstable features.
// Please keep this list lexiographically ordered.
pub advanced_env: bool,
pub avoid_dev_deps: bool,
pub binary_dep_depinfo: bool,
pub advanced_env: bool = (HIDDEN),
pub avoid_dev_deps: bool = ("Avoid installing dev-dependencies if possible"),
pub binary_dep_depinfo: bool = ("Track binary dependencies change"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
#[serde(deserialize_with = "deserialize_build_std")]
pub build_std: Option<Vec<String>>,
pub build_std_features: Option<Vec<String>>,
pub config_include: bool,
pub configurable_env: bool,
pub credential_process: bool,
pub doctest_in_workspace: bool,
pub doctest_xcompile: bool,
pub dual_proc_macros: bool,
pub enable_future_incompat_feature: bool,
pub extra_link_arg: bool,
pub features: Option<Vec<String>>,
pub jobserver_per_rustc: bool,
pub minimal_versions: bool,
pub mtime_on_use: bool,
pub multitarget: bool,
pub named_profiles: bool,
pub namespaced_features: bool,
pub no_index_update: bool,
pub panic_abort_tests: bool,
pub patch_in_config: bool,
pub rustdoc_map: bool,
pub separate_nightlies: bool,
pub terminal_width: Option<Option<usize>>,
pub timings: Option<Vec<String>>,
pub unstable_options: bool,
pub weak_dep_features: bool,
}
pub build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
pub build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
pub config_include: bool = ("Enable the `include` key in config files"),
pub configurable_env: bool = ("Enable the [env] section in the .cargo/config.toml file"),
pub credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
pub doctest_in_workspace: bool = ("TODO"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
pub dual_proc_macros: bool = ("TODO"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub enable_future_incompat_feature: bool = ("TODO"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub extra_link_arg: bool = ("Allow `cargo:rustc-link-arg` in build scripts"),
pub features: Option<Vec<String>> = (HIDDEN),
pub jobserver_per_rustc: bool = ("TODO"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub minimal_versions: bool = ("Install minimal dependency versions instead of maximum"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub mtime_on_use: bool = ("Configure Cargo to update the mtime of used files"),
pub multitarget: bool = ("Allow passing multiple `--target` flags to the cargo subcommand selected"),
pub named_profiles: bool = ("Allow defining custom profiles"),
pub namespaced_features: bool = ("Allow features with `dep:` prefix"),
pub no_index_update: bool = ("Do not update the registry, avoids a network request for benchmarking"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub panic_abort_tests: bool = ("Enable nightly support to compile test harness crates with -Cpanic=abort"),
PicoJr marked this conversation as resolved.
Show resolved Hide resolved
pub patch_in_config: bool = ("Allow `[patch]` sections in .cargo/config.toml files"),
pub rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
pub separate_nightlies: bool = (HIDDEN),
pub terminal_width: Option<Option<usize>> = ("Provide a terminal width to rustc for error truncation"),
pub timings: Option<Vec<String>> = ("Display concurrency information"),
pub unstable_options: bool = ("Allow the usage of unstable options"),
pub weak_dep_features: bool = ("Allow `dep_name?/feature` feature syntax")
);

const STABILIZED_COMPILE_PROGRESS: &str = "The progress bar is now always \
enabled when used on an interactive console.\n\
Expand Down
4 changes: 3 additions & 1 deletion tests/testsuite/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ fn z_flags_help() {
// Test that the output of `cargo -Z help` shows a different help screen with
// all the `-Z` flags.
cargo_process("-Z help")
.with_stdout_contains(" -Z unstable-options -- Allow the usage of unstable options")
.with_stdout_contains(
" -Z allow-features[..]-- Allow *only* the listed unstable features",
)
.run();
}

Expand Down