From dd20bec04d785cfb6739a3e03e0cd2f857877271 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 9 Jan 2023 11:54:58 -0600 Subject: [PATCH 1/7] Only read UTF8 and UTF8-BOM encoding closes #768 --- Src/CSharpier.Cli/CommandLineFormatter.cs | 10 +++++++++- Src/CSharpier.Cli/FileReader.cs | 20 ++++++------------- .../TestFiles/FileEncodingAnsi.cst | 4 ---- .../TestFiles/FileEncoding_UTF8.cst | 6 ++++++ .../TestFiles/FileEncoding_UTF8BOM.cst | 6 ++++++ 5 files changed, 27 insertions(+), 19 deletions(-) delete mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncodingAnsi.cst diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index 4c4b8efe4..044bca5a1 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -5,6 +5,8 @@ namespace CSharpier.Cli; +using System.Text; + internal static class CommandLineFormatter { public static async Task Format( @@ -366,7 +368,13 @@ CancellationToken cancellationToken if (codeFormattingResult.Errors.Any()) { - fileIssueLogger.WriteError("Failed to compile so was not formatted."); + var errorMessage = new StringBuilder(); + errorMessage.AppendLine("Failed to compile so was not formatted."); + foreach (var message in codeFormattingResult.Errors) + { + errorMessage.AppendLine(message.ToString()); + } + fileIssueLogger.WriteError(errorMessage.ToString()); Interlocked.Increment(ref commandLineFormatterResult.FailedCompilation); return; } diff --git a/Src/CSharpier.Cli/FileReader.cs b/Src/CSharpier.Cli/FileReader.cs index c9dff4e6d..037c504d9 100644 --- a/Src/CSharpier.Cli/FileReader.cs +++ b/Src/CSharpier.Cli/FileReader.cs @@ -25,24 +25,16 @@ CancellationToken cancellationToken try { await using var fileStream = fileSystem.File.OpenRead(filePath); - var detectionResult = CharsetDetector.DetectFromStream(fileStream); - var encoding = detectionResult?.Detected?.Encoding; - if (encoding == null) - { - unableToDetectEncoding = true; - encoding = Encoding.Default; - } - fileStream.Seek(0, SeekOrigin.Begin); - - // this fixes an issue with ANSI encoded files like csharpier-repos\AutoMapper\src\UnitTests\Internationalization.cs - var encodingToRead = encoding.CodePage == 852 ? Encoding.GetEncoding(1252) : encoding; - - using var streamReader = new StreamReader(fileStream, encodingToRead); + using var streamReader = new StreamReader(fileStream, new UTF8Encoding(false)); var fileContents = await streamReader.ReadToEndAsync(); - return new FileReaderResult(encoding, fileContents, unableToDetectEncoding); + return new FileReaderResult( + streamReader.CurrentEncoding, + fileContents, + unableToDetectEncoding + ); } finally { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncodingAnsi.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncodingAnsi.cst deleted file mode 100644 index 879b4ad62..000000000 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncodingAnsi.cst +++ /dev/null @@ -1,4 +0,0 @@ -public class AnsiCharacters -{ - public string Æøå; -} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8.cst index b2f47e56b..ff8462d1f 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8.cst @@ -2,3 +2,9 @@ public class ClassName { public void MethodName() { } } + +public enum MeetingLocation +{ + Café, + Restaurant +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8BOM.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8BOM.cst index ba08be206..0e622b6b7 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8BOM.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/FileEncoding_UTF8BOM.cst @@ -2,3 +2,9 @@ { public void MethodName() { } } + +public enum MeetingLocation +{ + Café, + Restaurant +} From ace52fb3e7e968e0596f6b966029527e0bcc2db5 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 9 Jan 2023 11:56:25 -0600 Subject: [PATCH 2/7] Removing UTF.Unknown --- Directory.Packages.props | 1 - Src/CSharpier.Cli/CSharpier.Cli.csproj | 1 - Src/CSharpier.Cli/FileReader.cs | 1 - Src/CSharpier.Tests/CSharpier.Tests.csproj | 1 - 4 files changed, 4 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 61e637e59..cf4cfc7e4 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -26,7 +26,6 @@ - diff --git a/Src/CSharpier.Cli/CSharpier.Cli.csproj b/Src/CSharpier.Cli/CSharpier.Cli.csproj index 5dab3a6ed..4467fd72a 100644 --- a/Src/CSharpier.Cli/CSharpier.Cli.csproj +++ b/Src/CSharpier.Cli/CSharpier.Cli.csproj @@ -15,7 +15,6 @@ - diff --git a/Src/CSharpier.Cli/FileReader.cs b/Src/CSharpier.Cli/FileReader.cs index 037c504d9..066011874 100644 --- a/Src/CSharpier.Cli/FileReader.cs +++ b/Src/CSharpier.Cli/FileReader.cs @@ -1,6 +1,5 @@ using System.IO.Abstractions; using System.Text; -using UtfUnknown; namespace CSharpier.Cli; diff --git a/Src/CSharpier.Tests/CSharpier.Tests.csproj b/Src/CSharpier.Tests/CSharpier.Tests.csproj index 6e02dd071..21867ef58 100644 --- a/Src/CSharpier.Tests/CSharpier.Tests.csproj +++ b/Src/CSharpier.Tests/CSharpier.Tests.csproj @@ -10,7 +10,6 @@ - From dc5451426659711118a8d3818270f397c3d1bd37 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 9 Jan 2023 12:02:34 -0600 Subject: [PATCH 3/7] Adding some comments for why we read without BOM --- Src/CSharpier.Cli/FileReader.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Src/CSharpier.Cli/FileReader.cs b/Src/CSharpier.Cli/FileReader.cs index 066011874..d663ec844 100644 --- a/Src/CSharpier.Cli/FileReader.cs +++ b/Src/CSharpier.Cli/FileReader.cs @@ -25,6 +25,9 @@ CancellationToken cancellationToken { await using var fileStream = fileSystem.File.OpenRead(filePath); + // this is kind of a "hack" - read the file without the BOM then + // streamReader.CurrentEncoding below will correctly be UTF8 or UTF8-BOM + // https://stackoverflow.com/a/27976558 using var streamReader = new StreamReader(fileStream, new UTF8Encoding(false)); var fileContents = await streamReader.ReadToEndAsync(); From 559ee933dc980eb388dd91c6a3cddfa7ed39bad5 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Thu, 12 Jan 2023 20:48:33 -0600 Subject: [PATCH 4/7] Update Src/CSharpier.Cli/FileReader.cs Co-authored-by: Lasath Fernando --- Src/CSharpier.Cli/FileReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/CSharpier.Cli/FileReader.cs b/Src/CSharpier.Cli/FileReader.cs index d663ec844..21d6ff657 100644 --- a/Src/CSharpier.Cli/FileReader.cs +++ b/Src/CSharpier.Cli/FileReader.cs @@ -28,7 +28,7 @@ CancellationToken cancellationToken // this is kind of a "hack" - read the file without the BOM then // streamReader.CurrentEncoding below will correctly be UTF8 or UTF8-BOM // https://stackoverflow.com/a/27976558 - using var streamReader = new StreamReader(fileStream, new UTF8Encoding(false)); + using var streamReader = new StreamReader(fileStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); var fileContents = await streamReader.ReadToEndAsync(); From f44ca909626064f76294b97ccd14f87593c54258 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 14 Jan 2023 10:16:56 -0600 Subject: [PATCH 5/7] formatting file --- Src/CSharpier.Cli/FileReader.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Src/CSharpier.Cli/FileReader.cs b/Src/CSharpier.Cli/FileReader.cs index 21d6ff657..ca2c59fe1 100644 --- a/Src/CSharpier.Cli/FileReader.cs +++ b/Src/CSharpier.Cli/FileReader.cs @@ -28,7 +28,10 @@ CancellationToken cancellationToken // this is kind of a "hack" - read the file without the BOM then // streamReader.CurrentEncoding below will correctly be UTF8 or UTF8-BOM // https://stackoverflow.com/a/27976558 - using var streamReader = new StreamReader(fileStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); + using var streamReader = new StreamReader( + fileStream, + new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) + ); var fileContents = await streamReader.ReadToEndAsync(); From 7b88f2a411864ca7f78a99633dfe8026aaf5494f Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 14 Jan 2023 10:19:43 -0600 Subject: [PATCH 6/7] see if this will fix the random build failures --- .github/workflows/validate_pull_request.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/validate_pull_request.yml b/.github/workflows/validate_pull_request.yml index b8c87850d..874ff6d98 100644 --- a/.github/workflows/validate_pull_request.yml +++ b/.github/workflows/validate_pull_request.yml @@ -40,4 +40,5 @@ jobs: 7.0.100 6.0.300 - run: | + dotnet restore Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj dotnet build Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj From 5ac6df1efbac46eb868363de18403baed1981b66 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 14 Jan 2023 10:26:40 -0600 Subject: [PATCH 7/7] fix test --- .github/workflows/validate_pull_request.yml | 1 - Src/CSharpier.Cli.Tests/CliTests.cs | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/validate_pull_request.yml b/.github/workflows/validate_pull_request.yml index 874ff6d98..b8c87850d 100644 --- a/.github/workflows/validate_pull_request.yml +++ b/.github/workflows/validate_pull_request.yml @@ -40,5 +40,4 @@ jobs: 7.0.100 6.0.300 - run: | - dotnet restore Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj dotnet build Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index 1c87d4bdf..ac06129c6 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -222,7 +222,9 @@ public async Task Should_Write_Error_With_Multiple_Piped_Files(string input, str result.ErrorOutput .Should() - .Be($"Error {output} - Failed to compile so was not formatted.{Environment.NewLine}"); + .Be( + $"Error {output} - Failed to compile so was not formatted.{Environment.NewLine} (1,26): error CS1513: }} expected{Environment.NewLine}" + ); result.ExitCode.Should().Be(1); }