From b74582e101b86bb683c2df6befe11752dd3fcbee Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 15 Mar 2018 11:13:50 -0700 Subject: [PATCH] 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 --- .../Sdk.Razor.CurrentVersion.targets | 7 +++++ .../IntegrationTests/BuildIntegrationTest.cs | 28 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets b/src/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets index 7a17ebc6c0..3332959fc2 100644 --- a/src/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets +++ b/src/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets @@ -166,6 +166,13 @@ Copyright (c) .NET Foundation. All rights reserved. true + + + true + + diff --git a/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs index cce650efc1..18a67e7038 100644 --- a/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs @@ -257,7 +257,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests [Fact] [InitializeTestProject("SimpleMvc")] - public async Task Build_WithoutViews_ProducesDepsFileWithotCompiilationContext() + public async Task Build_WithoutViews_ProducesDepsFileWithoutCompiilationContext() { Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true); var customDefine = "RazorSdkTest"; @@ -272,9 +272,33 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests 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] [InitializeTestProject("ClassLibrary")] - public async Task Build_ClassLibrary_DoesNotProduceDepsFileWithCompilationContext() + public async Task Build_ClassLibrary_ProducesDepsFileWithoutCompiilationContext() { var result = await DotnetMSBuild("Build");