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

Implement warnnotaserror Fixes #3062 #7309

Merged
merged 12 commits into from
Feb 16, 2022
Merged

Conversation

Forgind
Copy link
Member

@Forgind Forgind commented Jan 21, 2022

Fixes #3062

Context

We previously had warnaserror (and a property for it) that, when you specified error codes, upgraded those error codes from warnings to errors. If you just left it empty, it upgraded all warnings to errors. (Null meant don't upgrade.)

This adds that you can ask for all error codes to be upgraded, then downgrade just a few of them (via codes) back to warnings.

Changes Made

Implement WarnNotAsError both as a command line switch and as a property.

Testing

Added a unit test. Tried it out from the command line.

Notes

Should now work for:
TaskHosts
If set via property (and then only for that project)
Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

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

Is there a way to override the command line switch?

I'd like to see tests for the properties and the interaction between properties and the CLI switches (specifically around msbuild -warnaserror + <MSBuildWarningsNotAsErrors>).

@@ -1258,6 +1274,14 @@ Copyright (C) Microsoft Corporation. All rights reserved.
LOCALIZATION: The prefix "MSBUILD : error MSBxxxx:" should not be localized.
</comment>
</data>
<data name="MissingWarnNotAsErrorParameterError" UESanitized="true" Visibility="Public">
<value>MSBUILD : error MSB1060: Specify one or more warning codes to keep as warnings despite a global -warnaserror when using the -warnNotAsError switch.</value>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<value>MSBUILD : error MSB1060: Specify one or more warning codes to keep as warnings despite a global -warnaserror when using the -warnNotAsError switch.</value>
<value>MSBUILD : error MSB1060: Specify one or more warning codes when using the -warnNotAsError switch.</value>

Comment on lines +832 to +833
multiple warning codes. Has no effect if the -warnaserror
switch is not set.
Copy link
Member

Choose a reason for hiding this comment

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

Why not error if -warnaserror is not on?

Copy link
Member Author

Choose a reason for hiding this comment

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

If warnaserror is not set at all, no warnings are promoted to errors anyway, so there's nothing to revert to a warning.

More generally, you can clear the warnaserror switch, so I thought of this as only important if the user asked all warnings to be promoted. That said, I'm not opposed to make it revert error codes even if there are only a few warnings promoted to errors. I'm curious as to whether it's more confusing, if someone is unaware of the WarningsNotAsErrors switch, to have a switch clearly set but not doing anything versus rely on all warnings to be promoted and have one just not be.

Copy link
Member

Choose a reason for hiding this comment

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

I'm proposing emitting a new warning at command-line-parsing time that says " you said warnnotaserror but didn't have any warnaserrors"

Copy link
Member Author

Choose a reason for hiding this comment

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

You could still have a project-level warnaserror that you wanted not promoted, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

NTS: error

@@ -748,7 +768,7 @@ public bool ShouldTreatWarningAsError(string warningCode)
}

// An empty set means all warnings are errors.
return WarningsAsErrors.Count == 0 || WarningsAsErrors.Contains(warningCode);
return (WarningsAsErrors.Count == 0 && (WarningsNotAsErrors == null || !WarningsNotAsErrors.Contains(warningCode))) || WarningsAsErrors.Contains(warningCode);
Copy link
Member

Choose a reason for hiding this comment

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

This is fairly confusing. Extract to a named local function and add comments?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll hold off on this for now until we've nailed down whether -warnaserror:FOR123 -warnnotaserror:FOR123 should have the warning promoted to an error or not. This should clean itself up if we go with it being just a warning.

@@ -263,6 +264,7 @@ bool emptyParametersAllowed
new ParameterizedSwitchInfo( new string[] { "preprocess", "pp" }, ParameterizedSwitch.Preprocess, null, false, null, true, false ),
new ParameterizedSwitchInfo( new string[] { "targets", "ts" }, ParameterizedSwitch.Targets, null, false, null, true, false ),
new ParameterizedSwitchInfo( new string[] { "warnaserror", "err" }, ParameterizedSwitch.WarningsAsErrors, null, true, null, true, true ),
new ParameterizedSwitchInfo( new string[] { "warnnotaserror", "noerr" }, ParameterizedSwitch.WarningsNotAsErrors, null, true, "MissingWarnNotAsErrorParameterError", true, true ),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
new ParameterizedSwitchInfo( new string[] { "warnnotaserror", "noerr" }, ParameterizedSwitch.WarningsNotAsErrors, null, true, "MissingWarnNotAsErrorParameterError", true, true ),
new ParameterizedSwitchInfo( new string[] { "warnnotaserror", "noerr" }, ParameterizedSwitch.WarningsNotAsErrors, null, true, "MissingWarnNotAsErrorParameterError", true, false ),

@@ -163,6 +163,15 @@ ISet<string> WarningsAsErrors
set;
}

/// <summary>
/// Set of warnings to not treat as errors. Only has any effect if WarningsAsErrors is non-null but empty.
Copy link
Member

Choose a reason for hiding this comment

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

Sounds like you're making a decision that it's invalid to build with -warnaserrors:AB1234,CD5678 and then in a project set <MSBuildWarningsNotAsErrors>AB1234</MSBuildWarningsNotAsErrors>. Do you think that's right? I'm not sure myself.

Comment on lines 2507 to 2508
// TODO: Parse an environment variable as well?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// TODO: Parse an environment variable as well?

Comment on lines 2523 to +2525
// An empty /warnaserror is added as "null". In this case, the list is cleared
// so that all warnings are treated errors
warningsAsErrors.Clear();
warningSwitches.Clear();
Copy link
Member

Choose a reason for hiding this comment

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

This behavior wasn't present on warnasmessage before. Should it be now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, but also, I'd vote yes. It could theoretically break someone who has /warnasmessage:FOR123 /warnasmessage:, but if you were doing something bad, I'd say you deserve that. It's very logical to assume that converting warnings to errors/messages should work the same way, so we should do that.

static Microsoft.Build.Globbing.CompositeGlob.Create(System.Collections.Generic.IEnumerable<Microsoft.Build.Globbing.IMSBuildGlob> globs) -> Microsoft.Build.Globbing.IMSBuildGlob
Copy link
Member

Choose a reason for hiding this comment

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

Hm. Starting to wonder if the public API promotion should be in main rather than the release branch, since it will be an annoying merge here. But we also just checked in a revert that changed public API after forking so maybe not?

@@ -220,6 +220,11 @@ internal partial class LoggingService : ILoggingService, INodePacketHandler, IBu
/// </summary>
private IDictionary<int, ISet<string>> _warningsAsErrorsByProject;

/// <summary>
/// A list of warnings to treat as errors for an associated <see cref="BuildEventContext"/>. If an empty set, all warnings are treated as errors.
Copy link
Member

Choose a reason for hiding this comment

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

cough

return GetWarningsForProject(context, _warningsAsMessagesByProject, WarningsAsMessages);
}

private ICollection<string> GetWarningsForProject(BuildEventContext context, IDictionary<int, ISet<string>> warningsByProject, ISet<string> warnings)
Copy link
Member

Choose a reason for hiding this comment

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

Comments please

Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

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

LGTM with those added comments.

I really don't know how this is even vaguely possible—but it looks like main is wrong?
@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Feb 7, 2022
@Forgind Forgind merged commit 7d926d7 into dotnet:main Feb 16, 2022
@Forgind Forgind deleted the warn-not-as-error branch February 16, 2022 01:45
@baronfel baronfel added this to the VS 17.2 milestone Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option for "warning as errors except ..."
3 participants