Don't add already known documents to the misc files project (#10753)

Noticed in the logs that we were double-compiling files, and it was
causing issues in my "self versioned documents" branch. Separated out of
that to make it easier to review.

RCA is because we now share a project manager across VS and LSP server,
so we get real project information much sooner than before, which beats
file watchers etc.
This commit is contained in:
David Wengier 2024-08-19 09:53:10 +10:00 коммит произвёл GitHub
Родитель bc0b702171 f1c3a700f3
Коммит da79940a3a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 48 добавлений и 7 удалений

Просмотреть файл

@ -159,24 +159,24 @@ internal partial class RazorProjectService : IRazorProjectService, IRazorProject
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);
}

Просмотреть файл

@ -733,6 +733,47 @@ public class RazorProjectServiceTest(ITestOutputHelper testOutput) : LanguageSer
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()
{