Ensure PreserveCompilationContext is set if we're targeting precompilation tool

PreserveCompilationContext must be set for precompilation tool to work, even when
the app has no views.

Fixes #2168
This commit is contained in:
Pranav K 2018-03-15 11:13:50 -07:00
parent 5f69a01cc5
commit b74582e101
2 changed files with 33 additions and 2 deletions

View File

@ -166,6 +166,13 @@ Copyright (c) .NET Foundation. All rights reserved.
<MvcRazorCompileOnPublish Condition="'$(MvcRazorCompileOnPublish)' == ''">true</MvcRazorCompileOnPublish> <MvcRazorCompileOnPublish Condition="'$(MvcRazorCompileOnPublish)' == ''">true</MvcRazorCompileOnPublish>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<!-- Set PreserveCompilationContext = true, if RazorCompileToolSet = PrecompilationTool since the precompilation tool requires it to work correctly.
This is an important to maintain back-compat when the Sdk is used to compile 2.0 applications.
-->
<PreserveCompilationContext Condition="'$(PreserveCompilationContext)'=='' AND '$(ResolvedRazorCompileToolset)' == 'PrecompilationTool'">true</PreserveCompilationContext>
</PropertyGroup>
<!-- <!--
Properties that configure Razor SDK, but need to be defined in targets due to evaluation order. Properties that configure Razor SDK, but need to be defined in targets due to evaluation order.
--> -->

View File

@ -257,7 +257,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[Fact] [Fact]
[InitializeTestProject("SimpleMvc")] [InitializeTestProject("SimpleMvc")]
public async Task Build_WithoutViews_ProducesDepsFileWithotCompiilationContext() public async Task Build_WithoutViews_ProducesDepsFileWithoutCompiilationContext()
{ {
Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true); Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true);
var customDefine = "RazorSdkTest"; var customDefine = "RazorSdkTest";
@ -272,9 +272,33 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.Empty(dependencyContext.CompilationOptions.Defines); Assert.Empty(dependencyContext.CompilationOptions.Defines);
} }
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_WithoutViews_ProducesDepsFileWithCompiilationContext_WhenUsingPrecompilationTool()
{
Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true);
var customDefine = "RazorSdkTest";
var result = await DotnetMSBuild("Build", $"/p:DefineConstants={customDefine} /p:MvcRazorCompileOnPublish=true");
Assert.BuildPassed(result);
Assert.FileExists(result, OutputPath, "SimpleMvc.deps.json");
var depsFilePath = Path.Combine(Project.DirectoryPath, OutputPath, "SimpleMvc.deps.json");
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.AspNetCore.Html.Abstractions");
Assert.NotEmpty(packageReference.Assemblies);
var projectReference = dependencyContext.CompileLibraries.First(l => l.Name == "SimpleMvc");
Assert.NotEmpty(packageReference.Assemblies);
Assert.Contains(customDefine, dependencyContext.CompilationOptions.Defines);
}
[Fact] [Fact]
[InitializeTestProject("ClassLibrary")] [InitializeTestProject("ClassLibrary")]
public async Task Build_ClassLibrary_DoesNotProduceDepsFileWithCompilationContext() public async Task Build_ClassLibrary_ProducesDepsFileWithoutCompiilationContext()
{ {
var result = await DotnetMSBuild("Build"); var result = await DotnetMSBuild("Build");