Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/Features/Core/Portable/EditAndContinue/CommittedSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,32 @@ private static bool IsMatchingSourceText(SourceText sourceText, ImmutableArray<b
{
return sourceText;
}
else
{
// The comment above says that we must use the encoding from the IDE.
// However, the problem is that LSP protocol does not provide us encoding information,
// and we receive the content as string, not as byte[].
// There's a stale https://github.com/dotnet/roslyn/issues/63583 about it.

// Here we get 'encoding' from sourceText.Encoding,
// that is always created in DidOpenHandler with System.Text.Encoding.UTF8 (with BOM enabled)
// So by default it always calculates the checksum as BOM is present,
// and if the file is saved without BOM, it will not match the baseline checksum in PDB.
// In case if the current file is saved without BOM, it will cause the whole project marked is stale.
// This is a workaround to handle this specific scenario.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the core of the issue is in LSP server. I'd rather push on getting fix there. This workaround does not address other encodings people might use.

Copy link
Author

@noiseonwires noiseonwires Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, here Roslyn supports only UTF-8.

LSP protocol (and types like DidOpenTextDocumentParams) doesn't have encoding data (already discussed in #63583), so this will probably require much more massive work to be done.

Here we can apply the fallback if we see that checksum check failed. TBH I don't have any clear picture of how we can do something similar in LanguageServer (until we get encoding data from IDE, that will require also updating LSP clients in IDEs).

if (encoding?.GetPreamble().Length > 0)
{
log.Write($"Checksum differs for source file '{sourceFilePath}', trying without preamble", LogMessageSeverity.Warning);
// Try again without BOM
fileStream.Seek(0, SeekOrigin.Begin);
sourceText = SourceText.From(fileStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), checksumAlgorithm);

if (IsMatchingSourceText(sourceText, requiredChecksum, checksumAlgorithm))
{
return sourceText;
}
}
}

log.Write($"Checksum differs for source file '{sourceFilePath}'", LogMessageSeverity.Warning);

Expand Down
Loading