Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat files that don't compile as errors. #1311

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 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 (
(commandLineOptions.StandardInFileContents != null && 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,7 @@ CancellationToken cancellationToken
errorMessage.AppendLine(message.ToString());
}

if (commandLineOptions.WriteStdout)
if (!commandLineOptions.CompilationErrorsAsWarnings)
{
fileIssueLogger.WriteError(errorMessage.ToString());
}
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
39 changes: 30 additions & 9 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,28 @@ public void Format_Writes_Failed_To_Compile()

var result = this.Format(context);

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

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]
Expand All @@ -39,9 +57,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]
Expand All @@ -56,10 +74,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."
);
}

Expand All @@ -72,9 +90,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]
Expand All @@ -91,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 @@ -583,9 +602,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(
Expand Down Expand Up @@ -652,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 @@ -681,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
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>
Loading