Skip to content

Commit

Permalink
Implement warnnotaserror Fixes #3062 (#7309)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Forgind authored Feb 16, 2022
1 parent 2d98a90 commit 7d926d7
Show file tree
Hide file tree
Showing 33 changed files with 892 additions and 118 deletions.
19 changes: 19 additions & 0 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ public ISet<string> WarningsAsErrors
set;
}

/// <summary>
/// List of warnings to not treat as errors.
/// </summary>
public ISet<string> WarningsNotAsErrors
{
get;
set;
}

/// <summary>
/// List of warnings to treat as low importance messages.
/// </summary>
Expand Down Expand Up @@ -240,6 +249,11 @@ public void AddWarningsAsErrors(BuildEventContext buildEventContext, ISet<string
throw new NotImplementedException();
}

public void AddWarningsNotAsErrors(BuildEventContext buildEventContext, ISet<string> codes)
{
throw new NotImplementedException();
}

/// <summary>
/// Registers a distributed logger.
/// </summary>
Expand Down Expand Up @@ -609,6 +623,11 @@ public ICollection<string> GetWarningsAsErrors(BuildEventContext context)
throw new NotImplementedException();
}

public ICollection<string> GetWarningsNotAsErrors(BuildEventContext context)
{
throw new NotImplementedException();
}

public ICollection<string> GetWarningsAsMessages(BuildEventContext context)
{
throw new NotImplementedException();
Expand Down
16 changes: 16 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskHostConfiguration_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void ConstructorWithNullName()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);
}
);
Expand Down Expand Up @@ -96,6 +97,7 @@ public void ConstructorWithEmptyName()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);
}
);
Expand Down Expand Up @@ -131,6 +133,7 @@ public void ConstructorWithNullLocation()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);
}
);
Expand Down Expand Up @@ -168,6 +171,7 @@ public void ConstructorWithEmptyLocation()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);
}
);
Expand Down Expand Up @@ -203,6 +207,7 @@ public void TestValidConstructors()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

TaskHostConfiguration config2 = new TaskHostConfiguration(
Expand All @@ -228,6 +233,7 @@ public void TestValidConstructors()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

IDictionary<string, object> parameters = new Dictionary<string, object>();
Expand All @@ -254,6 +260,7 @@ public void TestValidConstructors()
taskParameters: parameters,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

IDictionary<string, object> parameters2 = new Dictionary<string, object>();
Expand Down Expand Up @@ -285,6 +292,7 @@ public void TestValidConstructors()
taskParameters: parameters2,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

HashSet<string> WarningsAsErrors = new HashSet<string>();
Expand Down Expand Up @@ -316,6 +324,7 @@ public void TestValidConstructors()
taskParameters: parameters2,
globalParameters: null,
warningsAsErrors: WarningsAsErrors,
warningsNotAsErrors: null,
warningsAsMessages: null);
}

Expand Down Expand Up @@ -354,6 +363,7 @@ public void TestTranslationWithNullDictionary()
taskParameters: null,
globalParameters: expectedGlobalProperties,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -399,6 +409,7 @@ public void TestTranslationWithEmptyDictionary()
taskParameters: new Dictionary<string, object>(),
globalParameters: new Dictionary<string, string>(),
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -449,6 +460,7 @@ public void TestTranslationWithValueTypesInDictionary()
taskParameters: parameters,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -497,6 +509,7 @@ public void TestTranslationWithITaskItemInDictionary()
taskParameters: parameters,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -544,6 +557,7 @@ public void TestTranslationWithITaskItemArrayInDictionary()
taskParameters: parameters,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -598,6 +612,7 @@ public void TestTranslationWithWarningsAsErrors()
taskParameters: null,
globalParameters: null,
warningsAsErrors: WarningsAsErrors,
warningsNotAsErrors: null,
warningsAsMessages: null);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down Expand Up @@ -647,6 +662,7 @@ public void TestTranslationWithWarningsAsMessages()
taskParameters: null,
globalParameters: null,
warningsAsErrors: null,
warningsNotAsErrors: null,
warningsAsMessages: WarningsAsMessages);

((ITranslatable)config).Translate(TranslationHelpers.GetWriteTranslator());
Expand Down
4 changes: 3 additions & 1 deletion src/Build/BackEnd/BuildManager/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ ILoggingService InitializeLoggingService()
AppendDebuggingLoggers(_buildParameters.Loggers),
_buildParameters.ForwardingLoggers,
_buildParameters.WarningsAsErrors,
_buildParameters.WarningsNotAsErrors,
_buildParameters.WarningsAsMessages);

_nodeManager.RegisterPacketHandler(NodePacketType.LogMessage, LogMessagePacket.FactoryForDeserialization, loggingService as INodePacketHandler);
Expand Down Expand Up @@ -2938,7 +2939,7 @@ void OnProjectStartedBody(ProjectStartedEventArgs e)
/// <summary>
/// Creates a logging service around the specified set of loggers.
/// </summary>
private ILoggingService CreateLoggingService(IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> forwardingLoggers, ISet<string> warningsAsErrors, ISet<string> warningsAsMessages)
private ILoggingService CreateLoggingService(IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> forwardingLoggers, ISet<string> warningsAsErrors, ISet<string> warningsNotAsErrors, ISet<string> warningsAsMessages)
{
Debug.Assert(Monitor.IsEntered(_syncLock));

Expand All @@ -2959,6 +2960,7 @@ private ILoggingService CreateLoggingService(IEnumerable<ILogger> loggers, IEnum
loggingService.OnProjectStarted += _projectStartedEventHandler;
loggingService.OnProjectFinished += _projectFinishedEventHandler;
loggingService.WarningsAsErrors = warningsAsErrors;
loggingService.WarningsNotAsErrors = warningsNotAsErrors;
loggingService.WarningsAsMessages = warningsAsMessages;

try
Expand Down
6 changes: 6 additions & 0 deletions src/Build/BackEnd/BuildManager/BuildParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ internal BuildParameters(BuildParameters other, bool resetEnvironment = false)
_logTaskInputs = other._logTaskInputs;
_logInitialPropertiesAndItems = other._logInitialPropertiesAndItems;
WarningsAsErrors = other.WarningsAsErrors == null ? null : new HashSet<string>(other.WarningsAsErrors, StringComparer.OrdinalIgnoreCase);
WarningsNotAsErrors = other.WarningsNotAsErrors == null ? null : new HashSet<string>(other.WarningsNotAsErrors, StringComparer.OrdinalIgnoreCase);
WarningsAsMessages = other.WarningsAsMessages == null ? null : new HashSet<string>(other.WarningsAsMessages, StringComparer.OrdinalIgnoreCase);
_projectLoadSettings = other._projectLoadSettings;
_interactive = other._interactive;
Expand Down Expand Up @@ -543,6 +544,11 @@ public bool OnlyLogCriticalEvents
/// </summary>
public ISet<string> WarningsAsErrors { get; set; }

/// <summary>
/// A list of warnings to not treat as errors. Only has any effect if WarningsAsErrors is empty.
/// </summary>
public ISet<string> WarningsNotAsErrors { get; set; }

/// <summary>
/// A list of warnings to treat as low importance messages.
/// </summary>
Expand Down
31 changes: 27 additions & 4 deletions src/Build/BackEnd/Components/Logging/ILoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
ISet<string> WarningsNotAsErrors
{
get;
set;
}

/// <summary>
/// A list of warnings to treat as low importance messages.
/// </summary>
Expand Down Expand Up @@ -234,6 +243,13 @@ MessageImportance MinimumRequiredMessageImportance
/// <param name="codes">The list of warning codes to treat as errors.</param>
void AddWarningsAsErrors(BuildEventContext buildEventContext, ISet<string> codes);

/// <summary>
/// Adds a set of warning codes to not treat as errors for the specified project instance ID.
/// </summary>
/// <param name="buildEventContext">A <see cref="BuildEventContext"/> to associate with the list of warning codes.</param>
/// <param name="codes">The list of warning codes not to treat as errors.</param>
void AddWarningsNotAsErrors(BuildEventContext buildEventContext, ISet<string> codes);

/// <summary>
/// Determines if the specified submission has logged an errors.
/// </summary>
Expand All @@ -242,17 +258,24 @@ MessageImportance MinimumRequiredMessageImportance
bool HasBuildSubmissionLoggedErrors(int submissionId);

/// <summary>
/// Returns a hashset of warnings to be logged as errors for the specified project instance ID.
/// Get the warnings that will be promoted to errors for the specified context.
/// </summary>
/// <param name="context">The build context through which warnings will be logged as errors.</param>
/// <returns>A Hashset containing warning codes that should be treated as errors.</returns>
/// <returns>A collection of warning codes that should be treated as errors.</returns>
ICollection<string> GetWarningsAsErrors(BuildEventContext context);

/// <summary>
/// Returns a hashset of warnings to be logged as messages for the specified project instance ID.
/// Get the warnings that will not be promoted to error for the specified context.
/// </summary>
/// <param name="context">The build context through which warnings will not be logged as errors.</param>
/// <returns>A collection of warning codes that should not be treated as errors.</returns>
ICollection<string> GetWarningsNotAsErrors(BuildEventContext context);

/// <summary>
/// Get the warnings that will be demoted to messages for the specified context.
/// </summary>
/// <param name="context">The build context through which warnings will be logged as errors.</param>
/// <returns>A Hashset containing warning codes that should be treated as messages.</returns>
/// <returns>A collection of warning codes that should be treated as messages.</returns>
ICollection<string> GetWarningsAsMessages(BuildEventContext context);

#region Register
Expand Down
Loading

0 comments on commit 7d926d7

Please sign in to comment.