Skip to content

Commit 0df263d

Browse files
Treat files that don't compile as errors. (#1311)
closes #1131 --------- Co-authored-by: Lasath Fernando <[email protected]>
1 parent 7d1dc56 commit 0df263d

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
lines changed

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ CommandLineFormatterResult result
330330
)
331331
{
332332
if (
333-
(commandLineOptions.StandardInFileContents != null && result.FailedCompilation > 0)
333+
(!commandLineOptions.CompilationErrorsAsWarnings && result.FailedCompilation > 0)
334334
|| (commandLineOptions.Check && result.UnformattedFiles > 0)
335335
|| result.FailedSyntaxTreeValidation > 0
336336
|| result.ExceptionsFormatting > 0
@@ -411,7 +411,7 @@ CancellationToken cancellationToken
411411
errorMessage.AppendLine(message.ToString());
412412
}
413413

414-
if (commandLineOptions.WriteStdout)
414+
if (!commandLineOptions.CompilationErrorsAsWarnings)
415415
{
416416
fileIssueLogger.WriteError(errorMessage.ToString());
417417
}

Src/CSharpier.Cli/CommandLineOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal class CommandLineOptions
1313
public bool WriteStdout { get; init; }
1414
public bool NoCache { get; init; }
1515
public bool NoMSBuildCheck { get; init; }
16+
public bool CompilationErrorsAsWarnings { get; init; }
1617
public bool IncludeGenerated { get; init; }
1718
public string? StandardInFileContents { get; init; }
1819
public string? ConfigPath { get; init; }
@@ -30,6 +31,7 @@ internal delegate Task<int> Handler(
3031
bool noCache,
3132
bool noMSBuildCheck,
3233
bool includeGenerated,
34+
bool compilationErrorsAsWarnings,
3335
string config,
3436
LogLevel logLevel,
3537
CancellationToken cancellationToken
@@ -93,6 +95,10 @@ public static RootCommand Create()
9395
new Option<string>(
9496
new[] { "--config-path" },
9597
"Path to the CSharpier configuration file"
98+
),
99+
new Option(
100+
new[] { "--compilation-errors-as-warnings" },
101+
"Treat compilation errors from files as warnings instead of errors."
96102
)
97103
};
98104

Src/CSharpier.Cli/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static async Task<int> Run(
3333
bool noCache,
3434
bool noMSBuildCheck,
3535
bool includeGenerated,
36+
bool compilationErrorsAsWarnings,
3637
string configPath,
3738
LogLevel logLevel,
3839
CancellationToken cancellationToken
@@ -95,7 +96,8 @@ CancellationToken cancellationToken
9596
SkipWrite = skipWrite,
9697
WriteStdout = writeStdout || standardInFileContents != null,
9798
IncludeGenerated = includeGenerated,
98-
ConfigPath = actualConfigPath
99+
ConfigPath = actualConfigPath,
100+
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
99101
};
100102

101103
return await CommandLineFormatter.Format(

Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
</PropertyGroup>
1818

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

2830
<!-- getting this to run a single time for projects that target multiple frameworks requires all of this

Src/CSharpier.Tests/CommandLineFormatterTests.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,28 @@ public void Format_Writes_Failed_To_Compile()
2424

2525
var result = this.Format(context);
2626

27+
result
28+
.ErrorOutputLines.First()
29+
.Should()
30+
.Be("Error ./Invalid.cs - Failed to compile so was not formatted.");
31+
32+
result.ExitCode.Should().Be(1);
33+
}
34+
35+
[Test]
36+
public void Format_Writes_Failed_To_Compile_As_Warning()
37+
{
38+
var context = new TestContext();
39+
context.WhenAFileExists("Invalid.cs", "asdfasfasdf");
40+
41+
var result = this.Format(context, compilationErrorsAsWarnings: true);
42+
2743
result
2844
.OutputLines.First()
2945
.Should()
3046
.Be("Warning ./Invalid.cs - Failed to compile so was not formatted.");
47+
48+
result.ExitCode.Should().Be(0);
3149
}
3250

3351
[Test]
@@ -39,9 +57,9 @@ public void Format_Writes_Failed_To_Compile_For_Subdirectory()
3957
var result = this.Format(context, directoryOrFilePaths: "Subdirectory");
4058

4159
result
42-
.OutputLines.First()
60+
.ErrorOutputLines.First()
4361
.Should()
44-
.Be("Warning ./Subdirectory/Invalid.cs - Failed to compile so was not formatted.");
62+
.Be("Error ./Subdirectory/Invalid.cs - Failed to compile so was not formatted.");
4563
}
4664

4765
[Test]
@@ -56,10 +74,10 @@ public void Format_Writes_Failed_To_Compile_For_FullPath()
5674
);
5775

5876
result
59-
.OutputLines.First()
77+
.ErrorOutputLines.First()
6078
.Should()
6179
.Be(
62-
$"Warning {context.GetRootPath().Replace('\\', '/')}/Subdirectory/Invalid.cs - Failed to compile so was not formatted."
80+
$"Error {context.GetRootPath().Replace('\\', '/')}/Subdirectory/Invalid.cs - Failed to compile so was not formatted."
6381
);
6482
}
6583

@@ -72,9 +90,9 @@ public void Format_Writes_Failed_To_Compile_With_Directory()
7290
var result = this.Format(context);
7391

7492
result
75-
.OutputLines.First()
93+
.ErrorOutputLines.First()
7694
.Should()
77-
.Be("Warning ./Directory/Invalid.cs - Failed to compile so was not formatted.");
95+
.Be("Error ./Directory/Invalid.cs - Failed to compile so was not formatted.");
7896
}
7997

8098
[Test]
@@ -91,6 +109,7 @@ public void Format_Writes_Unsupported()
91109
.Be(@"Warning ./Unsupported.js - Is an unsupported file type.");
92110
}
93111

112+
[Test]
94113
public void Format_Writes_File_With_Directory_Path()
95114
{
96115
var context = new TestContext();
@@ -583,9 +602,9 @@ public void File_With_Compilation_Error_Should_Not_Lose_Code()
583602

584603
context.GetFileContent("Invalid.cs").Should().Be(contents);
585604
result
586-
.OutputLines.First()
605+
.ErrorOutputLines.First()
587606
.Should()
588-
.Be("Warning ./Invalid.cs - Failed to compile so was not formatted.");
607+
.Be("Error ./Invalid.cs - Failed to compile so was not formatted.");
589608
}
590609

591610
[TestCase(
@@ -652,6 +671,7 @@ private FormatResult Format(
652671
bool check = false,
653672
bool writeStdout = false,
654673
bool includeGenerated = false,
674+
bool compilationErrorsAsWarnings = false,
655675
string? standardInFileContents = null,
656676
params string[] directoryOrFilePaths
657677
)
@@ -681,7 +701,8 @@ params string[] directoryOrFilePaths
681701
Check = check,
682702
WriteStdout = writeStdout || standardInFileContents != null,
683703
StandardInFileContents = standardInFileContents,
684-
IncludeGenerated = includeGenerated
704+
IncludeGenerated = includeGenerated,
705+
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
685706
},
686707
context.FileSystem,
687708
fakeConsole,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
4+
<clear />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
46
<add key="Local" value="../../nupkg" />
57
</packageSources>
8+
<packageSourceMapping>
9+
<packageSource key="nuget.org">
10+
<package pattern="*" />
11+
</packageSource>
12+
<packageSource key="Local">
13+
<package pattern="*" />
14+
</packageSource>
15+
</packageSourceMapping>
616
</configuration>

0 commit comments

Comments
 (0)