diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index a21030f0141..0a66a2382e9 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Context as _}; use cargo::core::shell::Shell; use cargo::core::{features, CliUnstable}; +use cargo::util::config::WarningHandling; use cargo::{self, drop_print, drop_println, CargoResult, CliResult, Config}; use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches}; use itertools::Itertools; @@ -181,7 +182,9 @@ Run with 'cargo -Z [FLAG] [COMMAND]'", config_configure(config, &expanded_args, subcommand_args, global_args, &exec)?; super::init_git(config); - exec.exec(config, subcommand_args) + let result = exec.exec(config, subcommand_args); + config.shell().error_for_warnings()?; + result } pub fn get_version_string(is_verbose: bool) -> String { @@ -394,6 +397,13 @@ fn config_configure( let frozen = args.flag("frozen") || global_args.frozen; let locked = args.flag("locked") || global_args.locked; let offline = args.flag("offline") || global_args.offline; + let warnings = match args.get_one::("warnings").map(String::as_str) { + Some("ignore") => Some(WarningHandling::Ignore), + Some("warn") => Some(WarningHandling::Warn), + Some("error") => Some(WarningHandling::Error), + None => None, + _ => unreachable!(), + }; let mut unstable_flags = global_args.unstable_flags; if let Some(values) = args.get_many::("unstable-features") { unstable_flags.extend(values.cloned()); @@ -405,6 +415,7 @@ fn config_configure( config.configure( verbose, quiet, + warnings, color, frozen, locked, @@ -595,6 +606,11 @@ See 'cargo help <>' for more information on a sp .value_name("WHEN") .global(true), ) + .arg( + opt("warnings", "Warning behavior") + .value_parser(["error", "warn", "ignore"]) + .global(true), + ) .arg( Arg::new("directory") .help("Change to DIRECTORY before doing anything (nightly-only)") diff --git a/src/cargo/core/compiler/job_queue/mod.rs b/src/cargo/core/compiler/job_queue/mod.rs index e39fe184dfa..f4050e1ef32 100644 --- a/src/cargo/core/compiler/job_queue/mod.rs +++ b/src/cargo/core/compiler/job_queue/mod.rs @@ -140,6 +140,7 @@ use crate::core::compiler::future_incompat::{ }; use crate::core::resolver::ResolveBehavior; use crate::core::{PackageId, Shell, TargetKind}; +use crate::util::config::WarningHandling; use crate::util::diagnostic_server::{self, DiagnosticPrinter}; use crate::util::errors::AlreadyPrintedError; use crate::util::machine_message::{self, Message as _}; @@ -314,8 +315,10 @@ impl<'cfg> DiagDedupe<'cfg> { return Ok(false); } let mut shell = self.config.shell(); - shell.print_ansi_stderr(diag.as_bytes())?; - shell.err().write_all(b"\n")?; + if shell.warnings() != WarningHandling::Ignore { + shell.print_ansi_stderr(diag.as_bytes())?; + shell.err().write_all(b"\n")?; + } Ok(true) } } diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index a9b9e84afcd..1fc9dd966ec 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -5,6 +5,7 @@ use std::io::IsTerminal; use anstream::AutoStream; use anstyle::Style; +use crate::util::config::WarningHandling; use crate::util::errors::CargoResult; use crate::util::style::*; @@ -57,6 +58,10 @@ pub struct Shell { /// Flag that indicates the current line needs to be cleared before /// printing. Used when a progress bar is currently displayed. needs_clear: bool, + /// How warning should be handled. + warnings: WarningHandling, + /// The number of warnings so far. + warning_count: usize, } impl fmt::Debug for Shell { @@ -115,6 +120,8 @@ impl Shell { }, verbosity: Verbosity::Verbose, needs_clear: false, + warnings: WarningHandling::Warn, + warning_count: 0, } } @@ -124,6 +131,8 @@ impl Shell { output: ShellOut::Write(AutoStream::never(out)), // strip all formatting on write verbosity: Verbosity::Verbose, needs_clear: false, + warnings: WarningHandling::Warn, + warning_count: 0, } } @@ -263,10 +272,13 @@ impl Shell { /// Prints an amber 'warning' message. pub fn warn(&mut self, message: T) -> CargoResult<()> { - match self.verbosity { - Verbosity::Quiet => Ok(()), - _ => self.print(&"warning", Some(&message), &WARN, false), + self.warning_count += 1; + if matches!(self.verbosity, Verbosity::Quiet) + || matches!(self.warnings, WarningHandling::Ignore) + { + return Ok(()); } + self.print(&"warning", Some(&message), &WARN, false) } /// Prints a cyan 'note' message. @@ -284,6 +296,31 @@ impl Shell { self.verbosity } + /// Updates how warnings are handled. + pub fn set_warnings(&mut self, warnings: WarningHandling) { + self.warnings = warnings; + } + + /// Gets the warning handling behavior. + pub fn warnings(&self) -> WarningHandling { + self.warnings + } + + /// Emit an error if any warnings have been seen. + pub fn error_for_warnings(&self) -> CargoResult<()> { + if self.warning_count > 0 && self.warnings == WarningHandling::Error { + let quiet_note = if self.verbosity == Verbosity::Quiet { + " (note that verbosity is set to quiet, which may hide warnings)" + } else { + "" + }; + anyhow::bail!( + "warnings detected and warnings are disallowed by configuration{quiet_note}" + ); + } + Ok(()) + } + /// Updates the color choice (always, never, or auto) from a string.. pub fn set_color_choice(&mut self, color: Option<&str>) -> CargoResult<()> { if let ShellOut::Stream { diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 039cf7cd04f..bc31878c2b6 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -158,6 +158,16 @@ pub struct CredentialCacheValue { pub operation_independent: bool, } +/// Whether warnings should warn, be ignored, or cause an error. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Default)] +#[serde(rename_all = "kebab-case")] +pub enum WarningHandling { + #[default] + Warn, + Ignore, + Error, +} + /// Configuration information for cargo. This is not specific to a build, it is information /// relating to cargo itself. #[derive(Debug)] @@ -971,6 +981,7 @@ impl Config { &mut self, verbose: u32, quiet: bool, + warnings: Option, color: Option<&str>, frozen: bool, locked: bool, @@ -1032,6 +1043,8 @@ impl Config { self.shell().set_verbosity(verbosity); self.shell().set_color_choice(color)?; + self.shell() + .set_warnings(warnings.or_else(|| term.warnings).unwrap_or_default()); self.progress_config = term.progress.unwrap_or_default(); self.extra_verbose = extra_verbose; self.frozen = frozen; @@ -2563,6 +2576,7 @@ struct TermConfig { #[serde(default)] #[serde(deserialize_with = "progress_or_string")] progress: Option, + warnings: Option, } #[derive(Debug, Default, Deserialize)] diff --git a/src/doc/man/includes/options-display.md b/src/doc/man/includes/options-display.md index 917dac49cc0..e775c508a89 100644 --- a/src/doc/man/includes/options-display.md +++ b/src/doc/man/includes/options-display.md @@ -22,3 +22,14 @@ Control when colored output is used. Valid values: May also be specified with the `term.color` [config value](../reference/config.html). {{/option}} + +{{#option "`--warnings`"}} +Overrides how warnings are handled. Valid values: + +* `warn` (default): warnings are displayed and do not fail the operation. +* `error`: if any warnings are encountered an error will be emitted at the of the operation. +* `ignore`: warnings will be silently ignored. + +May also be specified with the `term.warnings` +[config value](../reference/config.html). +{{/option}} diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md index 9ef42f68f4d..5414df917e2 100644 --- a/src/doc/src/reference/config.md +++ b/src/doc/src/reference/config.md @@ -1257,6 +1257,19 @@ Controls whether or not extra detailed messages are displayed by Cargo. Specifying the `--quiet` flag will override and disable verbose output. Specifying the `--verbose` flag will override and force verbose output. +#### `term.warnings` +* Type: string +* Default: "warn" +* Environment: `CARGO_TERM_WARNINGS` + +Overrides how warnings are handled, including warnings that come from `rustc`. + +The `--warnings=` CLI option overrides this configuration value. + +* `warn` (default): warnings are displayed and do not fail the operation. +* `error`: if any warnings are encountered an error will be emitted at the of the operation. +* `ignore`: warnings will be silently ignored. + #### `term.color` * Type: string * Default: "auto" diff --git a/tests/testsuite/cargo/help/stdout.log b/tests/testsuite/cargo/help/stdout.log index e15848ab784..8de241c08fc 100644 --- a/tests/testsuite/cargo/help/stdout.log +++ b/tests/testsuite/cargo/help/stdout.log @@ -4,19 +4,20 @@ Usage: cargo [..][OPTIONS] [COMMAND] cargo [..][OPTIONS] -Zscript [ARGS]... Options: - -V, --version Print version info and exit - --list List installed commands - --explain Provide a detailed explanation of a rustc error message - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - -q, --quiet Do not print cargo log messages - --color Coloring: auto, always, never - -C Change to DIRECTORY before doing anything (nightly-only) - --frozen Require Cargo.lock and cache are up to date - --locked Require Cargo.lock is up to date - --offline Run without accessing the network - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -V, --version Print version info and exit + --list List installed commands + --explain Provide a detailed explanation of a rustc error message + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + -q, --quiet Do not print cargo log messages + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + -C Change to DIRECTORY before doing anything (nightly-only) + --frozen Require Cargo.lock and cache are up to date + --locked Require Cargo.lock is up to date + --offline Run without accessing the network + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Commands: build, b Compile the current package diff --git a/tests/testsuite/cargo_add/help/stdout.log b/tests/testsuite/cargo_add/help/stdout.log index cf2a913132d..4384f2bdb87 100644 --- a/tests/testsuite/cargo_add/help/stdout.log +++ b/tests/testsuite/cargo_add/help/stdout.log @@ -54,6 +54,11 @@ Options: --color Coloring: auto, always, never + --warnings + Warning behavior + + [possible values: error, warn, ignore] + --config Override a configuration value diff --git a/tests/testsuite/cargo_bench/help/stdout.log b/tests/testsuite/cargo_bench/help/stdout.log index a310f73aa28..df76a131b81 100644 --- a/tests/testsuite/cargo_bench/help/stdout.log +++ b/tests/testsuite/cargo_bench/help/stdout.log @@ -14,6 +14,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_build/help/stdout.log b/tests/testsuite/cargo_build/help/stdout.log index 56b934cd168..c6f56a60add 100644 --- a/tests/testsuite/cargo_build/help/stdout.log +++ b/tests/testsuite/cargo_build/help/stdout.log @@ -9,6 +9,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_check/help/stdout.log b/tests/testsuite/cargo_check/help/stdout.log index 92d44a6ded2..615f2698c5d 100644 --- a/tests/testsuite/cargo_check/help/stdout.log +++ b/tests/testsuite/cargo_check/help/stdout.log @@ -9,6 +9,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_clean/help/stdout.log b/tests/testsuite/cargo_clean/help/stdout.log index 6e9e82772f1..665872fbaa7 100644 --- a/tests/testsuite/cargo_clean/help/stdout.log +++ b/tests/testsuite/cargo_clean/help/stdout.log @@ -3,14 +3,15 @@ Remove artifacts that cargo has generated in the past Usage: cargo[EXE] clean [OPTIONS] Options: - --doc Whether or not to clean just the documentation directory - -q, --quiet Do not print cargo log messages - -n, --dry-run Display what would be deleted without deleting anything - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + --doc Whether or not to clean just the documentation directory + -q, --quiet Do not print cargo log messages + -n, --dry-run Display what would be deleted without deleting anything + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -p, --package [] Package to clean artifacts for diff --git a/tests/testsuite/cargo_config/help/stdout.log b/tests/testsuite/cargo_config/help/stdout.log index 50caca72a4c..5bb9d189329 100644 --- a/tests/testsuite/cargo_config/help/stdout.log +++ b/tests/testsuite/cargo_config/help/stdout.log @@ -6,11 +6,12 @@ Commands: get Options: - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --frozen Require Cargo.lock and cache are up to date diff --git a/tests/testsuite/cargo_doc/help/stdout.log b/tests/testsuite/cargo_doc/help/stdout.log index 8ff5f9b72a7..ed635e57f7c 100644 --- a/tests/testsuite/cargo_doc/help/stdout.log +++ b/tests/testsuite/cargo_doc/help/stdout.log @@ -11,6 +11,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_fetch/help/stdout.log b/tests/testsuite/cargo_fetch/help/stdout.log index 32f29f1b39a..76685295585 100644 --- a/tests/testsuite/cargo_fetch/help/stdout.log +++ b/tests/testsuite/cargo_fetch/help/stdout.log @@ -3,12 +3,13 @@ Fetch dependencies of a package from the network Usage: cargo[EXE] fetch [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Compilation Options: --target [] Fetch dependencies for the target triple diff --git a/tests/testsuite/cargo_fix/help/stdout.log b/tests/testsuite/cargo_fix/help/stdout.log index dbbd11b7752..efe739d5b4a 100644 --- a/tests/testsuite/cargo_fix/help/stdout.log +++ b/tests/testsuite/cargo_fix/help/stdout.log @@ -14,6 +14,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_generate_lockfile/help/stdout.log b/tests/testsuite/cargo_generate_lockfile/help/stdout.log index 07eff888a20..2acf36ef9bc 100644 --- a/tests/testsuite/cargo_generate_lockfile/help/stdout.log +++ b/tests/testsuite/cargo_generate_lockfile/help/stdout.log @@ -3,12 +3,13 @@ Generate the lockfile for a package Usage: cargo[EXE] generate-lockfile [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --manifest-path Path to Cargo.toml diff --git a/tests/testsuite/cargo_help/help/stdout.log b/tests/testsuite/cargo_help/help/stdout.log index a03946b45ff..0fe54c2cfd7 100644 --- a/tests/testsuite/cargo_help/help/stdout.log +++ b/tests/testsuite/cargo_help/help/stdout.log @@ -6,11 +6,12 @@ Arguments: [COMMAND] Options: - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --frozen Require Cargo.lock and cache are up to date diff --git a/tests/testsuite/cargo_init/help/stdout.log b/tests/testsuite/cargo_init/help/stdout.log index 588b45ccf4a..b3782a54292 100644 --- a/tests/testsuite/cargo_init/help/stdout.log +++ b/tests/testsuite/cargo_init/help/stdout.log @@ -18,6 +18,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_install/help/stdout.log b/tests/testsuite/cargo_install/help/stdout.log index 5e3458d37a8..c82156acfe5 100644 --- a/tests/testsuite/cargo_install/help/stdout.log +++ b/tests/testsuite/cargo_install/help/stdout.log @@ -24,6 +24,7 @@ Options: --debug Build in debug mode (with the 'dev' profile) instead of release mode -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_locate_project/help/stdout.log b/tests/testsuite/cargo_locate_project/help/stdout.log index 1c6ea7b2561..6b349ea3702 100644 --- a/tests/testsuite/cargo_locate_project/help/stdout.log +++ b/tests/testsuite/cargo_locate_project/help/stdout.log @@ -8,6 +8,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_login/help/stdout.log b/tests/testsuite/cargo_login/help/stdout.log index e0d5e7e69ce..de5e518e610 100644 --- a/tests/testsuite/cargo_login/help/stdout.log +++ b/tests/testsuite/cargo_login/help/stdout.log @@ -11,6 +11,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_logout/help/stdout.log b/tests/testsuite/cargo_logout/help/stdout.log index fe328d765a9..117f256c34b 100644 --- a/tests/testsuite/cargo_logout/help/stdout.log +++ b/tests/testsuite/cargo_logout/help/stdout.log @@ -7,6 +7,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_metadata/help/stdout.log b/tests/testsuite/cargo_metadata/help/stdout.log index 939fc40c9c0..3ed8710b740 100644 --- a/tests/testsuite/cargo_metadata/help/stdout.log +++ b/tests/testsuite/cargo_metadata/help/stdout.log @@ -11,6 +11,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_new/help/stdout.log b/tests/testsuite/cargo_new/help/stdout.log index 3df5eceb87e..f55498b22a1 100644 --- a/tests/testsuite/cargo_new/help/stdout.log +++ b/tests/testsuite/cargo_new/help/stdout.log @@ -18,6 +18,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_owner/help/stdout.log b/tests/testsuite/cargo_owner/help/stdout.log index 110df8e9afb..543f1670f35 100644 --- a/tests/testsuite/cargo_owner/help/stdout.log +++ b/tests/testsuite/cargo_owner/help/stdout.log @@ -15,6 +15,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_package/help/stdout.log b/tests/testsuite/cargo_package/help/stdout.log index 5079c2a6fa3..7b2b2124fc3 100644 --- a/tests/testsuite/cargo_package/help/stdout.log +++ b/tests/testsuite/cargo_package/help/stdout.log @@ -3,16 +3,17 @@ Assemble the local package into a distributable tarball Usage: cargo[EXE] package [OPTIONS] Options: - -l, --list Print files included in a package without making one - --no-verify Don't verify the contents by building them - --no-metadata Ignore warnings about a lack of human-usable metadata - --allow-dirty Allow dirty working directories to be packaged - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -l, --list Print files included in a package without making one + --no-verify Don't verify the contents by building them + --no-metadata Ignore warnings about a lack of human-usable metadata + --allow-dirty Allow dirty working directories to be packaged + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -p, --package [] Package(s) to assemble diff --git a/tests/testsuite/cargo_pkgid/help/stdout.log b/tests/testsuite/cargo_pkgid/help/stdout.log index 5971e88dc6f..229e74553d0 100644 --- a/tests/testsuite/cargo_pkgid/help/stdout.log +++ b/tests/testsuite/cargo_pkgid/help/stdout.log @@ -6,12 +6,13 @@ Arguments: [SPEC] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -p, --package [] Argument to get the package ID specifier for diff --git a/tests/testsuite/cargo_publish/help/stdout.log b/tests/testsuite/cargo_publish/help/stdout.log index df2594fb462..14ed774e91e 100644 --- a/tests/testsuite/cargo_publish/help/stdout.log +++ b/tests/testsuite/cargo_publish/help/stdout.log @@ -12,6 +12,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_read_manifest/help/stdout.log b/tests/testsuite/cargo_read_manifest/help/stdout.log index 83db5413d92..f5d3cabc426 100644 --- a/tests/testsuite/cargo_read_manifest/help/stdout.log +++ b/tests/testsuite/cargo_read_manifest/help/stdout.log @@ -5,12 +5,13 @@ Deprecated, use `cargo metadata --no-deps` instead. Usage: cargo[EXE] read-manifest [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --manifest-path Path to Cargo.toml diff --git a/tests/testsuite/cargo_remove/help/stdout.log b/tests/testsuite/cargo_remove/help/stdout.log index 47d2c87adae..72a9c11ec07 100644 --- a/tests/testsuite/cargo_remove/help/stdout.log +++ b/tests/testsuite/cargo_remove/help/stdout.log @@ -6,13 +6,14 @@ Arguments: ... Dependencies to be removed Options: - -n, --dry-run Don't actually write the manifest - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -n, --dry-run Don't actually write the manifest + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Section: --dev Remove from dev-dependencies diff --git a/tests/testsuite/cargo_report/help/stdout.log b/tests/testsuite/cargo_report/help/stdout.log index 67819de55c4..89981a58436 100644 --- a/tests/testsuite/cargo_report/help/stdout.log +++ b/tests/testsuite/cargo_report/help/stdout.log @@ -6,11 +6,12 @@ Commands: future-incompatibilities Reports any crates which will eventually stop compiling Options: - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --frozen Require Cargo.lock and cache are up to date diff --git a/tests/testsuite/cargo_run/help/stdout.log b/tests/testsuite/cargo_run/help/stdout.log index 97c13382a67..2848758a488 100644 --- a/tests/testsuite/cargo_run/help/stdout.log +++ b/tests/testsuite/cargo_run/help/stdout.log @@ -11,6 +11,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_rustc/help/stdout.log b/tests/testsuite/cargo_rustc/help/stdout.log index 8d184dc0629..03f0789377e 100644 --- a/tests/testsuite/cargo_rustc/help/stdout.log +++ b/tests/testsuite/cargo_rustc/help/stdout.log @@ -14,6 +14,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_rustdoc/help/stdout.log b/tests/testsuite/cargo_rustdoc/help/stdout.log index 8d4415cbb63..6eba3384282 100644 --- a/tests/testsuite/cargo_rustdoc/help/stdout.log +++ b/tests/testsuite/cargo_rustdoc/help/stdout.log @@ -12,6 +12,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_search/help/stdout.log b/tests/testsuite/cargo_search/help/stdout.log index 9cc508bba9c..baf1828399e 100644 --- a/tests/testsuite/cargo_search/help/stdout.log +++ b/tests/testsuite/cargo_search/help/stdout.log @@ -12,6 +12,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/cargo_test/help/stdout.log b/tests/testsuite/cargo_test/help/stdout.log index d69fcb0edb5..6cf80350223 100644 --- a/tests/testsuite/cargo_test/help/stdout.log +++ b/tests/testsuite/cargo_test/help/stdout.log @@ -16,6 +16,7 @@ Options: -q, --quiet Display one character per test instead of one line -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_tree/help/stdout.log b/tests/testsuite/cargo_tree/help/stdout.log index 4170583a854..9d6a9dca18b 100644 --- a/tests/testsuite/cargo_tree/help/stdout.log +++ b/tests/testsuite/cargo_tree/help/stdout.log @@ -3,24 +3,25 @@ Display a tree visualization of a dependency graph Usage: cargo[EXE] tree [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -e, --edges The kinds of dependencies to display (features, normal, build, dev, all, - no-normal, no-build, no-dev, no-proc-macro) - -i, --invert [] Invert the tree direction and focus on the given package - --prune Prune the given package from the display of the dependency tree - --depth Maximum display depth of the dependency tree - --prefix Change the prefix (indentation) of how each entry is displayed [default: - indent] [possible values: depth, indent, none] - --no-dedupe Do not de-duplicate (repeats all shared dependencies) - -d, --duplicates Show only dependencies which come in multiple versions (implies -i) - --charset Character set to use in output [default: utf8] [possible values: utf8, - ascii] - -f, --format Format string used for printing dependencies [default: {p}] - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -e, --edges The kinds of dependencies to display (features, normal, build, dev, + all, no-normal, no-build, no-dev, no-proc-macro) + -i, --invert [] Invert the tree direction and focus on the given package + --prune Prune the given package from the display of the dependency tree + --depth Maximum display depth of the dependency tree + --prefix Change the prefix (indentation) of how each entry is displayed + [default: indent] [possible values: depth, indent, none] + --no-dedupe Do not de-duplicate (repeats all shared dependencies) + -d, --duplicates Show only dependencies which come in multiple versions (implies -i) + --charset Character set to use in output [default: utf8] [possible values: utf8, + ascii] + -f, --format Format string used for printing dependencies [default: {p}] + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -p, --package [] Package to be used as the root of the tree diff --git a/tests/testsuite/cargo_uninstall/help/stdout.log b/tests/testsuite/cargo_uninstall/help/stdout.log index efdf11c039a..e570eb49b73 100644 --- a/tests/testsuite/cargo_uninstall/help/stdout.log +++ b/tests/testsuite/cargo_uninstall/help/stdout.log @@ -6,13 +6,14 @@ Arguments: [SPEC]... Options: - --root Directory to uninstall packages from - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + --root Directory to uninstall packages from + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -p, --package [] Package to uninstall diff --git a/tests/testsuite/cargo_update/help/stdout.log b/tests/testsuite/cargo_update/help/stdout.log index 92caeb65601..5eac83fc671 100644 --- a/tests/testsuite/cargo_update/help/stdout.log +++ b/tests/testsuite/cargo_update/help/stdout.log @@ -3,15 +3,16 @@ Update dependencies as recorded in the local lock file Usage: cargo[EXE] update [OPTIONS] [SPEC]... Options: - -n, --dry-run Don't actually write the lockfile - --recursive Force updating all dependencies of [SPEC]... as well - --precise Update [SPEC] to exactly PRECISE - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -n, --dry-run Don't actually write the lockfile + --recursive Force updating all dependencies of [SPEC]... as well + --precise Update [SPEC] to exactly PRECISE + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Package Selection: -w, --workspace Only update the workspace packages diff --git a/tests/testsuite/cargo_vendor/help/stdout.log b/tests/testsuite/cargo_vendor/help/stdout.log index 7f37ab56edd..36a2fdf5b9c 100644 --- a/tests/testsuite/cargo_vendor/help/stdout.log +++ b/tests/testsuite/cargo_vendor/help/stdout.log @@ -13,6 +13,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details diff --git a/tests/testsuite/cargo_verify_project/help/stdout.log b/tests/testsuite/cargo_verify_project/help/stdout.log index a61534500d5..c61c042d4c3 100644 --- a/tests/testsuite/cargo_verify_project/help/stdout.log +++ b/tests/testsuite/cargo_verify_project/help/stdout.log @@ -3,12 +3,13 @@ Check correctness of crate manifest Usage: cargo[EXE] verify-project [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --manifest-path Path to Cargo.toml diff --git a/tests/testsuite/cargo_version/help/stdout.log b/tests/testsuite/cargo_version/help/stdout.log index 3f79051ad40..3a3d0369ea3 100644 --- a/tests/testsuite/cargo_version/help/stdout.log +++ b/tests/testsuite/cargo_version/help/stdout.log @@ -3,12 +3,13 @@ Show version information Usage: cargo[EXE] version [OPTIONS] Options: - -q, --quiet Do not print cargo log messages - -v, --verbose... Use verbose output (-vv very verbose/build.rs output) - --color Coloring: auto, always, never - --config Override a configuration value - -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details - -h, --help Print help + -q, --quiet Do not print cargo log messages + -v, --verbose... Use verbose output (-vv very verbose/build.rs output) + --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] + --config Override a configuration value + -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details + -h, --help Print help Manifest Options: --frozen Require Cargo.lock and cache are up to date diff --git a/tests/testsuite/cargo_yank/help/stdout.log b/tests/testsuite/cargo_yank/help/stdout.log index 61dc800c7e0..27a2b5c99ec 100644 --- a/tests/testsuite/cargo_yank/help/stdout.log +++ b/tests/testsuite/cargo_yank/help/stdout.log @@ -14,6 +14,7 @@ Options: -q, --quiet Do not print cargo log messages -v, --verbose... Use verbose output (-vv very verbose/build.rs output) --color Coloring: auto, always, never + --warnings Warning behavior [possible values: error, warn, ignore] --config Override a configuration value -Z Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 9e77048c3f3..f3901727ca7 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -107,6 +107,7 @@ impl ConfigBuilder { 0, false, None, + None, false, false, false, diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index d31255d0ae7..9a5a90c8183 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -175,6 +175,7 @@ mod vendor; mod verify_project; mod version; mod warn_on_failure; +mod warning_override; mod weak_dep_features; mod workspaces; mod yank; diff --git a/tests/testsuite/warning_override.rs b/tests/testsuite/warning_override.rs new file mode 100644 index 00000000000..982240e8741 --- /dev/null +++ b/tests/testsuite/warning_override.rs @@ -0,0 +1,85 @@ +//! Tests for overriding warning behavior using `--warnings=` on the CLI or `term.warnings` config option. + +use cargo_test_support::{project, Project}; + +const WARNING1: &'static str = "[WARNING] unused variable: `x`"; +const WARNING2: &'static str = "[WARNING] unused config key `build.xyz` in `[..]`"; + +fn make_project(main_src: &str) -> Project { + project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#, + ) + .file("src/main.rs", &format!("fn main() {{ {} }}", main_src)) + .build() +} + +#[cargo_test] +fn rustc_warnings() { + let p = make_project("let x = 3;"); + p.cargo("check --warnings=warn") + .with_stderr_contains(WARNING1) + .run(); + + p.cargo("check --warnings=error") + .with_stderr_contains(WARNING1) + .with_stderr_contains( + "[ERROR] warnings detected and warnings are disallowed by configuration", + ) + .with_status(101) + .run(); + + p.cargo("check --warnings=ignore") + .with_stderr_does_not_contain(WARNING1) + .run(); +} + +#[cargo_test] +fn config() { + let p = make_project("let x = 3;"); + p.cargo("check") + .env("CARGO_TERM_WARNINGS", "error") + .with_stderr_contains(WARNING1) + .with_stderr_contains( + "[ERROR] warnings detected and warnings are disallowed by configuration", + ) + .with_status(101) + .run(); + + // CLI has precedence over config + p.cargo("check --warnings=ignore") + .env("CARGO_TERM_WARNINGS", "error") + .with_stderr_does_not_contain(WARNING1) + .run(); + + p.cargo("check") + .env("CARGO_TERM_WARNINGS", "ignore") + .with_stderr_does_not_contain(WARNING1) + .run(); +} + +#[cargo_test] +/// Warnings that come from cargo rather than rustc +fn cargo_warnings() { + let p = make_project(""); + p.change_file(".cargo/config.toml", "[build]\nxyz = false"); + p.cargo("check").with_stderr_contains(WARNING2).run(); + + p.cargo("check --warnings=error") + .with_stderr_contains(WARNING2) + .with_stderr_contains( + "[ERROR] warnings detected and warnings are disallowed by configuration", + ) + .with_status(101) + .run(); + + p.cargo("check --warnings=ignore") + .with_stderr_does_not_contain(WARNING2) + .run(); +}