Skip to content

Commit

Permalink
Make sure we use the right project in the updated project snapshot
Browse files Browse the repository at this point in the history
This seems like the cause of #10865
  • Loading branch information
davidwengier committed Sep 16, 2024
1 parent f929020 commit 1986043
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
using System;
using System.Composition;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis.Razor.Workspaces;

namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;

[Export(typeof(DocumentSnapshotFactory)), Shared]
[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 readonly Lazy<ProjectSnapshotFactory> _projectSnapshotFactory = projectSnapshotFactory;
private readonly IFilePathService _filePathService = filePathService;

public RemoteDocumentSnapshot GetOrCreate(TextDocument textDocument)
{
lock (s_documentSnapshots)
{
if (!s_documentSnapshots.TryGetValue(textDocument, out var documentSnapshot))
{
var projectSnapshot = _projectSnapshotFactory.Value.GetOrCreate(textDocument.Project);
documentSnapshot = new RemoteDocumentSnapshot(textDocument, projectSnapshot, _filePathService);
var projectSnapshotFactory = _projectSnapshotFactory.Value;
var projectSnapshot = projectSnapshotFactory.GetOrCreate(textDocument.Project);
documentSnapshot = new RemoteDocumentSnapshot(textDocument, projectSnapshot, projectSnapshotFactory);
s_documentSnapshots.Add(textDocument, documentSnapshot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;

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 RemoteProjectSnapshot _projectSnapshot = projectSnapshot;
private readonly IFilePathService _filePathService = filePathService;
private readonly ProjectSnapshotFactory _projectSnapshotFactory = projectSnapshotFactory;

// TODO: Delete this field when the source generator is hooked up
private Document? _generatedDocument;
Expand Down Expand Up @@ -63,8 +62,10 @@ public IDocumentSnapshot WithText(SourceText text)
{
var id = _textDocument.Id;
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)
Expand Down

0 comments on commit 1986043

Please sign in to comment.