Skip to content

Commit a873a1d

Browse files
committed
Publish on document change if the config file doesn't exist
THIS IS THE BUG FIX
1 parent ea64e3c commit a873a1d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorProjectInfoPublisher.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventAr
171171
}
172172

173173
break;
174+
case ProjectChangeKind.DocumentChanged:
175+
// DocumentChanged normally isn't a great trigger for publishing, given that it happens while a user types
176+
// but for a brand new project, its possible this DocumentChanged actually represents a DocumentOpen, and
177+
// it could be the first one, so its important to publish if there is no project configuration file present
178+
if (ProjectWorkspacePublishable(args) &&
179+
_projectConfigurationFilePathStore.TryGet(args.ProjectKey, out var configurationFilePath) &&
180+
!FileExists(configurationFilePath))
181+
{
182+
ImmediatePublish(args.Newer!);
183+
}
184+
185+
break;
186+
174187
case ProjectChangeKind.DocumentRemoved:
175188
case ProjectChangeKind.DocumentAdded:
176189

@@ -195,6 +208,10 @@ internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventAr
195208
case ProjectChangeKind.ProjectRemoved:
196209
RemovePublishingData(args.Older!);
197210
break;
211+
212+
default:
213+
Debug.Fail("A new ProjectChangeKind has been added that the RazorProjectInfoPublisher doesn't know how to deal with");
214+
break;
198215
}
199216

200217
static bool ProjectWorkspacePublishable(ProjectChangeEventArgs args)

src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/RazorProjectInfoPublisherTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,44 @@ await RunOnDispatcherThreadAsync(() =>
158158
Assert.True(serializationSuccessful);
159159
}
160160

161+
[Fact]
162+
public async Task ProjectManager_Changed_DocumentOpened_InitializedProject_NoFile_Active_Publishes()
163+
{
164+
// Arrange
165+
var serializationSuccessful = false;
166+
var hostProject = new HostProject(@"C:\path\to\project.csproj", @"C:\path\to\obj", RazorConfiguration.Default, rootNamespace: "TestRootNamespace");
167+
var hostDocument = new HostDocument(@"C:\path\to\file.razor", "file.razor");
168+
_projectSnapshotManager.ProjectAdded(hostProject);
169+
_projectSnapshotManager.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Default);
170+
_projectSnapshotManager.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(hostDocument.FilePath));
171+
var projectSnapshot = _projectSnapshotManager.GetProjects()[0];
172+
var expectedConfigurationFilePath = @"C:\path\to\obj\bin\Debug\project.razor.bin";
173+
_projectConfigurationFilePathStore.Set(projectSnapshot.Key, expectedConfigurationFilePath);
174+
var publisher = new TestRazorProjectInfoPublisher(
175+
_projectConfigurationFilePathStore,
176+
onSerializeToFile: (snapshot, configurationFilePath) =>
177+
{
178+
Assert.Equal(expectedConfigurationFilePath, configurationFilePath);
179+
serializationSuccessful = true;
180+
},
181+
configurationFileExists: false)
182+
{
183+
EnqueueDelay = 10,
184+
_active = true
185+
};
186+
publisher.Initialize(_projectSnapshotManager);
187+
188+
// Act
189+
await RunOnDispatcherThreadAsync(() =>
190+
{
191+
_projectSnapshotManager.DocumentOpened(hostProject.Key, hostDocument.FilePath, SourceText.From(string.Empty));
192+
});
193+
194+
// Assert
195+
Assert.Empty(publisher.DeferredPublishTasks);
196+
Assert.True(serializationSuccessful);
197+
}
198+
161199
[Theory]
162200
[InlineData(ProjectChangeKind.DocumentAdded)]
163201
[InlineData(ProjectChangeKind.DocumentRemoved)]

0 commit comments

Comments
 (0)