Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 18113de

Browse files
committed
Save with original Encoding
The Encoding of a file in Roslyn can change as it is rewritten. Make sure to save with the original file encoding.
1 parent 06a9f9c commit 18113de

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,12 @@ private async Task SaveChanges(Solution solution, Solution originalSolution, Can
105105
var sourceText = await document.GetTextAsync(cancellationToken);
106106
using (var file = File.Open(document.FilePath, FileMode.Truncate, FileAccess.Write))
107107
{
108-
var encoding = sourceText.Encoding;
108+
// The encoding of the file can change during the rewrite. Make sure to save with the
109+
// original encoding
110+
var originalDocument = originalSolution.GetDocument(documentId);
111+
var originalSourceText = await originalDocument.GetTextAsync(cancellationToken);
109112

110-
// TODO: It seems like a bug that Encoding could change but it is definitely
111-
// happening. Ex: ArrayBuilder.Enumerator.cs
112-
if (encoding == null)
113-
{
114-
var originalDocument = originalSolution.GetDocument(documentId);
115-
var originalSourceText = await originalDocument.GetTextAsync(cancellationToken);
116-
encoding = originalSourceText.Encoding;
117-
}
118-
119-
using (var writer = new StreamWriter(file, encoding))
113+
using (var writer = new StreamWriter(file, originalSourceText.Encoding))
120114
{
121115
sourceText.Write(writer, cancellationToken);
122116
}

src/Microsoft.DotNet.CodeFormatting/Rules/HasNoIllegalHeadersFormattingRule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal sealed class HasNoIllegalHeadersFormattingRule : ILocalSemanticFormatti
2424
// We are going to remove any multiline comments that *only* contain these characters
2525
private const string CommentFormattingCharacters = "*/=-";
2626

27-
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
27+
public Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
2828
{
2929
var leadingTrivia = syntaxNode.GetLeadingTrivia();
3030
SyntaxTriviaList newTrivia = leadingTrivia;
@@ -71,9 +71,9 @@ public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxN
7171
}
7272

7373
if (leadingTrivia.Equals(newTrivia))
74-
return syntaxNode;
74+
return Task.FromResult(syntaxNode);
7575

76-
return syntaxNode.WithLeadingTrivia(newTrivia);
76+
return Task.FromResult(syntaxNode.WithLeadingTrivia(newTrivia));
7777
}
7878

7979
private string[] GetIllegalHeaders(Document document)

0 commit comments

Comments
 (0)