Use new PreserveCompilationReferences in Razor Sdk (dotnet/aspnetcore-tooling#187)

* Use new PreserveCompilationReferences in Razor Sdk

Fixes https://github.com/aspnet/AspNetCore/issues/6512
\n\nCommit migrated from f58c436961
This commit is contained in:
Pranav K 2019-02-11 19:54:04 -08:00 committed by GitHub
parent abd5ab8080
commit 8b61ae7196
13 changed files with 80 additions and 34 deletions

View File

@ -42,14 +42,6 @@ Copyright (c) .NET Foundation. All rights reserved.
-->
<CopyRazorGenerateFilesToPublishDirectory Condition="'$(CopyRazorGenerateFilesToPublishDirectory)'==''">false</CopyRazorGenerateFilesToPublishDirectory>
<!--
Set to true to copy reference assembly items to the publish directory.
Typically reference assemblies are not needed for a published application if Razor compilation occurs at build-time
or publish-time. By default, the Razor SDK will suppress the copying of reference assemblies to the publish directory.
-->
<CopyRefAssembliesToPublishDirectory Condition="'$(CopyRefAssembliesToPublishDirectory)'==''">false</CopyRefAssembliesToPublishDirectory>
<!--
Determines the toolset to use to compile Razor (.cshtml) files. Defaults to 'Implicit' to let the Razor Sdk determine the toolset to use.
Valid values include 'Implicit', 'RazorSdk', and 'PrecompilationTool'.
@ -68,7 +60,11 @@ Copyright (c) .NET Foundation. All rights reserved.
-->
<RazorGenerateOutputFileExtension>.g.cs</RazorGenerateOutputFileExtension>
<!-- Determines if the deps file includes complication context -->
<PreserveCompilationContext>true</PreserveCompilationContext>
<!-- Determines if the refs folder is produced as part of build \ publish -->
<PreserveCompilationReferences>false</PreserveCompilationReferences>
</PropertyGroup>
<ItemGroup Condition="'$(EnableDefaultItems)' == 'true' And '$(EnableDefaultContentItems)' == 'true'">

View File

@ -174,8 +174,14 @@ Copyright (c) .NET Foundation. All rights reserved.
<CopyRazorGenerateFilesToPublishDirectory Condition="'$(MvcRazorExcludeViewFilesFromPublish)'=='true'">false</CopyRazorGenerateFilesToPublishDirectory>
<CopyRazorGenerateFilesToPublishDirectory Condition="'$(MvcRazorExcludeViewFilesFromPublish)'=='false'">true</CopyRazorGenerateFilesToPublishDirectory>
<CopyRefAssembliesToPublishDirectory Condition="'$(MvcRazorExcludeRefAssembliesFromPublish)'=='true'">false</CopyRefAssembliesToPublishDirectory>
<CopyRefAssembliesToPublishDirectory Condition="'$(MvcRazorExcludeRefAssembliesFromPublish)'=='false'">true</CopyRefAssembliesToPublishDirectory>
<!--
In 2.x, setting CopyRefAssembliesToPublishDirectory controlled copying of the refs directory to the publish directory.
If explicitly specified, use it to override PreserveCompilationReferences.
-->
<PreserveCompilationReferences Condition="'$(CopyRefAssembliesToPublishDirectory)'!=''">$(CopyRefAssembliesToPublishDirectory)</PreserveCompilationReferences>
<PreserveCompilationReferences Condition="'$(MvcRazorExcludeRefAssembliesFromPublish)'=='true'">false</PreserveCompilationReferences>
<PreserveCompilationReferences Condition="'$(MvcRazorExcludeRefAssembliesFromPublish)'=='false'">true</PreserveCompilationReferences>
<!--
We can't set the actual default value here due to evaluation order (depends on $(OutDir)).
@ -228,6 +234,16 @@ Copyright (c) .NET Foundation. All rights reserved.
<MvcRazorCompileOnPublish Condition="'$(MvcRazorCompileOnPublish)' == ''">true</MvcRazorCompileOnPublish>
</PropertyGroup>
<!-- Back-compat for PrecompilationTool -->
<PropertyGroup>
<!--
For 2.x desktop targeting projects using MvcPrecompilation, ref assemblies are required to compile the PrecompiledViews.dll,
but are removed by the tool once done. Set PreserveCompilationReferences = true, ignoring any value configured in the project,
if the project is using the precompilation tool.
-->
<PreserveCompilationReferences Condition="'$(ResolvedRazorCompileToolset)'=='PrecompilationTool'">true</PreserveCompilationReferences>
</PropertyGroup>
<!--
Properties that configure Razor SDK, but need to be defined in targets due to evaluation order.
-->
@ -711,21 +727,6 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>
<Target
Name="_RazorRemoveRefAssembliesFromPublish"
AfterTargets="ComputeRefAssembliesToPublish"
Condition="'$(ResolvedRazorCompileToolset)'=='RazorSdk' and '$(RazorCompileOnPublish)'=='true' and '$(CopyRefAssembliesToPublishDirectory)'=='false'">
<!--
The ref assemblies are published whenever PreserveCompilationContext is true, which we expect to be true for
most usages of Razor. There's no setting that excludes just the ref assemblies, so we do it ourselves.
-->
<ItemGroup>
<ResolvedFileToPublish
Remove="%(ResolvedFileToPublish.Identity)"
Condition="'%(ResolvedFileToPublish.RelativePath)'=='$(RefAssembliesFolderName)\%(Filename)%(Extension)'"/>
</ItemGroup>
</Target>
<Target Name="_CheckForMissingRazorCompiler" Condition="'$(IsRazorCompilerReferenced)' != 'true'">
<Error
Text="A PackageReference for 'Microsoft.AspNetCore.Razor.Design' was not included in your project. This package is required to compile Razor files. Typically, a

View File

@ -235,7 +235,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_WithViews_ProducesDepsFileWithCompilationContext()
public async Task Build_WithViews_ProducesDepsFileWithCompilationContext_ButNoReferences()
{
var customDefine = "RazorSdkTest";
var result = await DotnetMSBuild("Build", $"/p:DefineConstants={customDefine}");
@ -247,13 +247,16 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
var dependencyContext = ReadDependencyContext(depsFilePath);
// Pick a couple of libraries and ensure they have some compile references
var packageReference = dependencyContext.CompileLibraries.First(l => l.Name == "Microsoft.NETCore.App");
var packageReference = dependencyContext.CompileLibraries.First(l => l.Name == "System.Diagnostics.DiagnosticSource");
Assert.NotEmpty(packageReference.Assemblies);
var projectReference = dependencyContext.CompileLibraries.First(l => l.Name == "SimpleMvc");
Assert.NotEmpty(projectReference.Assemblies);
Assert.Contains(customDefine, dependencyContext.CompilationOptions.Defines);
// Verify no refs folder is produced
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");
}
[Fact]

View File

@ -37,5 +37,25 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Path.Combine(IntermediateOutputPath, "SimpleMvc21.TagHelpers.output.cache"),
@"""Name"":""SimpleMvc.SimpleTagHelper""");
}
[Fact]
[InitializeTestProject("SimpleMvc21")]
public async Task Publish_NETCoreApp21TargetingProject()
{
TargetFramework = "netcoreapp2.1";
var result = await DotnetMSBuild("Publish");
Assert.BuildPassed(result);
Assert.FileExists(result, PublishOutputPath, "SimpleMvc21.dll");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc21.pdb");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc21.Views.dll");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc21.Views.pdb");
// By default refs and .cshtml files will not be copied on publish
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
}
}
}

View File

@ -247,6 +247,30 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileCountEquals(result, 8, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Publish_WithCopySettingsInProjectFile_CopiesFiles()
{
AddProjectFileContent(@"
<PropertyGroup>
<CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>");
var result = await DotnetMSBuild("Publish");
Assert.BuildPassed(result);
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.dll");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.pdb");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.dll");
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.pdb");
// By default refs and .cshtml files will not be copied on publish
Assert.FileExists(result, PublishOutputPath, "refs", "mscorlib.dll");
Assert.FileCountEquals(result, 8, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
}
[Fact] // Tests old MvcPrecompilation behavior that we support for compat.
[InitializeTestProject("SimpleMvc")]
public async Task Publish_MvcRazorExcludeFilesFromPublish_False_CopiesFiles()

View File

@ -4,7 +4,7 @@
<!-- To generate baselines, run tests with /p:GenerateBaselines=true -->
<DefineConstants Condition="'$(GenerateBaselines)'=='true'">$(DefineConstants);GENERATE_BASELINES</DefineConstants>
<DefineConstants>$(DefineConstants);__RemoveThisBitTo__GENERATE_BASELINES</DefineConstants>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>

View File

@ -32,6 +32,7 @@ namespace Microsoft.CodeAnalysis
private static IEnumerable<string> ResolvePaths(CompilationLibrary library)
{
#if NETFRAMEWORK
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (var i = 0; i < assemblies.Length; i++)
{
@ -40,6 +41,7 @@ namespace Microsoft.CodeAnalysis
return new[] { assemblies[i].Location };
}
}
#endif
try
{

View File

@ -17,7 +17,7 @@
<!-- Test Placeholder -->
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
<ProjectReference Include="..\ClassLibrary\ClassLibrary.csproj"/>
</ItemGroup>

View File

@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'==''">

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'==''">

View File

@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'==''">

View File

@ -20,7 +20,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'==''">

View File

@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" IsImplicitlyDefined="true" />
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'==''">