Skip to content

Commit b239817

Browse files
committed
Set ErrorLog property if not set by user
1 parent 890e0f7 commit b239817

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-23
lines changed

README.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,11 @@ development processes.
8686

8787
## Getting Started
8888

89-
### Enabling SARIF logs
89+
### Using SARIF logs
9090

91-
SquiggleCop uses [SARIF](https://sarifweb.azurewebsites.net/) v2.1 files to work its magic. If you aren't already
92-
producing SARIF files as part of your build, set the
93-
[`ErrorLog`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#errorlog)
94-
property, either in a
95-
[Directory.Build.props](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022)
96-
so it automatically applies to all projects:
97-
98-
```xml
99-
<ErrorLog>$(MSBuildProjectFile).diagnostics.sarif,version=2.1</ErrorLog>
100-
```
101-
102-
> [!TIP]
103-
> We recommend you add `*.sarif` to your `.gitignore` file
104-
105-
or on the command-line for ad-hoc validation:
106-
107-
```powershell
108-
dotnet build -p:ErrorLog=diagnostics.sarif%2cversion=2.1
109-
```
110-
111-
> [!IMPORTANT]
112-
> The comma or semi-colon character in the log path must be XML-escaped
91+
SquiggleCop uses [SARIF](https://sarifweb.azurewebsites.net/) v2.1 files to work its magic. SquiggleCop automatically
92+
enables SARIF logs if needed and places them in the `obj/` folder. If you want / need to customize the SARIF output
93+
path, see [Set the SARIF output path](#set-the-sarif-output-path).
11394

11495
### CLI Tool
11596

@@ -285,6 +266,28 @@ Upload your SARIF reports as pipeline artifacts to help narrow down issues.
285266

286267
## Advanced configuration
287268

269+
### Set the SARIF output path
270+
271+
If the [`ErrorLog`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#errorlog)
272+
property is unset, SquiggleCop sets it just before the `CoreCompile` target. To set a custom SARIF output path, set the
273+
property to something like this:
274+
275+
```xml
276+
<ErrorLog>$(MSBuildProjectFile).sarif,version=2.1</ErrorLog>
277+
```
278+
279+
> [!TIP]
280+
> We recommend you add `*.sarif` to your `.gitignore` file
281+
282+
or set the property on the command-line as part of ad-hoc validation:
283+
284+
```powershell
285+
dotnet build -p:ErrorLog=diagnostics.sarif%2cversion=2.1
286+
```
287+
288+
> [!IMPORTANT]
289+
> The comma or semi-colon character in the log path must be XML-escaped
290+
288291
### Alternate baseline paths
289292

290293
By default, SquiggleCop expects the baseline file to be named `SquiggleCop.Baseline.yaml` and placed next to the project

src/Tasks/build/SquiggleCop.Tasks.targets

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,20 @@
2626
AutoBaseline="$(SquiggleCop_AutoBaseline)"
2727
BaselineFiles="@(SquiggleCop_BaselineFiles)"/>
2828
</Target>
29+
30+
<Target Name="_SquiggleCop_SetErrorLog" BeforeTargets="CoreCompile">
31+
<!--
32+
ErrorLog is needed for SquiggleCop.
33+
34+
The value is set in a Target and not directly as a property because `$(IntermediateOutputPath)` and `$(OutputPath)`
35+
are calculated properties and thus shouldn't be relied on during the initial property evaluation phase.
36+
See https://github.com/dotnet/sdk/issues/41852.
37+
38+
We use `$(IntermediateOutputPath)` to ensure the file ends up in the `obj/` folder and not with sources to clearly
39+
delineate inputs and outputs.
40+
-->
41+
<PropertyGroup Condition=" '$(ErrorLog)' == '' ">
42+
<ErrorLog>$(IntermediateOutputPath)$(MSBuildProjectName).sarif,version=2.1</ErrorLog>
43+
</PropertyGroup>
44+
</Target>
2945
</Project>

tests/Tasks.Tests/ErrorLogParameterTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public class ErrorLogParameterTests : TestBase
88
public async Task NoErrorLogReportsWarning()
99
{
1010
ProjectCreator.Templates.SimpleBuild()
11+
.Target(name: "_UnsetErrorLogPropertyBeforeSquiggleCopRuns", beforeTargets: "AfterCompile")
12+
.TaskMessage("Unsetting ErrorLog...")
13+
.TargetPropertyGroup()
14+
.TargetProperty(name: "ErrorLog", unevaluatedValue: string.Empty)
1115
.Save(Path.Combine(TestRootPath, "project.csproj"))
1216
.TryBuild(restore: true, out bool result, out BuildOutput output);
1317

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Build.Framework;
2+
using Microsoft.Build.Utilities.ProjectCreation;
3+
4+
namespace SquiggleCop.Tasks.Tests;
5+
6+
public class ErrorLogPropertyDefaultValueTests : TestBase
7+
{
8+
[Fact]
9+
public void NotExplicitlySet()
10+
{
11+
ProjectCreator project = ProjectCreator.Templates.SimpleBuild()
12+
.Target(name: "OutputErrorLog", beforeTargets: "AfterCompile")
13+
.TaskMessage("ErrorLog=$(ErrorLog)", importance: MessageImportance.High);
14+
15+
project
16+
.Save(Path.Combine(TestRootPath, "project.csproj"))
17+
.TryBuild(restore: true, out bool result, out BuildOutput output);
18+
19+
string[] logs = output.Messages.Where(message => message.Contains("ErrorLog=")).ToArray();
20+
logs.Should().ContainSingle().Which.Should().Be($"ErrorLog=obj{Path.DirectorySeparatorChar}Debug{Path.DirectorySeparatorChar}net8.0{Path.DirectorySeparatorChar}project.sarif,version=2.1");
21+
}
22+
23+
[Fact]
24+
public void ExplicitlySet()
25+
{
26+
ProjectCreator project = ProjectCreator.Templates.SimpleBuild()
27+
.PropertyGroup()
28+
.ErrorLog("explicit.log", "2.1")
29+
.Target(name: "OutputErrorLog", beforeTargets: "AfterCompile")
30+
.TaskMessage("ErrorLog=$(ErrorLog)", importance: MessageImportance.High);
31+
32+
project
33+
.Save(Path.Combine(TestRootPath, "project.csproj"))
34+
.TryBuild(restore: true, out bool result, out BuildOutput output);
35+
36+
string[] logs = output.Messages.Where(message => message.Contains("ErrorLog=")).ToArray();
37+
logs.Should().ContainSingle().Which.Should().Be($"ErrorLog=explicit.log,version=2.1");
38+
}
39+
}

0 commit comments

Comments
 (0)