Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use merged Razor compiler DLL #38943

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration>
<packageSources>
<clear />
<add key="general-testing" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/general-testing/nuget/v3/index.json" />
<!--Begin: Package sources managed by Dependency Flow automation. Do not edit the sources below.-->
<!-- Begin: Package sources from dotnet-aspnetcore -->
<!-- End: Package sources from dotnet-aspnetcore -->
Expand Down
16 changes: 8 additions & 8 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,22 @@
<Sha>f26b79eb34d13648bedc5ec98e2151f8c55db978</Sha>
<SourceBuild RepoName="aspnetcore" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Razor.Tooling.Internal" Version="7.0.0-preview.24120.8">
<Dependency Name="Microsoft.CodeAnalysis.Razor.Tooling.Internal" Version="7.0.0-preview.24122.2">
<Uri>https://github.com/dotnet/razor</Uri>
<Sha>67bd8253e308d06bf249a02911d06317d5d7b5b9</Sha>
<Sha>545e66651230b9bd112e6f2b9f8bdd19444f6fb9</Sha>
</Dependency>
<Dependency Name="Microsoft.AspNetCore.Mvc.Razor.Extensions.Tooling.Internal" Version="7.0.0-preview.24120.8">
<Dependency Name="Microsoft.AspNetCore.Mvc.Razor.Extensions.Tooling.Internal" Version="7.0.0-preview.24122.2">
<Uri>https://github.com/dotnet/razor</Uri>
<Sha>67bd8253e308d06bf249a02911d06317d5d7b5b9</Sha>
<Sha>545e66651230b9bd112e6f2b9f8bdd19444f6fb9</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.Sdk.Razor.SourceGenerators.Transport" Version="7.0.0-preview.24120.8">
<Dependency Name="Microsoft.NET.Sdk.Razor.SourceGenerators.Transport" Version="7.0.0-preview.24122.2">
<Uri>https://github.com/dotnet/razor</Uri>
<Sha>67bd8253e308d06bf249a02911d06317d5d7b5b9</Sha>
<Sha>545e66651230b9bd112e6f2b9f8bdd19444f6fb9</Sha>
</Dependency>
<!-- Intermediate is necessary for source build. -->
<Dependency Name="Microsoft.SourceBuild.Intermediate.razor" Version="7.0.0-preview.24120.8">
<Dependency Name="Microsoft.SourceBuild.Intermediate.razor" Version="7.0.0-preview.24122.2">
<Uri>https://github.com/dotnet/razor</Uri>
<Sha>67bd8253e308d06bf249a02911d06317d5d7b5b9</Sha>
<Sha>545e66651230b9bd112e6f2b9f8bdd19444f6fb9</Sha>
<SourceBuild RepoName="razor" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.Extensions.FileProviders.Embedded" Version="9.0.0-preview.2.24120.6">
Expand Down
6 changes: 3 additions & 3 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@
</PropertyGroup>
<!-- Dependencies from https://github.com/dotnet/razor -->
<PropertyGroup>
<MicrosoftAspNetCoreMvcRazorExtensionsToolingInternalPackageVersion>7.0.0-preview.24120.8</MicrosoftAspNetCoreMvcRazorExtensionsToolingInternalPackageVersion>
<MicrosoftCodeAnalysisRazorToolingInternalVersion>7.0.0-preview.24120.8</MicrosoftCodeAnalysisRazorToolingInternalVersion>
<MicrosoftNETSdkRazorSourceGeneratorsTransportPackageVersion>7.0.0-preview.24120.8</MicrosoftNETSdkRazorSourceGeneratorsTransportPackageVersion>
<MicrosoftAspNetCoreMvcRazorExtensionsToolingInternalPackageVersion>7.0.0-preview.24122.2</MicrosoftAspNetCoreMvcRazorExtensionsToolingInternalPackageVersion>
<MicrosoftCodeAnalysisRazorToolingInternalVersion>7.0.0-preview.24122.2</MicrosoftCodeAnalysisRazorToolingInternalVersion>
<MicrosoftNETSdkRazorSourceGeneratorsTransportPackageVersion>7.0.0-preview.24122.2</MicrosoftNETSdkRazorSourceGeneratorsTransportPackageVersion>
</PropertyGroup>
<PropertyGroup>
<!-- Dependencies from https://github.com/dotnet/wpf -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ Copyright (c) .NET Foundation. All rights reserved.

<ItemGroup>
<RazorExtension Include="MVC-3.0">
<AssemblyName>Microsoft.CodeAnalysis.Razor.Compiler.Mvc</AssemblyName>
<AssemblyFilePath>$(RazorSdkDirectoryRoot)tools\Microsoft.CodeAnalysis.Razor.Compiler.Mvc.dll</AssemblyFilePath>
<AssemblyName>Microsoft.CodeAnalysis.Razor.Compiler</AssemblyName>
<AssemblyFilePath>$(RazorSdkDirectoryRoot)tools\Microsoft.CodeAnalysis.Razor.Compiler.dll</AssemblyFilePath>
</RazorExtension>
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/RazorSdk/Tool/DefaultExtensionDependencyChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class DefaultExtensionDependencyChecker : ExtensionDependencyChecker
"System",
"Microsoft.CodeAnalysis",
"Microsoft.AspNetCore.Razor",
"Microsoft.Extensions.ObjectPool"
};

private readonly ExtensionAssemblyLoader _loader;
Expand Down
25 changes: 13 additions & 12 deletions src/RazorSdk/Tool/DiscoverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// 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).
/// </summary>
/// <remarks>
/// Needed so the Razor compiler can change its APIs without breaking legacy MVC scenarios.
Expand All @@ -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.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" 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<int> ExecuteCoreAsync()
Expand Down
12 changes: 11 additions & 1 deletion src/RazorSdk/Tool/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,9 +78,18 @@ protected override Task<int> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down