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

Add option to error when warnings are emitted, or ignore warnings #12875

Closed
wants to merge 2 commits into from

Conversation

arlosi
Copy link
Contributor

@arlosi arlosi commented Oct 24, 2023

What does this PR try to resolve?

Users want a way to deny all warnings, particularly in CI.

  • Adds --warnings error|warn|ignore CLI flag
  • Adds term.warnings config option.

The options mean:

  • error emits an error if any warnings were emitted during the Cargo operation
  • warn is the existing behavior
  • ignore hides the warnings

Open Questions:

  • Should this be unstable initially via -Zunstable-options?
  • Should it only apply to rustc warnings (not Cargo warnings) until cargo has better control over diagnostics system (User control over cargo warnings #12235)?
    • This could be confusing if users want to deny all warnings and set this flag, only to see some warnings still allowed.
  • Where should the config option go?
    • I used term.warnings, since that also where verbose and quiet live. However, it's not really a terminal setting.
    • [build] is another option, but this applies to all warnings, not just build warnings.
  • Since we have --config "term.warnings='error'" do we need the --warnings CLI option?
    • The CLI option is easier to discover and use than --config but adds 1 more option to every command's help.

Fixes #8424

How should we test and review this PR?

Tests are included in the PR. Resolving the open questions is the majority of the review work.

@rustbot
Copy link
Collaborator

rustbot commented Oct 24, 2023

r? @epage

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cli Area: Command-line interface, option parsing, etc. A-configuration Area: cargo config files and env vars A-console-output Area: Terminal output, colors, progress bar, etc. A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 24, 2023
@@ -595,6 +606,11 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
.value_name("WHEN")
.global(true),
)
.arg(
opt("warnings", "Warning behavior")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be unstable initially via -Zunstable-options?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We likely should if we move forward with this.

Comment on lines 274 to +281
pub fn warn<T: fmt::Display>(&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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it only apply to rustc warnings (not Cargo warnings) until cargo has better control over diagnostics system (#12235)?

@@ -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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should the config option go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

term feels wrong.

We have build but this will control other circumstances (maybe even cargo metadata!). However, build might be the first place people look.

Maybe we should have some kind of general category?

@@ -595,6 +606,11 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
.value_name("WHEN")
.global(true),
)
.arg(
opt("warnings", "Warning behavior")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have --config "term.warnings='error'" do we need the --warnings CLI option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards

  • Stabilize the config and add the flag later (take the incremental approach).
    • That also means that we'll mostly have early adopters at first which will reduce the amount annoyance if we say skip cargo warnings and add them later
    • This will help us vet how much the flag is needed compared to the config
  • When we add the flag, maybe only put it on warning-heavy commands (cargo check, cargo build) to help people discover it without flooding every command with it (cargo version --warnings error?)

Comment on lines +185 to +187
let result = exec.exec(config, subcommand_args);
config.shell().error_for_warnings()?;
result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every command comes back to this, like cargo run does an exec_replace

@@ -595,6 +606,11 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
.value_name("WHEN")
.global(true),
)
.arg(
opt("warnings", "Warning behavior")
.value_parser(["error", "warn", "ignore"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use PossibleValuesParser with TypedValueParser::map to make this work with the native type, rather than returning a string, centralizing the processing

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels weird that shell knows of this, seems more like a config thing

Comment on lines +318 to +320
if shell.warnings() != WarningHandling::Ignore {
shell.print_ansi_stderr(diag.as_bytes())?;
shell.err().write_all(b"\n")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed it but how are we turning compiler warnings into errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not turning the warnings into errors. They're still printed as warnings.

After all the warnings are emitted, a single error is emitted if any warnings were present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put another way, how are we tracking compiler warnings to fail the process.

Comment on lines +1269 to +1271
* `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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you did error and ignore rather than deny/forbid and allow?

On one hand, the semantics are slightly different than the levels but on the other hand, they are familiar, predictable terms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed clearer what the behavior would be. If we do deny for "emit an error" and allow for "ignore warnings" what do we call the default "warn" option?

Maybe deny/warn/allow ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forbid might better fit our semantics, but either way that was what I had originally assumed.

@epage epage added the T-cargo Team: Cargo label Oct 24, 2023
@bors
Copy link
Contributor

bors commented Nov 8, 2023

☔ The latest upstream changes (presumably #12889) made this pull request unmergeable. Please resolve the merge conflicts.

@jstarks
Copy link

jstarks commented Jan 16, 2024

Is there any way to help make progress on this? We've been working around the lack of this by programmatically mutating config.toml in our CI, which is really hard to reason about.

We can't use RUSTFLAGS directly because of its longstanding problems (overrides all other sources of flags, doesn't work for host binaries when cross-compiling).

@xxchan
Copy link
Contributor

xxchan commented Jul 16, 2024

@arlosi Hi, do you have time or interest to continue working on this? If not, I'd like to push forward based on your work.

@arlosi arlosi added the I-nominated-to-discuss To be discussed during issue triage on the next Cargo team meeting label Jul 16, 2024
@arlosi
Copy link
Contributor Author

arlosi commented Jul 16, 2024

@xxchan Thanks for your interest here!

Last time we discussed this was when Cargo's Linting system was in development and we wanted to see how that would interact here. I'll bring this up at next week's team meeting to discuss how to move forward.

@epage
Copy link
Contributor

epage commented Jul 16, 2024

@ehuss ehuss removed the I-nominated-to-discuss To be discussed during issue triage on the next Cargo team meeting label Jul 30, 2024
@arlosi
Copy link
Contributor Author

arlosi commented Aug 12, 2024

Implementation has moved to #14388

@arlosi arlosi closed this Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-execution Area: anything dealing with executing the compiler A-cli Area: Command-line interface, option parsing, etc. A-configuration Area: cargo config files and env vars A-console-output Area: Terminal output, colors, progress bar, etc. A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add --deny-warnings functionality for all commands.
7 participants