From 22d0b0005f512cf6b4aeaf7a467ece10873aa80f Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 27 Jul 2024 14:53:01 -0500 Subject: [PATCH 1/3] Treat files that don't compile as errors. closes #1131 --- Src/CSharpier.Cli/CommandLineFormatter.cs | 11 ++-------- .../CommandLineFormatterTests.cs | 22 ++++++++++--------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index c03481a23..f292447ba 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -330,7 +330,7 @@ CommandLineFormatterResult result ) { if ( - (commandLineOptions.StandardInFileContents != null && result.FailedCompilation > 0) + result.FailedCompilation > 0 || (commandLineOptions.Check && result.UnformattedFiles > 0) || result.FailedSyntaxTreeValidation > 0 || result.ExceptionsFormatting > 0 @@ -411,14 +411,7 @@ CancellationToken cancellationToken errorMessage.AppendLine(message.ToString()); } - if (commandLineOptions.WriteStdout) - { - fileIssueLogger.WriteError(errorMessage.ToString()); - } - else - { - fileIssueLogger.WriteWarning(errorMessage.ToString()); - } + fileIssueLogger.WriteError(errorMessage.ToString()); Interlocked.Increment(ref commandLineFormatterResult.FailedCompilation); return; diff --git a/Src/CSharpier.Tests/CommandLineFormatterTests.cs b/Src/CSharpier.Tests/CommandLineFormatterTests.cs index 9b39201e3..134812956 100644 --- a/Src/CSharpier.Tests/CommandLineFormatterTests.cs +++ b/Src/CSharpier.Tests/CommandLineFormatterTests.cs @@ -25,9 +25,11 @@ public void Format_Writes_Failed_To_Compile() var result = this.Format(context); result - .OutputLines.First() + .ErrorOutputLines.First() .Should() - .Be("Warning ./Invalid.cs - Failed to compile so was not formatted."); + .Be("Error ./Invalid.cs - Failed to compile so was not formatted."); + + result.ExitCode.Should().Be(1); } [Test] @@ -39,9 +41,9 @@ public void Format_Writes_Failed_To_Compile_For_Subdirectory() var result = this.Format(context, directoryOrFilePaths: "Subdirectory"); result - .OutputLines.First() + .ErrorOutputLines.First() .Should() - .Be("Warning ./Subdirectory/Invalid.cs - Failed to compile so was not formatted."); + .Be("Error ./Subdirectory/Invalid.cs - Failed to compile so was not formatted."); } [Test] @@ -56,10 +58,10 @@ public void Format_Writes_Failed_To_Compile_For_FullPath() ); result - .OutputLines.First() + .ErrorOutputLines.First() .Should() .Be( - $"Warning {context.GetRootPath().Replace('\\', '/')}/Subdirectory/Invalid.cs - Failed to compile so was not formatted." + $"Error {context.GetRootPath().Replace('\\', '/')}/Subdirectory/Invalid.cs - Failed to compile so was not formatted." ); } @@ -72,9 +74,9 @@ public void Format_Writes_Failed_To_Compile_With_Directory() var result = this.Format(context); result - .OutputLines.First() + .ErrorOutputLines.First() .Should() - .Be("Warning ./Directory/Invalid.cs - Failed to compile so was not formatted."); + .Be("Error ./Directory/Invalid.cs - Failed to compile so was not formatted."); } [Test] @@ -583,9 +585,9 @@ public void File_With_Compilation_Error_Should_Not_Lose_Code() context.GetFileContent("Invalid.cs").Should().Be(contents); result - .OutputLines.First() + .ErrorOutputLines.First() .Should() - .Be("Warning ./Invalid.cs - Failed to compile so was not formatted."); + .Be("Error ./Invalid.cs - Failed to compile so was not formatted."); } [TestCase( From eacbcdab145af07d18f1d11923d1c3a0ee9c40b0 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sun, 4 Aug 2024 14:47:36 -0500 Subject: [PATCH 2/3] Getting csharpier.msbuild happy with the new compilation errors being 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. --- Src/CSharpier.Cli/CommandLineFormatter.cs | 11 ++++++++-- Src/CSharpier.Cli/CommandLineOptions.cs | 6 ++++++ Src/CSharpier.Cli/Program.cs | 4 +++- .../build/CSharpier.MsBuild.targets | 4 +++- .../CommandLineFormatterTests.cs | 21 ++++++++++++++++++- .../net8.0/net8.0.csproj | 2 +- Tests/CSharpier.MsBuild.Test/nuget.config | 10 +++++++++ 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index f292447ba..e6a3e7878 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -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 @@ -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; diff --git a/Src/CSharpier.Cli/CommandLineOptions.cs b/Src/CSharpier.Cli/CommandLineOptions.cs index 263dea685..04b17041d 100644 --- a/Src/CSharpier.Cli/CommandLineOptions.cs +++ b/Src/CSharpier.Cli/CommandLineOptions.cs @@ -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; } @@ -30,6 +31,7 @@ internal delegate Task Handler( bool noCache, bool noMSBuildCheck, bool includeGenerated, + bool compilationErrorsAsWarnings, string config, LogLevel logLevel, CancellationToken cancellationToken @@ -93,6 +95,10 @@ public static RootCommand Create() new Option( 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." ) }; diff --git a/Src/CSharpier.Cli/Program.cs b/Src/CSharpier.Cli/Program.cs index 5e1ace75b..be5ddf5fe 100644 --- a/Src/CSharpier.Cli/Program.cs +++ b/Src/CSharpier.Cli/Program.cs @@ -33,6 +33,7 @@ public static async Task Run( bool noCache, bool noMSBuildCheck, bool includeGenerated, + bool compilationErrorsAsWarnings, string configPath, LogLevel logLevel, CancellationToken cancellationToken @@ -95,7 +96,8 @@ CancellationToken cancellationToken SkipWrite = skipWrite, WriteStdout = writeStdout || standardInFileContents != null, IncludeGenerated = includeGenerated, - ConfigPath = actualConfigPath + ConfigPath = actualConfigPath, + CompilationErrorsAsWarnings = compilationErrorsAsWarnings, }; return await CommandLineFormatter.Format( diff --git a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets index 8c414e6fc..4af14af55 100644 --- a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets +++ b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets @@ -17,12 +17,14 @@ + + IgnoreExitCode="true" + Command="dotnet "$(CSharpierDllPath)" $(CSharpierArgs) --no-msbuild-check --compilation-errors-as-warnings "$(MSBuildProjectDirectory)" > $(NullOutput) " />