-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BuildCheck has better message when Check fails on Initialize (#10612)
Partial fix of #10522 Context When a Check throws an exception it falls with an internal logger exception and crashes the build. This is not ideal. It decided that when a Check fails, we do not fail the build, just give a warning and deregister the check. Changes Made Changed the BuildCheck logger to catch initialization exceptions, and dispatch them as warnings and not errors. Testing Added end to end test with a sample custom check that throws an exception. Notes This is just a catch for exception on Check initialization. There are tests added for other cases but the fix will be different for them, so it will be done in another PR.
- Loading branch information
1 parent
b1e6c25
commit 2206a05
Showing
15 changed files
with
283 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/ErrorCustomCheck.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\Common\CommonTest.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="ErrorCustomCheck.props" Pack="true" PackagePath="build\ErrorCustomCheck.props" /> | ||
<Content Include="README.md" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\Common\CommonTest.targets" /> | ||
|
||
</Project> |
6 changes: 6 additions & 0 deletions
6
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/ErrorCustomCheck.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<PropertyGroup> | ||
<MSBuildCheck>$([MSBuild]::RegisterBuildCheck($(MSBuildThisFileDirectory)ErrorCustomCheck.dll))</MSBuildCheck> | ||
</PropertyGroup> | ||
</Project> |
43 changes: 43 additions & 0 deletions
43
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/ErrorOnInitializeCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Build.Construction; | ||
using Microsoft.Build.Experimental.BuildCheck; | ||
|
||
namespace ErrorCustomCheck | ||
{ | ||
public sealed class ErrorOnInitializeCheck : Check | ||
{ | ||
public static CheckRule SupportedRule = new CheckRule( | ||
"X01236", | ||
"Title", | ||
"Description", | ||
"Message format: {0}", | ||
new CheckConfiguration()); | ||
|
||
public override string FriendlyName => "ErrorOnInitializeCheck"; | ||
|
||
public override IReadOnlyList<CheckRule> SupportedRules { get; } = new List<CheckRule>() { SupportedRule }; | ||
|
||
public override void Initialize(ConfigurationContext configurationContext) | ||
{ | ||
// configurationContext to be used only if check needs external configuration data. | ||
throw new Exception("Something went wrong initializing"); | ||
} | ||
|
||
public override void RegisterActions(IBuildCheckRegistrationContext registrationContext) | ||
{ | ||
registrationContext.RegisterEvaluatedPropertiesAction(EvaluatedPropertiesAction); | ||
} | ||
|
||
private void EvaluatedPropertiesAction(BuildCheckDataContext<EvaluatedPropertiesCheckData> context) | ||
{ | ||
context.ReportResult(BuildCheckResult.Create( | ||
SupportedRule, | ||
ElementLocation.EmptyLocation, | ||
"This check should have been disabled")); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/ErrorOnRegisteredAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Build.Construction; | ||
using Microsoft.Build.Experimental.BuildCheck; | ||
|
||
namespace ErrorCustomCheck | ||
{ | ||
public sealed class ErrorOnRegisteredAction : Check | ||
{ | ||
public static CheckRule SupportedRule = new CheckRule( | ||
"X01237", | ||
"Title", | ||
"Description", | ||
"Message format: {0}", | ||
new CheckConfiguration()); | ||
|
||
public override string FriendlyName => "ErrorOnEvaluatedPropertiesCheck"; | ||
|
||
public override IReadOnlyList<CheckRule> SupportedRules { get; } = new List<CheckRule>() { SupportedRule }; | ||
|
||
public override void Initialize(ConfigurationContext configurationContext) | ||
{ | ||
// configurationContext to be used only if check needs external configuration data. | ||
} | ||
|
||
public override void RegisterActions(IBuildCheckRegistrationContext registrationContext) | ||
{ | ||
registrationContext.RegisterEvaluatedPropertiesAction(EvaluatedPropertiesAction); | ||
} | ||
|
||
private void EvaluatedPropertiesAction(BuildCheckDataContext<EvaluatedPropertiesCheckData> context) | ||
{ | ||
throw new Exception("something went wrong"); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/ErrorWhenRegisteringActions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Build.Construction; | ||
using Microsoft.Build.Experimental.BuildCheck; | ||
|
||
namespace ErrorCustomCheck | ||
{ | ||
public sealed class ErrorWhenRegisteringActions : Check | ||
{ | ||
public static CheckRule SupportedRule = new CheckRule( | ||
"X01238", | ||
"Title", | ||
"Description", | ||
"Message format: {0}", | ||
new CheckConfiguration()); | ||
|
||
public override string FriendlyName => "ErrorOnEvaluatedPropertiesCheck"; | ||
|
||
public override IReadOnlyList<CheckRule> SupportedRules { get; } = new List<CheckRule>() { SupportedRule }; | ||
|
||
public override void Initialize(ConfigurationContext configurationContext) | ||
{ | ||
// configurationContext to be used only if check needs external configuration data. | ||
} | ||
|
||
public override void RegisterActions(IBuildCheckRegistrationContext registrationContext) | ||
{ | ||
registrationContext.RegisterEvaluatedPropertiesAction(EvaluatedPropertiesAction); | ||
throw new Exception("something went wrong"); | ||
} | ||
|
||
private void EvaluatedPropertiesAction(BuildCheckDataContext<EvaluatedPropertiesCheckData> context) | ||
{ | ||
context.ReportResult(BuildCheckResult.Create( | ||
SupportedRule, | ||
ElementLocation.EmptyLocation, | ||
"This check should have been disabled")); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/BuildCheck.UnitTests/TestAssets/ErrorCustomCheck/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# MSBuild Custom Check Template | ||
|
||
## Overview | ||
MSBuild Custom Check Template is a .NET template designed to streamline the creation of MSBuild check libraries. This template facilitates the development of custom checks targeting .NET Standard, enabling developers to inspect and enforce conventions, standards, or patterns within their MSBuild builds. | ||
|
||
## Features | ||
- Simplified template for creating MSBuild check libraries. | ||
- Targeting .NET Standard for cross-platform compatibility. | ||
- Provides a starting point for implementing custom check rules. | ||
|
||
## Getting Started | ||
To use the MSBuild Custom Check Template, follow these steps: | ||
1. Install the template using the following command: | ||
```bash | ||
dotnet new install msbuildcheck | ||
2. Instantiate a custom template: | ||
```bash | ||
dotnet new msbuildcheck -n <ProjectName> | ||
### Prerequisites | ||
- .NET SDK installed on your machine. |