Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,20 @@ internal DocumentState UpdateTree(SyntaxNode newRoot, PreservationMode mode)

// use the encoding that we get from the new root
var encoding = newRoot.SyntaxTree.Encoding;
if (encoding is null)
{
// The new tree doesn't specify an encoding. For these cases, continue to use the previous encoding of the
// document.
if (TryGetSyntaxTree(out var priorTree))
{
// this is most likely available since UpdateTree is normally called after modifying the existing tree.
encoding = priorTree.Encoding;
}
else if (TryGetText(out var priorText))
{
encoding = priorText.Encoding;
}
}

var syntaxTreeFactory = LanguageServices.GetRequiredService<ISyntaxTreeFactoryService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2893,13 +2893,14 @@ class C { }";
Assert.Equal(encoding.EncodingName, text.Encoding.EncodingName);
Assert.Equal(fileContent, text.ToString());

// update root blindly again, after observing encoding, see that encoding is overridden to null
// update root blindly again, after observing encoding, see that encoding is preserved
// 🐉 Tools rely on encoding preservation; see https://github.com/dotnet/sdk/issues/46780
var doc3 = document.WithSyntaxRoot(gen.CompilationUnit()); // empty CU
var doc3text = await doc3.GetTextAsync();
Assert.Null(doc3text.Encoding);
Assert.Same(text.Encoding, doc3text.Encoding);
var doc3tree = await doc3.GetSyntaxTreeAsync();
Assert.Null(doc3tree.Encoding);
Assert.Null(doc3tree.GetText().Encoding);
Assert.Same(text.Encoding, doc3tree.Encoding);
Assert.Same(text.Encoding, doc3tree.GetText().Encoding);

// change doc to have no encoding, still succeeds at writing to disk with old encoding
var root = await document.GetSyntaxRootAsync();
Expand Down
Loading