diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index 21c98339ae..4055cd936f 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -245,6 +245,11 @@ internal partial class DocumentState foreach (var item in importItems) { + if (item is NotFoundProjectItem) + { + continue; + } + if (item.PhysicalPath is null) { // This is a default import. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs index cf73aad118..1ddc2e227f 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Linq; @@ -48,6 +49,13 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase bool allowDiagnostics = false, RazorLSPOptions? razorLSPOptions = null, bool inGlobalNamespace = false) + { + // Run with and without forceRuntimeCodeGeneration + await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, true); + await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, false); + } + + private async Task RunFormattingTestAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration) { // Arrange fileKind ??= FileKinds.Component; @@ -62,7 +70,7 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase var path = "file:///path/to/Document." + fileKind; var uri = new Uri(path); - var (codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(source, uri.AbsolutePath, tagHelpers, fileKind, allowDiagnostics, inGlobalNamespace); + var (codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(source, uri.AbsolutePath, tagHelpers, fileKind, allowDiagnostics, inGlobalNamespace, forceRuntimeCodeGeneration); var options = new FormattingOptions() { TabSize = tabSize, @@ -217,7 +225,7 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase return source.WithChanges(changes); } - private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = default, bool allowDiagnostics = false, bool inGlobalNamespace = false) + private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = default, bool allowDiagnostics = false, bool inGlobalNamespace = false, bool forceRuntimeCodeGeneration = false) { fileKind ??= FileKinds.Component; tagHelpers = tagHelpers.NullToEmpty(); @@ -255,12 +263,21 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase .Setup(d => d.TargetPath) .Returns(importsPath); - var projectEngine = RazorProjectEngine.Create(builder => - { - builder.SetRootNamespace(inGlobalNamespace ? string.Empty : "Test"); - builder.Features.Add(new DefaultTypeNameFeature()); - RazorExtensions.Register(builder); - }); + var projectFileSystem = new TestRazorProjectFileSystem([ + new TestRazorProjectItem(path, fileKind: fileKind), + new TestRazorProjectItem(importsPath, fileKind: FileKinds.ComponentImport), + ]); + + var projectEngine = RazorProjectEngine.Create( + new RazorConfiguration(RazorLanguageVersion.Latest, "TestConfiguration", ImmutableArray.Empty, new LanguageServerFlags(forceRuntimeCodeGeneration)), + projectFileSystem, + builder => + { + builder.SetRootNamespace(inGlobalNamespace ? string.Empty : "Test"); + builder.Features.Add(new DefaultTypeNameFeature()); + RazorExtensions.Register(builder); + }); + var codeDocument = projectEngine.ProcessDesignTime(sourceDocument, fileKind, ImmutableArray.Create(importsDocument), tagHelpers); if (!allowDiagnostics) @@ -290,9 +307,18 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase documentSnapshot .Setup(d => d.TargetPath) .Returns(path); + documentSnapshot + .Setup(d => d.Project.Configuration) + .Returns(projectEngine.Configuration); + documentSnapshot + .Setup(d => d.GetTextAsync()) + .ReturnsAsync(codeDocument.Source.Text); documentSnapshot .Setup(d => d.Project.GetTagHelpersAsync(It.IsAny())) .Returns(new ValueTask>(tagHelpers)); + documentSnapshot + .Setup(d => d.Project.GetProjectEngine()) + .Returns(projectEngine); documentSnapshot .Setup(d => d.FileKind) .Returns(fileKind);