Skip to content

Commit

Permalink
Getting csharpier.msbuild happy with the new compilation errors being…
Browse files Browse the repository at this point in the history
… treated as errors by csharpier.

There didn't appear to be a good way to get msbuild happy with the different possible outcomes without adding the flag to treat compilation errors as warnings.
I tried using IgnoreStandardErrorWarningFormat and IgnoreExitCode in different combinations based on if CSharpier_Check was true or false but couldn't get every scenario working.
  • Loading branch information
belav committed Aug 4, 2024
1 parent 4a6145b commit eacbcda
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ CommandLineFormatterResult result
)
{
if (
result.FailedCompilation > 0
(!commandLineOptions.CompilationErrorsAsWarnings && result.FailedCompilation > 0)
|| (commandLineOptions.Check && result.UnformattedFiles > 0)
|| result.FailedSyntaxTreeValidation > 0
|| result.ExceptionsFormatting > 0
Expand Down Expand Up @@ -411,7 +411,14 @@ CancellationToken cancellationToken
errorMessage.AppendLine(message.ToString());
}

fileIssueLogger.WriteError(errorMessage.ToString());
if (!commandLineOptions.CompilationErrorsAsWarnings)
{
fileIssueLogger.WriteError(errorMessage.ToString());
}
else
{
fileIssueLogger.WriteWarning(errorMessage.ToString());
}

Interlocked.Increment(ref commandLineFormatterResult.FailedCompilation);
return;
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Cli/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class CommandLineOptions
public bool WriteStdout { get; init; }
public bool NoCache { get; init; }
public bool NoMSBuildCheck { get; init; }
public bool CompilationErrorsAsWarnings { get; init; }
public bool IncludeGenerated { get; init; }
public string? StandardInFileContents { get; init; }
public string? ConfigPath { get; init; }
Expand All @@ -30,6 +31,7 @@ internal delegate Task<int> Handler(
bool noCache,
bool noMSBuildCheck,
bool includeGenerated,
bool compilationErrorsAsWarnings,
string config,
LogLevel logLevel,
CancellationToken cancellationToken
Expand Down Expand Up @@ -93,6 +95,10 @@ public static RootCommand Create()
new Option<string>(
new[] { "--config-path" },
"Path to the CSharpier configuration file"
),
new Option(
new[] { "--compilation-errors-as-warnings" },
"Treat compilation errors from files as warnings instead of errors."
)
};

Expand Down
4 changes: 3 additions & 1 deletion Src/CSharpier.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static async Task<int> Run(
bool noCache,
bool noMSBuildCheck,
bool includeGenerated,
bool compilationErrorsAsWarnings,
string configPath,
LogLevel logLevel,
CancellationToken cancellationToken
Expand Down Expand Up @@ -95,7 +96,8 @@ CancellationToken cancellationToken
SkipWrite = skipWrite,
WriteStdout = writeStdout || standardInFileContents != null,
IncludeGenerated = includeGenerated,
ConfigPath = actualConfigPath
ConfigPath = actualConfigPath,
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
};

return await CommandLineFormatter.Format(
Expand Down
4 changes: 3 additions & 1 deletion Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
</PropertyGroup>

<Target Name="CSharpierFormatInner" Condition="'$(CSharpier_Bypass)' != 'true'">
<!-- IgnoreExitCode cleans up the output when files aren't formatted and fail the check -->
<!-- &gt; NullOutput suppresses the output from this so that compilation errors will show up a single time -->
<Exec
ConsoleToMSBuild="true"
StdOutEncoding="utf-8"
StdErrEncoding="utf-8"
Command="dotnet &quot;$(CSharpierDllPath)&quot; $(CSharpierArgs) --no-msbuild-check &quot;$(MSBuildProjectDirectory)&quot; &gt; $(NullOutput) " />
IgnoreExitCode="true"
Command="dotnet &quot;$(CSharpierDllPath)&quot; $(CSharpierArgs) --no-msbuild-check --compilation-errors-as-warnings &quot;$(MSBuildProjectDirectory)&quot; &gt; $(NullOutput) " />
</Target>

<!-- getting this to run a single time for projects that target multiple frameworks requires all of this
Expand Down
21 changes: 20 additions & 1 deletion Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public void Format_Writes_Failed_To_Compile()
result.ExitCode.Should().Be(1);
}

[Test]
public void Format_Writes_Failed_To_Compile_As_Warning()
{
var context = new TestContext();
context.WhenAFileExists("Invalid.cs", "asdfasfasdf");

var result = this.Format(context, compilationErrorsAsWarnings: true);

result
.OutputLines.First()
.Should()
.Be("Warning ./Invalid.cs - Failed to compile so was not formatted.");

result.ExitCode.Should().Be(0);
}

[Test]
public void Format_Writes_Failed_To_Compile_For_Subdirectory()
{
Expand Down Expand Up @@ -93,6 +109,7 @@ public void Format_Writes_Unsupported()
.Be(@"Warning ./Unsupported.js - Is an unsupported file type.");
}

[Test]
public void Format_Writes_File_With_Directory_Path()
{
var context = new TestContext();
Expand Down Expand Up @@ -654,6 +671,7 @@ private FormatResult Format(
bool check = false,
bool writeStdout = false,
bool includeGenerated = false,
bool compilationErrorsAsWarnings = false,
string? standardInFileContents = null,
params string[] directoryOrFilePaths
)
Expand Down Expand Up @@ -683,7 +701,8 @@ params string[] directoryOrFilePaths
Check = check,
WriteStdout = writeStdout || standardInFileContents != null,
StandardInFileContents = standardInFileContents,
IncludeGenerated = includeGenerated
IncludeGenerated = includeGenerated,
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
},
context.FileSystem,
fakeConsole,
Expand Down
2 changes: 1 addition & 1 deletion Tests/CSharpier.MsBuild.Test/net8.0/net8.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.0.1">
<PackageReference Include="CSharpier.MsBuild" Version="0.29.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
10 changes: 10 additions & 0 deletions Tests/CSharpier.MsBuild.Test/nuget.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Local" value="../../nupkg" />
</packageSources>
<packageSourceMapping>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
<packageSource key="Local">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>

0 comments on commit eacbcda

Please sign in to comment.