Make sure RazorSdk works when BuildingInVisualStudio \ BuildProjectReferences is disabled

Fixes #2247
This commit is contained in:
Pranav K 2018-04-13 11:26:22 -07:00
parent f8dc5c4702
commit 8d1de6ec80
8 changed files with 101 additions and 3 deletions

View File

@ -69,6 +69,11 @@ Copyright (c) .NET Foundation. All rights reserved.
_RazorAddDebugSymbolsProjectOutputGroupOutput
</DebugSymbolsProjectOutputGroupDependsOn>
<PrepareForRunDependsOn>
_RazorPrepareForRun;
$(PrepareForRunDependsOn)
</PrepareForRunDependsOn>
</PropertyGroup>
<!--
@ -361,6 +366,15 @@ Copyright (c) .NET Foundation. All rights reserved.
</Target>
<!--
Set up RazorCompile to run before PrepareForRun. This should ensure that the Razor dll and pdbs are available to be copied
as part of GetCopyToOutputDirectoryItems which is invoked during PrepareForRun.
-->
<Target
Name="_RazorPrepareForRun"
DependsOnTargets="RazorCompile"
Condition="'$(ResolvedRazorCompileToolset)'=='RazorSdk' and '$(RazorCompileOnBuild)'=='true'" />
<!--
Called as part of GetCopyToOutputDirectoryItems - this target populates the list of items that get
copied to the output directory when building as a project reference.
@ -368,7 +382,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<Target
Name="_RazorGetCopyToOutputDirectoryItems"
BeforeTargets="GetCopyToOutputDirectoryItems"
DependsOnTargets="RazorCompile"
Condition="'$(ResolvedRazorCompileToolset)'=='RazorSdk' and '$(RazorCompileOnBuild)'=='true'">
<!--

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
@ -472,6 +473,41 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvcFSharp.Views.pdb");
}
[Fact]
[InitializeTestProject("AppWithP2PReference", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Build_WithP2P_WorksWhenBuildProjectReferencesIsDisabled()
{
// Simulates building the same way VS does by setting BuildProjectReferences=false.
// With this flag, P2P references aren't resolved during GetCopyToOutputDirectoryItems. This test ensures that
// no Razor work is done in such a scenario and the build succeeds.
var additionalProjectContent = @"
<ItemGroup>
<ProjectReference Include=""..\ClassLibrary2\ClassLibrary2.csproj"" />
</ItemGroup>
";
AddProjectFileContent(additionalProjectContent);
var result = await DotnetMSBuild(target: default);
Assert.BuildPassed(result);
Assert.FileExists(result, OutputPath, "AppWithP2PReference.dll");
Assert.FileExists(result, OutputPath, "AppWithP2PReference.Views.dll");
Assert.FileExists(result, OutputPath, "ClassLibrary.dll");
Assert.FileExists(result, OutputPath, "ClassLibrary.Views.dll");
Assert.FileExists(result, OutputPath, "ClassLibrary2.dll");
Assert.FileExists(result, OutputPath, "ClassLibrary2.Views.dll");
// Force a rebuild of ClassLibrary2 by changing a file
var class2Path = Path.Combine(Project.SolutionPath, "ClassLibrary2", "Class2.cs");
File.AppendAllText(class2Path, Environment.NewLine + "// Some changes");
// dotnet msbuild /p:BuildProjectReferences=false
result = await DotnetMSBuild(target: default, "/p:BuildProjectReferences=false", suppressRestore: true);
Assert.BuildPassed(result);
}
private static DependencyContext ReadDependencyContext(string depsFilePath)
{
var reader = new DependencyContextJsonReader();

View File

@ -37,7 +37,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
MSBuildIntegrationTestBase.Project = ProjectDirectory.Create(_originalProjectName, _testProjectName, _baseDirectory, _additionalProjects, _language);
MSBuildIntegrationTestBase.TargetFramework = _originalProjectName == "ClassLibrary" ? "netstandard2.0" : "netcoreapp2.0";
MSBuildIntegrationTestBase.TargetFramework = _originalProjectName.StartsWith("ClassLibrary") ? "netstandard2.0" : "netcoreapp2.0";
}
public override void After(MethodInfo methodUnderTest)

View File

@ -79,7 +79,12 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
buildArgumentList.Add($"/p:_RazorSuppressCurrentUserOnlyPipeOptions=true");
}
buildArgumentList.Add($"/t:{target} /p:Configuration={Configuration} {args}");
if (!string.IsNullOrEmpty(target))
{
buildArgumentList.Add($"/t:{target}");
}
buildArgumentList.Add($"/p:Configuration={Configuration} {args}");
var buildArguments = string.Join(" ", buildArgumentList);
return MSBuildProcessManager.RunProcessAsync(

View File

@ -0,0 +1,13 @@
using System;
using ClassLibrary;
namespace ClassLibrary2
{
public class Class2
{
public void Method()
{
Console.WriteLine(typeof(Class1));
}
}
}

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<_RazorMSBuildRoot>$(SolutionRoot)src\Microsoft.AspNetCore.Razor.Design\bin\$(Configuration)\netstandard2.0\</_RazorMSBuildRoot>
</PropertyGroup>
<Import Project="$(SolutionRoot)src\Microsoft.AspNetCore.Razor.Design\build\netstandard2.0\Microsoft.AspNetCore.Razor.Design.props" />
<PropertyGroup>
<!-- Override for the MVC extension -->
<_MvcExtensionAssemblyPath>$(SolutionRoot)src\Microsoft.AspNetCore.Mvc.Razor.Extensions\bin\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll</_MvcExtensionAssemblyPath>
</PropertyGroup>
<Import Project="$(SolutionRoot)src\Microsoft.AspNetCore.Mvc.Razor.Extensions\build\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary\ClassLibrary.csproj" />
</ItemGroup>
<!-- Test Placeholder -->
<Import Project="$(SolutionRoot)src\Microsoft.AspNetCore.Mvc.Razor.Extensions\build\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.targets" />
</Project>

View File

@ -0,0 +1,2 @@
@{ var message = "Hello world";}
@message

View File

@ -0,0 +1,2 @@
@using ClassLibrary
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers