diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs index 74ebb392272..57e0def447f 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs @@ -159,24 +159,24 @@ await _projectManager private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater, string filePath) { var textDocumentPath = FilePathNormalizer.Normalize(filePath); + _logger.LogDebug($"Asked to add {textDocumentPath} to the miscellaneous files project, because we don't have project info (yet?)"); - _logger.LogDebug($"Adding {filePath} to the miscellaneous files project, because we don't have project info (yet?)"); - var miscFilesProject = _projectManager.GetMiscellaneousProject(); - - if (miscFilesProject.GetDocument(FilePathNormalizer.Normalize(textDocumentPath)) is not null) + if (_projectManager.TryResolveDocumentInAnyProject(textDocumentPath, _logger, out var document)) { - // Document already added. This usually occurs when VSCode has already pre-initialized - // open documents and then we try to manually add all known razor documents. + // Already in a known project, so we don't want it in the misc files project + _logger.LogDebug($"File {textDocumentPath} is already in {document.Project.Key}, so we're not adding it to the miscellaneous files project"); return; } + var miscFilesProject = _projectManager.GetMiscellaneousProject(); + // Representing all of our host documents with a re-normalized target path to workaround GetRelatedDocument limitations. var normalizedTargetFilePath = textDocumentPath.Replace('/', '\\').TrimStart('\\'); var hostDocument = new HostDocument(textDocumentPath, normalizedTargetFilePath); var textLoader = _remoteTextLoaderFactory.Create(textDocumentPath); - _logger.LogInformation($"Adding document '{filePath}' to project '{miscFilesProject.Key}'."); + _logger.LogInformation($"Adding document '{textDocumentPath}' to project '{miscFilesProject.Key}'."); updater.DocumentAdded(miscFilesProject.Key, hostDocument, textLoader); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs index 0802a4cf61d..326170da442 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs @@ -733,6 +733,47 @@ public async Task AddDocumentToMiscProjectAsync_AddsDocumentToMiscellaneousProje Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath)); } + [Fact] + public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument() + { + // Arrange + const string ProjectFilePath = "C:/path/to/project.csproj"; + const string IntermediateOutputPath = "C:/path/to/obj"; + const string RootNamespace = "TestRootNamespace"; + const string DocumentFilePath = "C:/path/to/document.cshtml"; + + await _projectService.AddProjectAsync( + ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken); + await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken); + + // Act + using var listener = _projectManager.ListenToNotifications(); + + // Act + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Assert + listener.AssertNoNotifications(); + } + + [Fact] + public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument_InMiscFiles() + { + // Arrange + const string DocumentFilePath = "C:/path/to/document.cshtml"; + + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Act + using var listener = _projectManager.ListenToNotifications(); + + // Act + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Assert + listener.AssertNoNotifications(); + } + [Fact] public async Task RemoveDocument_RemovesDocumentFromOwnerProject() {