diff --git a/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/FileBasedPrograms/CanonicalMiscFilesProjectLoader.cs b/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/FileBasedPrograms/CanonicalMiscFilesProjectLoader.cs index 1dc74897b658f..a9666bdb19188 100644 --- a/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/FileBasedPrograms/CanonicalMiscFilesProjectLoader.cs +++ b/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/FileBasedPrograms/CanonicalMiscFilesProjectLoader.cs @@ -124,14 +124,15 @@ public async ValueTask TryRemoveMiscellaneousDocumentAsync(string document return await ExecuteUnderGateAsync(async loadedProjects => { // Try to find and remove the document from the miscellaneous workspace only - var miscWorkspace = _workspaceFactory.MiscellaneousFilesWorkspaceProjectFactory.Workspace; + var solution = _workspaceFactory.MiscellaneousFilesWorkspaceProjectFactory.Workspace.CurrentSolution; - var documents = miscWorkspace.CurrentSolution.GetDocumentIdsWithFilePath(documentPath); - if (documents.Any()) + // Filter to actual documents, ignoring additional documents like Razor files etc. + var documentIds = solution.GetDocumentIdsWithFilePath(documentPath).WhereAsArray(id => solution.GetDocument(id) is not null); + if (documentIds.Length > 0) { await _workspaceFactory.MiscellaneousFilesWorkspaceProjectFactory.ApplyChangeToWorkspaceAsync(workspace => { - foreach (var documentId in documents) + foreach (var documentId in documentIds) { workspace.OnDocumentRemoved(documentId); }