Skip to content

Commit d8a4db3

Browse files
committed
Don't allow virtual documents to go back in time
1 parent c416e7c commit d8a4db3

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentPublisher.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,22 @@ public void PublishCSharp(ProjectKey projectKey, string filePath, SourceText sou
7272
previouslyPublishedData = PublishData.Default;
7373
}
7474

75+
if (previouslyPublishedData.HostDocumentVersion > hostDocumentVersion)
76+
{
77+
// We've already published a newer version of this document. No-op.
78+
Debug.Fail("C# document being published that is older than one we've previously published!");
79+
_logger.LogWarning($"Skipping publish of C# for {filePath} because we've already published version {previouslyPublishedData.HostDocumentVersion}, and this request is for {hostDocumentVersion}.");
80+
return;
81+
}
82+
7583
textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);
7684
if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
7785
{
7886
// Source texts match along with host document versions. We've already published something that looks like this. No-op.
7987
return;
8088
}
8189

82-
_logger.LogTrace(
90+
_logger.LogDebug(
8391
$"Updating C# buffer of {filePath} for project {documentKey.ProjectKey} to correspond with host document " +
8492
$"version {hostDocumentVersion}. {previouslyPublishedData.SourceText.Length} -> {sourceText.Length} = Change delta of " +
8593
$"{sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Count} text changes.");
@@ -111,14 +119,22 @@ public void PublishHtml(ProjectKey projectKey, string filePath, SourceText sourc
111119
previouslyPublishedData = PublishData.Default;
112120
}
113121

122+
if (previouslyPublishedData.HostDocumentVersion > hostDocumentVersion)
123+
{
124+
// We've already published a newer version of this document. No-op.
125+
Debug.Fail("Html document being published that is older than one we've previously published!");
126+
_logger.LogWarning($"Skipping publish of Html for {filePath} because we've already published version {previouslyPublishedData.HostDocumentVersion}, and this request is for {hostDocumentVersion}.");
127+
return;
128+
}
129+
114130
textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);
115131
if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
116132
{
117133
// Source texts match along with host document versions. We've already published something that looks like this. No-op.
118134
return;
119135
}
120136

121-
_logger.LogTrace(
137+
_logger.LogDebug(
122138
$"Updating HTML buffer of {filePath} to correspond with host document version {hostDocumentVersion}. {previouslyPublishedData.SourceText.Length} -> {sourceText.Length} = Change delta of {sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Count} text changes.");
123139

124140
_publishedHtmlData[filePath] = new PublishData(sourceText, hostDocumentVersion);

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Endpoints/RazorCustomMessageTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ private async Task<SynchronizedResult<TVirtualDocumentSnapshot>> TrySynchronizeV
146146
return await TempForCohost_TrySynchronizeVirtualDocumentAsync<TVirtualDocumentSnapshot>(hostDocument, cancellationToken);
147147
}
148148

149+
_logger.LogDebug($"Trying to synchronize for {caller} to version {requiredHostDocumentVersion} of {hostDocument.Uri} for {hostDocument.GetProjectContext()?.Id ?? "(no project context)"}");
150+
149151
// For Html documents we don't do anything fancy, just call the standard service
150152
// If we're not generating unique document file names, then we can treat C# documents the same way
151153
if (!_languageServerFeatureOptions.IncludeProjectKeyInGeneratedFilePath ||
@@ -154,8 +156,6 @@ private async Task<SynchronizedResult<TVirtualDocumentSnapshot>> TrySynchronizeV
154156
return await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync<TVirtualDocumentSnapshot>(requiredHostDocumentVersion, hostDocument.Uri, cancellationToken).ConfigureAwait(false);
155157
}
156158

157-
_logger.LogDebug($"Trying to synchronize for {caller} to version {requiredHostDocumentVersion} of {hostDocument.Uri} for {hostDocument.GetProjectContext()?.Id ?? "(no project context)"}");
158-
159159
var virtualDocument = FindVirtualDocument<TVirtualDocumentSnapshot>(hostDocument.Uri, hostDocument.GetProjectContext());
160160

161161
if (virtualDocument is { ProjectKey.IsUnknown: true })

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Endpoints/RazorCustomMessageTarget_UpdateHtmlBuffer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Razor.Logging;
89
using Microsoft.CodeAnalysis.Razor.Protocol;
910
using StreamJsonRpc;
1011

@@ -35,6 +36,9 @@ internal void UpdateHtmlBuffer(UpdateBufferRequest request)
3536
}
3637

3738
var hostDocumentUri = new Uri(request.HostDocumentFilePath);
39+
40+
_logger.LogDebug($"UpdateHtmlBuffer for {request.HostDocumentVersion} of {hostDocumentUri} in {request.ProjectKeyId}");
41+
3842
_documentManager.UpdateVirtualDocument<HtmlVirtualDocument>(
3943
hostDocumentUri,
4044
request.Changes.Select(change => change.ToVisualStudioTextChange()).ToArray(),

0 commit comments

Comments
 (0)