From 4f22f5891ba3d9d6a8fcf91e6d9e8be239be86f0 Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Thu, 22 Feb 2024 13:23:07 +0100 Subject: [PATCH 1/4] Use merged Razor compiler DLL --- .../Targets/Microsoft.NET.Sdk.Razor.Configuration.targets | 4 ++-- src/RazorSdk/Tool/DiscoverCommand.cs | 4 ++-- .../ScopedCssIntegrationTests.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Configuration.targets b/src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Configuration.targets index 8f0fa59ba967..29f55239ee41 100644 --- a/src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Configuration.targets +++ b/src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Configuration.targets @@ -93,8 +93,8 @@ Copyright (c) .NET Foundation. All rights reserved. - Microsoft.CodeAnalysis.Razor.Compiler.Mvc - $(RazorSdkDirectoryRoot)tools\Microsoft.CodeAnalysis.Razor.Compiler.Mvc.dll + Microsoft.CodeAnalysis.Razor.Compiler + $(RazorSdkDirectoryRoot)tools\Microsoft.CodeAnalysis.Razor.Compiler.dll diff --git a/src/RazorSdk/Tool/DiscoverCommand.cs b/src/RazorSdk/Tool/DiscoverCommand.cs index fd67bdabc4bb..827d477b33f2 100644 --- a/src/RazorSdk/Tool/DiscoverCommand.cs +++ b/src/RazorSdk/Tool/DiscoverCommand.cs @@ -109,8 +109,8 @@ internal static void PatchExtensions(CommandOption extensionNames, CommandOption var extensionName = extensionNames.Values[i]; var replacementFileName = extensionName switch { - "MVC-1.0" or "MVC-1.1" => "Microsoft.CodeAnalysis.Razor.Compiler.Mvc.Version1_X.dll", - "MVC-2.0" or "MVC-2.1" => "Microsoft.CodeAnalysis.Razor.Compiler.Mvc.Version2_X.dll", + "MVC-1.0" or "MVC-1.1" => "Microsoft.CodeAnalysis.Razor.Compiler.dll", + "MVC-2.0" or "MVC-2.1" => "Microsoft.CodeAnalysis.Razor.Compiler.dll", _ => null, }; diff --git a/test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs b/test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs index 8469b0a9f7d5..b68fd5bb46d1 100644 --- a/test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs +++ b/test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs @@ -90,7 +90,7 @@ public void CanOverrideScopeIdentifiers() var scoped = Path.Combine(intermediateOutputPath, "scopedcss", "Styles", "Pages", "Counter.rz.scp.css"); new FileInfo(scoped).Should().Exist(); new FileInfo(scoped).Should().Contain("b-overriden"); - var generated = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs"); + var generated = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs"); new FileInfo(generated).Should().Exist(); new FileInfo(generated).Should().Contain("b-overriden"); new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist(); @@ -319,7 +319,7 @@ public void Build_RemovingScopedCssAndBuilding_UpdatesGeneratedCodeAndBundle() new FileInfo(generatedBundle).Should().Exist(); var generatedProjectBundle = Path.Combine(intermediateOutputPath, "scopedcss", "projectbundle", "ComponentApp.bundle.scp.css"); new FileInfo(generatedProjectBundle).Should().Exist(); - var generatedCounter = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs"); + var generatedCounter = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs"); new FileInfo(generatedCounter).Should().Exist(); var componentThumbprint = FileThumbPrint.Create(generatedCounter); From 8ac0be44a75dc2538ad5ac316d8041038984a04f Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Thu, 22 Feb 2024 13:34:39 +0100 Subject: [PATCH 2/4] Use testing Razor build --- NuGet.config | 1 + eng/Version.Details.xml | 16 ++++++++-------- eng/Versions.props | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/NuGet.config b/NuGet.config index 52f8c9735c20..f1178c13c56d 100644 --- a/NuGet.config +++ b/NuGet.config @@ -2,6 +2,7 @@ + diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index a96139e83ea6..c0dd10f9a609 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -316,22 +316,22 @@ f26b79eb34d13648bedc5ec98e2151f8c55db978 - + https://github.com/dotnet/razor - 67bd8253e308d06bf249a02911d06317d5d7b5b9 + 545e66651230b9bd112e6f2b9f8bdd19444f6fb9 - + https://github.com/dotnet/razor - 67bd8253e308d06bf249a02911d06317d5d7b5b9 + 545e66651230b9bd112e6f2b9f8bdd19444f6fb9 - + https://github.com/dotnet/razor - 67bd8253e308d06bf249a02911d06317d5d7b5b9 + 545e66651230b9bd112e6f2b9f8bdd19444f6fb9 - + https://github.com/dotnet/razor - 67bd8253e308d06bf249a02911d06317d5d7b5b9 + 545e66651230b9bd112e6f2b9f8bdd19444f6fb9 diff --git a/eng/Versions.props b/eng/Versions.props index daa28aae8c5a..9c85b8607b8d 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -160,9 +160,9 @@ - 7.0.0-preview.24120.8 - 7.0.0-preview.24120.8 - 7.0.0-preview.24120.8 + 7.0.0-preview.24122.2 + 7.0.0-preview.24122.2 + 7.0.0-preview.24122.2 From 84cb0c8d80d5716130818150d55eb7416c4d605b Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Thu, 22 Feb 2024 16:24:23 +0100 Subject: [PATCH 3/4] Exempt ObjectPool dependency of the Razor compiler --- src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs b/src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs index d47f0cb0e7ab..774c3caadc58 100644 --- a/src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs +++ b/src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs @@ -18,6 +18,7 @@ internal class DefaultExtensionDependencyChecker : ExtensionDependencyChecker "System", "Microsoft.CodeAnalysis", "Microsoft.AspNetCore.Razor", + "Microsoft.Extensions.ObjectPool" }; private readonly ExtensionAssemblyLoader _loader; From d14a778899b7acf6a55fa365416e87cb43462152 Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Fri, 23 Feb 2024 12:14:01 +0100 Subject: [PATCH 4/4] Fix extension loading --- src/RazorSdk/Tool/DiscoverCommand.cs | 25 +++++++++++++------------ src/RazorSdk/Tool/GenerateCommand.cs | 12 +++++++++++- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/RazorSdk/Tool/DiscoverCommand.cs b/src/RazorSdk/Tool/DiscoverCommand.cs index 827d477b33f2..41b6248bec5f 100644 --- a/src/RazorSdk/Tool/DiscoverCommand.cs +++ b/src/RazorSdk/Tool/DiscoverCommand.cs @@ -94,8 +94,13 @@ protected override bool ValidateArguments() return true; } + private const string RazorCompilerFileName = "Microsoft.CodeAnalysis.Razor.Compiler.dll"; + + internal static string GetRazorCompilerPath() + => Path.Combine(Path.GetDirectoryName(typeof(Application).Assembly.Location), RazorCompilerFileName); + /// - /// Replaces the assembly for MVC extension v1 or v2 with the one shipped alongside SDK (as opposed to the one from NuGet). + /// Replaces the assembly for MVC extension with the one shipped alongside SDK (as opposed to the one from NuGet). /// /// /// Needed so the Razor compiler can change its APIs without breaking legacy MVC scenarios. @@ -107,32 +112,28 @@ internal static void PatchExtensions(CommandOption extensionNames, CommandOption for (int i = 0; i < extensionNames.Values.Count; i++) { var extensionName = extensionNames.Values[i]; - var replacementFileName = extensionName switch + + string expectedOriginalPath = extensionName switch { - "MVC-1.0" or "MVC-1.1" => "Microsoft.CodeAnalysis.Razor.Compiler.dll", - "MVC-2.0" or "MVC-2.1" => "Microsoft.CodeAnalysis.Razor.Compiler.dll", + "MVC-1.0" or "MVC-1.1" or "MVC-2.0" or "MVC-2.1" => "Microsoft.AspNetCore.Mvc.Razor.Extensions", + "MVC-3.0" => "Microsoft.CodeAnalysis.Razor.Compiler", _ => null, }; - if (replacementFileName != null) + if (expectedOriginalPath is not null) { var extensionFilePath = extensionFilePaths.Values[i]; - if (!HasExpectedFileName(extensionFilePath)) + if (!string.Equals(expectedOriginalPath, Path.GetFileNameWithoutExtension(extensionFilePath), StringComparison.OrdinalIgnoreCase)) { error.WriteLine($"Extension '{extensionName}' has unexpected path '{extensionFilePath}'."); } else { currentDirectory ??= Path.GetDirectoryName(typeof(Application).Assembly.Location); - extensionFilePaths.Values[i] = Path.Combine(currentDirectory, replacementFileName); + extensionFilePaths.Values[i] = Path.Combine(currentDirectory, RazorCompilerFileName); } } } - - static bool HasExpectedFileName(string filePath) - { - return "Microsoft.AspNetCore.Mvc.Razor.Extensions".Equals(Path.GetFileNameWithoutExtension(filePath), StringComparison.OrdinalIgnoreCase); - } } protected override Task ExecuteCoreAsync() diff --git a/src/RazorSdk/Tool/GenerateCommand.cs b/src/RazorSdk/Tool/GenerateCommand.cs index f3399f5d9d29..d90280ee6e0b 100644 --- a/src/RazorSdk/Tool/GenerateCommand.cs +++ b/src/RazorSdk/Tool/GenerateCommand.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Reflection; using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Razor; @@ -77,9 +78,18 @@ protected override Task ExecuteCoreAsync() // Loading all of the extensions should succeed as the dependency checker will have already // loaded them. var extensions = new RazorExtension[ExtensionNames.Values.Count]; + string razorCompilerPath = null; for (var i = 0; i < ExtensionNames.Values.Count; i++) { - extensions[i] = new AssemblyExtension(ExtensionNames.Values[i], Parent.Loader.LoadFromPath(ExtensionFilePaths.Values[i])); + // If the extension is the Razor compiler, we'll use the referenced assembly (instead of the SDK one). + // Otherwise the extension's ProvideRazorExtensionInitializerAttribute would be different from the AssemblyExtension's one, + // hence the extension would not be loaded properly. + razorCompilerPath ??= DiscoverCommand.GetRazorCompilerPath(); + var assembly = string.Equals(ExtensionFilePaths.Values[i], razorCompilerPath, StringComparison.OrdinalIgnoreCase) + ? typeof(AssemblyExtension).Assembly + : Parent.Loader.LoadFromPath(ExtensionFilePaths.Values[i]); + + extensions[i] = new AssemblyExtension(ExtensionNames.Values[i], assembly); } var version = RazorLanguageVersion.Parse(Version.Value());