Skip to content

Commit d37d410

Browse files
committed
Fix IR and HTML outputs with latest Razor versions
1 parent fce0753 commit d37d410

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

src/Compiler/Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private CompiledAssembly CompileNoCache(
210210
Type = "ir",
211211
Label = "IR",
212212
Language = "csharp",
213-
EagerText = codeDocument.Map(d => d?.GetDocumentIntermediateNode().Serialize() ?? "").Serialize(),
213+
EagerText = codeDocument.Map(d => d?.GetDocumentIntermediateNodeSafe().Serialize() ?? "").Serialize(),
214214
},
215215
.. string.IsNullOrEmpty(razorDiagnostics)
216216
? ImmutableArray<CompiledFileOutput>.Empty
@@ -237,7 +237,7 @@ .. string.IsNullOrEmpty(razorDiagnostics)
237237
Language = "html",
238238
LazyText = new(() =>
239239
{
240-
var document = codeDocument.Unwrap()?.GetDocumentIntermediateNode()
240+
var document = codeDocument.Unwrap()?.GetDocumentIntermediateNodeSafe()
241241
?? throw new InvalidOperationException("No IR available.");
242242

243243
if (document.DocumentKind.StartsWith("mvc"))

src/Compiler/Utils.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Razor.Language;
2+
using Microsoft.AspNetCore.Razor.Language.Intermediate;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.Text;
@@ -140,15 +141,7 @@ static string toBase36(ReadOnlySpan<byte> hash)
140141

141142
public static RazorCSharpDocument GetCSharpDocumentSafe(this RazorCodeDocument document)
142143
{
143-
// GetCSharpDocument extension method has been turned into an instance method in https://github.com/dotnet/razor/pull/11939.
144-
if (document.GetType().GetMethod("GetCSharpDocument", BindingFlags.Instance | BindingFlags.NonPublic) is { } method)
145-
{
146-
return (RazorCSharpDocument)method.Invoke(document, [])!;
147-
}
148-
149-
return (RazorCSharpDocument)typeof(RazorCodeDocument).Assembly.GetType("Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions")!
150-
.GetMethod("GetCSharpDocument", BindingFlags.Static | BindingFlags.Public)!
151-
.Invoke(null, [document])!;
144+
return document.GetDocumentDataSafe<RazorCSharpDocument>("GetCSharpDocument");
152145
}
153146

154147
public static IReadOnlyList<RazorDiagnostic> GetDiagnostics(this RazorCSharpDocument document)
@@ -160,6 +153,24 @@ public static IReadOnlyList<RazorDiagnostic> GetDiagnostics(this RazorCSharpDocu
160153
.GetValue(document)!;
161154
}
162155

156+
private static T GetDocumentDataSafe<T>(this RazorCodeDocument document, string methodName, string? instanceMethodName = null)
157+
{
158+
// GetCSharpDocument and similar extension methods have been turned into instance methods in https://github.com/dotnet/razor/pull/11939.
159+
if (document.GetType().GetMethod(instanceMethodName ?? methodName, BindingFlags.Instance | BindingFlags.NonPublic) is { } method)
160+
{
161+
return (T)method.Invoke(document, [])!;
162+
}
163+
164+
return (T)typeof(RazorCodeDocument).Assembly.GetType("Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions")!
165+
.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public)!
166+
.Invoke(null, [document])!;
167+
}
168+
169+
public static DocumentIntermediateNode GetDocumentIntermediateNodeSafe(this RazorCodeDocument document)
170+
{
171+
return document.GetDocumentDataSafe<DocumentIntermediateNode>("GetDocumentIntermediateNode", "GetDocumentNode");
172+
}
173+
163174
public static string GetGeneratedCode(this RazorCSharpDocument document)
164175
{
165176
// There can be either `string GeneratedCode` or `SourceText Text` property.
@@ -177,15 +188,7 @@ public static string GetGeneratedCode(this RazorCSharpDocument document)
177188

178189
public static RazorSyntaxTree GetSyntaxTreeSafe(this RazorCodeDocument document)
179190
{
180-
// GetSyntaxTree extension method has been turned into an instance method in https://github.com/dotnet/razor/pull/11939.
181-
if (document.GetType().GetMethod("GetSyntaxTree", BindingFlags.Instance | BindingFlags.NonPublic) is { } method)
182-
{
183-
return (RazorSyntaxTree)method.Invoke(document, [])!;
184-
}
185-
186-
return (RazorSyntaxTree)typeof(RazorCodeDocument).Assembly.GetType("Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions")!
187-
.GetMethod("GetSyntaxTree", BindingFlags.Static | BindingFlags.Public)!
188-
.Invoke(null, [document])!;
191+
return document.GetDocumentDataSafe<RazorSyntaxTree>("GetSyntaxTree");
189192
}
190193

191194
public static IEnumerable<RazorProjectItem> EnumerateItemsSafe(this RazorProjectFileSystem fileSystem, string basePath)
@@ -241,6 +244,15 @@ .. var rest
241244
.Invoke(engine, [projectItem, .. Enumerable.Repeat<object?>(null, method.GetParameters().Length - 1)])!;
242245
}
243246

247+
public static string Serialize(this IntermediateNode node)
248+
{
249+
// IntermediateNode.DebuggerToString renamed to GetDebuggerDisplay in https://github.com/dotnet/razor/pull/11931.
250+
var type = node.GetType();
251+
var method = type.GetMethod("DebuggerToString", BindingFlags.Instance | BindingFlags.NonPublic)
252+
?? type.GetMethod("GetDebuggerDisplay", BindingFlags.Instance | BindingFlags.NonPublic);
253+
return (string)method!.Invoke(node, [])!;
254+
}
255+
244256
public static void SetCSharpLanguageVersionSafe(this RazorProjectEngineBuilder builder, LanguageVersion languageVersion)
245257
{
246258
// Changed in https://github.com/dotnet/razor/commit/40384334fd4c20180c25b3c88a82d3ca5da07487.

src/RazorAccess/RazorAccessors.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.AspNetCore.Razor.Language;
2-
using Microsoft.AspNetCore.Razor.Language.Intermediate;
32
using Microsoft.CodeAnalysis;
43
using Microsoft.NET.Sdk.Razor.SourceGenerators;
54
using System.Runtime.CompilerServices;
@@ -39,13 +38,6 @@ private static object GetFileKindFromPath(string filePath)
3938
return FileKinds.GetFileKindFromPath(filePath);
4039
}
4140

42-
public static string Serialize(this DocumentIntermediateNode node)
43-
{
44-
var formatter = new DebuggerDisplayFormatter();
45-
formatter.FormatTree(node);
46-
return formatter.ToString();
47-
}
48-
4941
public static string Serialize(this RazorSyntaxTree tree)
5042
{
5143
// SerializedValue property has been moved to another type in https://github.com/dotnet/razor/pull/11859.

test/UnitTests/CompilerProxyTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,34 @@ await services.GetRequiredService<CompilerDependencyProvider>()
7878

7979
var diagnosticsText = compiled.GetRequiredGlobalOutput(CompiledAssembly.DiagnosticsOutputType).EagerText;
8080
Assert.NotNull(diagnosticsText);
81+
output.WriteLine("Diagnostics:");
8182
output.WriteLine(diagnosticsText);
83+
output.WriteLine(string.Empty);
8284
Assert.Empty(diagnosticsText);
8385

8486
var cSharpText = await compiled.GetRequiredGlobalOutput("cs").GetTextAsync(outputFactory: null);
87+
output.WriteLine("C#:");
8588
output.WriteLine(cSharpText);
89+
output.WriteLine(string.Empty);
8690
Assert.Contains("class TestComponent", cSharpText);
8791

8892
var syntaxText = await compiled.Files.First().Value.GetOutput("syntax")!.GetTextAsync(outputFactory: null);
93+
output.WriteLine("Syntax:");
8994
output.WriteLine(syntaxText);
95+
output.WriteLine(string.Empty);
9096
Assert.Contains("RazorDocument", syntaxText);
97+
98+
var irText = await compiled.Files.First().Value.GetOutput("ir")!.GetTextAsync(outputFactory: null);
99+
output.WriteLine("IR:");
100+
output.WriteLine(irText);
101+
output.WriteLine(string.Empty);
102+
Assert.Contains("""component.1.0""", irText);
103+
104+
var htmlText = await compiled.Files.First().Value.GetOutput("html")!.GetTextAsync(outputFactory: null);
105+
output.WriteLine("HTML:");
106+
output.WriteLine(htmlText);
107+
output.WriteLine(string.Empty);
108+
Assert.Contains("test", htmlText);
91109
}
92110

93111
[Theory]

0 commit comments

Comments
 (0)