Make sure we use the right project in the updated project snapshot

This seems like the cause of https://github.com/dotnet/razor/issues/10865
This commit is contained in:
David Wengier 2024-09-16 11:20:30 +10:00
Родитель f929020b39
Коммит 19860435fc
2 изменённых файлов: 9 добавлений и 9 удалений

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

@ -4,18 +4,16 @@
using System; using System;
using System.Composition; using System.Composition;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis.Razor.Workspaces;
namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
[Export(typeof(DocumentSnapshotFactory)), Shared] [Export(typeof(DocumentSnapshotFactory)), Shared]
[method: ImportingConstructor] [method: ImportingConstructor]
internal class DocumentSnapshotFactory(Lazy<ProjectSnapshotFactory> projectSnapshotFactory, IFilePathService filePathService) internal class DocumentSnapshotFactory(Lazy<ProjectSnapshotFactory> projectSnapshotFactory)
{ {
private static readonly ConditionalWeakTable<TextDocument, RemoteDocumentSnapshot> s_documentSnapshots = new(); private static readonly ConditionalWeakTable<TextDocument, RemoteDocumentSnapshot> s_documentSnapshots = new();
private readonly Lazy<ProjectSnapshotFactory> _projectSnapshotFactory = projectSnapshotFactory; private readonly Lazy<ProjectSnapshotFactory> _projectSnapshotFactory = projectSnapshotFactory;
private readonly IFilePathService _filePathService = filePathService;
public RemoteDocumentSnapshot GetOrCreate(TextDocument textDocument) public RemoteDocumentSnapshot GetOrCreate(TextDocument textDocument)
{ {
@ -23,8 +21,9 @@ internal class DocumentSnapshotFactory(Lazy<ProjectSnapshotFactory> projectSnaps
{ {
if (!s_documentSnapshots.TryGetValue(textDocument, out var documentSnapshot)) if (!s_documentSnapshots.TryGetValue(textDocument, out var documentSnapshot))
{ {
var projectSnapshot = _projectSnapshotFactory.Value.GetOrCreate(textDocument.Project); var projectSnapshotFactory = _projectSnapshotFactory.Value;
documentSnapshot = new RemoteDocumentSnapshot(textDocument, projectSnapshot, _filePathService); var projectSnapshot = projectSnapshotFactory.GetOrCreate(textDocument.Project);
documentSnapshot = new RemoteDocumentSnapshot(textDocument, projectSnapshot, projectSnapshotFactory);
s_documentSnapshots.Add(textDocument, documentSnapshot); s_documentSnapshots.Add(textDocument, documentSnapshot);
} }

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

@ -7,16 +7,15 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
internal class RemoteDocumentSnapshot(TextDocument textDocument, RemoteProjectSnapshot projectSnapshot, IFilePathService filePathService) : IDocumentSnapshot internal class RemoteDocumentSnapshot(TextDocument textDocument, RemoteProjectSnapshot projectSnapshot, ProjectSnapshotFactory projectSnapshotFactory) : IDocumentSnapshot
{ {
private readonly TextDocument _textDocument = textDocument; private readonly TextDocument _textDocument = textDocument;
private readonly RemoteProjectSnapshot _projectSnapshot = projectSnapshot; private readonly RemoteProjectSnapshot _projectSnapshot = projectSnapshot;
private readonly IFilePathService _filePathService = filePathService; private readonly ProjectSnapshotFactory _projectSnapshotFactory = projectSnapshotFactory;
// TODO: Delete this field when the source generator is hooked up // TODO: Delete this field when the source generator is hooked up
private Document? _generatedDocument; private Document? _generatedDocument;
@ -63,8 +62,10 @@ internal class RemoteDocumentSnapshot(TextDocument textDocument, RemoteProjectSn
{ {
var id = _textDocument.Id; var id = _textDocument.Id;
var newDocument = _textDocument.Project.Solution.WithAdditionalDocumentText(id, text).GetAdditionalDocument(id).AssumeNotNull(); var newDocument = _textDocument.Project.Solution.WithAdditionalDocumentText(id, text).GetAdditionalDocument(id).AssumeNotNull();
var project = newDocument.Project;
var projectSnapshot = _projectSnapshotFactory.GetOrCreate(project);
return new RemoteDocumentSnapshot(newDocument, _projectSnapshot, _filePathService); return new RemoteDocumentSnapshot(newDocument, projectSnapshot, _projectSnapshotFactory);
} }
public bool TryGetGeneratedOutput([NotNullWhen(true)] out RazorCodeDocument? result) public bool TryGetGeneratedOutput([NotNullWhen(true)] out RazorCodeDocument? result)