Skip to content

Commit f0a326f

Browse files
authored
Don't generate code for closed files that are part of fallback projects (#9503)
Fixes a regression from #9486
2 parents a92ccae + a44da2d commit f0a326f

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Razor.Language;
5+
6+
namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;
7+
8+
internal class FallbackHostProject : HostProject
9+
{
10+
public FallbackHostProject(string projectFilePath, string intermediateOutputPath, RazorConfiguration razorConfiguration, string rootNamespace, string displayName)
11+
: base(projectFilePath, intermediateOutputPath, razorConfiguration, rootNamespace, displayName)
12+
{
13+
}
14+
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/FallbackProjectManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ private void AddFallbackProject(ProjectId projectId, string filePath)
8282
}
8383

8484
var rootNamespace = project.DefaultNamespace ?? "ASP";
85-
var hostProject = new HostProject(project.FilePath, intermediateOutputPath, FallbackRazorConfiguration.Latest, rootNamespace, project.Name);
85+
86+
// We create this as a fallback project so that other parts of the system can reason about them - eg we don't do code
87+
// generation for closed files for documents in these projects. If these projects become "real", either because capabilities
88+
// change or simply a timing difference between Roslyn and our CPS components, the HostProject instance associated with
89+
// the project will be updated, and it will no longer be a fallback project.
90+
var hostProject = new FallbackHostProject(project.FilePath, intermediateOutputPath, FallbackRazorConfiguration.Latest, rootNamespace, project.Name);
8691

8792
_projectSnapshotManagerAccessor.Instance.ProjectAdded(hostProject);
8893

src/Razor/src/Microsoft.VisualStudio.Editor.Razor/BackgroundDocumentGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ public void Enqueue(IProjectSnapshot project, IDocumentSnapshot document)
134134
throw new ArgumentNullException(nameof(document));
135135
}
136136

137+
if (project is ProjectSnapshot { HostProject: FallbackHostProject })
138+
{
139+
// We don't support closed file code generation for fallback projects
140+
return;
141+
}
142+
137143
_dispatcher.AssertDispatcherThread();
138144

139145
lock (Work)

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/FallbackProjectManagerTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,34 @@ public void DynamicFileAdded_UnknownProject_Adds()
6767
var project = Assert.Single(_projectSnapshotManager.GetProjects());
6868
Assert.Equal("RootNamespace", project.RootNamespace);
6969

70+
Assert.IsType<FallbackHostProject>(((ProjectSnapshot)project).HostProject);
71+
7072
var documentFilePath = Assert.Single(project.DocumentFilePaths);
7173
Assert.Equal(SomeProjectFile1.FilePath, documentFilePath);
7274
}
7375

76+
[Fact]
77+
public void DynamicFileAdded_UnknownToKnownProject_NotFallbackHostProject()
78+
{
79+
var projectId = ProjectId.CreateNewId();
80+
var projectInfo = ProjectInfo.Create(projectId, VersionStamp.Default, "DisplayName", "AssemblyName", LanguageNames.CSharp, filePath: SomeProject.FilePath)
81+
.WithCompilationOutputInfo(new CompilationOutputInfo().WithAssemblyPath(Path.Combine(SomeProject.IntermediateOutputPath, "SomeProject.dll")))
82+
.WithDefaultNamespace("RootNamespace");
83+
84+
Workspace.TryApplyChanges(Workspace.CurrentSolution.AddProject(projectInfo));
85+
86+
_fallbackProjectManger.DynamicFileAdded(projectId, SomeProject.Key, SomeProject.FilePath, SomeProjectFile1.FilePath);
87+
88+
var project = Assert.Single(_projectSnapshotManager.GetProjects());
89+
Assert.IsType<FallbackHostProject>(((ProjectSnapshot)project).HostProject);
90+
91+
var hostProject = new HostProject(SomeProject.FilePath, SomeProject.IntermediateOutputPath, RazorConfiguration.Default, "RootNamespace", "DisplayName");
92+
_projectSnapshotManager.ProjectConfigurationChanged(hostProject);
93+
94+
project = Assert.Single(_projectSnapshotManager.GetProjects());
95+
Assert.IsNotType<FallbackHostProject>(((ProjectSnapshot)project).HostProject);
96+
}
97+
7498
[Fact]
7599
public void DynamicFileAdded_TrackedProject_AddsDocuments()
76100
{

0 commit comments

Comments
 (0)