Fix source generator updates using additional files

This commit is contained in:
Matt Parker
2025-11-28 18:08:18 +10:00
parent 5bebf7e677
commit f4b1e9c1c0
2 changed files with 5 additions and 10 deletions

View File

@@ -919,15 +919,10 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
Guard.Against.NullOrEmpty(newContent, nameof(newContent)); Guard.Against.NullOrEmpty(newContent, nameof(newContent));
var documentIdsWithFilePath = _workspace!.CurrentSolution.GetDocumentIdsWithFilePath(fileModel.Path); var documentIdsWithFilePath = _workspace!.CurrentSolution.GetDocumentIdsWithFilePath(fileModel.Path);
var documentId = documentIdsWithFilePath switch var documentId = documentIdsWithFilePath.FirstOrDefault(); // Linked files should take care of the rest of the documents with the same path
{
{Length: 1} => documentIdsWithFilePath[0],
{Length: > 1} => documentIdsWithFilePath.SingleOrDefault(d => d.ProjectId == GetProjectForSharpIdeFile(fileModel).Id),
_ => null
};
if (documentId is null) if (documentId is null)
{ {
_logger.LogWarning("UpdateDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path); _logger.LogTrace("UpdateDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path);
return false; return false;
} }

View File

@@ -107,10 +107,9 @@ public class FileChangedService
} }
var afterSaveTask = (file, changeType) switch var afterSaveTask = (file, changeType) switch
{ {
({ IsRoslynWorkspaceFile: true }, _) => HandleWorkspaceFileChanged(file, newContents),
({ IsCsprojFile: true }, FileChangeType.IdeSaveToDisk or FileChangeType.ExternalChange) => HandleCsprojChanged(file), ({ IsCsprojFile: true }, FileChangeType.IdeSaveToDisk or FileChangeType.ExternalChange) => HandleCsprojChanged(file),
({ IsCsprojFile: true }, _) => Task.CompletedTask, ({ IsCsprojFile: true }, _) => Task.CompletedTask,
_ => throw new InvalidOperationException("Unknown file change type.") (_, _) => HandlePotentialWorkspaceFile_Changed(file, newContents)
}; };
await afterSaveTask; await afterSaveTask;
} }
@@ -130,7 +129,8 @@ public class FileChangedService
_updateSolutionDiagnosticsQueue.AddWork(); _updateSolutionDiagnosticsQueue.AddWork();
} }
private async Task HandleWorkspaceFileChanged(SharpIdeFile file, string newContents) /// AdditionalFiles such as txt files may have changed, so we need to attempt to update the workspace regardless of extension
private async Task HandlePotentialWorkspaceFile_Changed(SharpIdeFile file, string newContents)
{ {
var fileUpdatedInWorkspace = await _roslynAnalysis.UpdateDocument(file, newContents); var fileUpdatedInWorkspace = await _roslynAnalysis.UpdateDocument(file, newContents);
if (fileUpdatedInWorkspace is false) return; if (fileUpdatedInWorkspace is false) return;