Skip to content

Commit 0cf2e42

Browse files
author
Andrew Hall
committed
Allow graceful handling of ArgumentException when trying to apply changes to SourceText in LspTextChangesLoader (#11727)
Helps mitigate dotnet/vscode-csharp#8147
1 parent bb34eb8 commit 0cf2e42

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/LspDynamicFileProvider.LspTextChangesTextLoader.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,32 @@ private sealed class LspTextChangesTextLoader(
3636

3737
public override async Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptions options, CancellationToken cancellationToken)
3838
{
39-
if (_document is null)
39+
try
4040
{
41-
var text = ApplyChanges(_emptySourceText.Value, _changes);
42-
return TextAndVersion.Create(text, VersionStamp.Default.GetNewerVersion());
43-
}
41+
if (_document is null)
42+
{
43+
var text = ApplyChanges(_emptySourceText.Value, _changes);
44+
return TextAndVersion.Create(text, VersionStamp.Default.GetNewerVersion());
45+
}
46+
47+
var sourceText = await _document.GetTextAsync(cancellationToken).ConfigureAwait(false);
4448

45-
var sourceText = await _document.GetTextAsync(cancellationToken).ConfigureAwait(false);
49+
// Validate the checksum information so the edits are known to be correct
4650

47-
// Validate the checksum information so the edits are known to be correct
48-
if (IsSourceTextMatching(sourceText))
51+
if (IsSourceTextMatching(sourceText))
52+
{
53+
var version = await _document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false);
54+
var newText = ApplyChanges(sourceText, _changes);
55+
return TextAndVersion.Create(newText, version.GetNewerVersion());
56+
}
57+
}
58+
catch (Exception ex) when (ex is not OperationCanceledException)
4959
{
50-
var version = await _document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false);
51-
var newText = ApplyChanges(sourceText, _changes);
52-
return TextAndVersion.Create(newText, version.GetNewerVersion());
60+
// This happens if ApplyChanges tries to apply an invalid TextChange.
61+
// This is recoverable but incurs a perf hit for getting the full text below.
62+
63+
// TODO: Add ability to capture a fault here in EA. There's something wrong if
64+
// the Checksum matches but the text changes can't be applied.
5365
}
5466

5567
return await GetFullDocumentFromServerAsync(cancellationToken).ConfigureAwait(false);

0 commit comments

Comments
 (0)