Skip to content

Commit 78d4438

Browse files
authored
Allow RazorTagHelper task to be invoked by MSBuild targets that do not specify ToolExe (#28416)
The RazorTagHelper task in the SDK is invoked by targets outside the SDK. Consequently, any new requirements on the tasks need to be compatible with these versions. In 5.0, we changed the task to expect the path to be passed in by the ToolExe property. This changes allows using dotnet.exe from the ambient PATH to be used instead (the previous behavior)
1 parent a4f30ae commit 78d4438

File tree

10 files changed

+147
-6
lines changed

10 files changed

+147
-6
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Testing;
7+
8+
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
9+
{
10+
public class MvcBuildIntegrationTest22 : MvcBuildIntegrationTestLegacy
11+
{
12+
public MvcBuildIntegrationTest22(LegacyBuildServerTestFixture buildServer)
13+
: base(buildServer)
14+
{
15+
}
16+
17+
public override string TestProjectName => "SimpleMvc22";
18+
public override string TargetFramework => "netcoreapp2.2";
19+
20+
[ConditionalFact]
21+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
22+
public async Task BuildProject_UsingDesktopMSBuild()
23+
{
24+
using var _ = CreateTestProject();
25+
26+
// Build
27+
// This is a regression test for https://github.com/dotnet/aspnetcore/issues/28333. We're trying to ensure
28+
// building in Desktop when DOTNET_HOST_PATH is not configured continues to work.
29+
// Explicitly unset it to verify a value is not being picked up as an ambient value.
30+
var result = await DotnetMSBuild("Build", args: "/p:DOTNET_HOST_PATH=", msBuildProcessKind: MSBuildProcessKind.Desktop);
31+
32+
Assert.BuildPassed(result);
33+
Assert.FileExists(result, OutputPath, OutputFileName);
34+
Assert.FileExists(result, OutputPath, $"{TestProjectName}.pdb");
35+
Assert.FileExists(result, OutputPath, $"{TestProjectName}.Views.dll");
36+
Assert.FileExists(result, OutputPath, $"{TestProjectName}.Views.pdb");
37+
38+
// Verify RazorTagHelper works
39+
Assert.FileExists(result, IntermediateOutputPath, $"{TestProjectName}.TagHelpers.input.cache");
40+
Assert.FileExists(result, IntermediateOutputPath, $"{TestProjectName}.TagHelpers.output.cache");
41+
Assert.FileContains(
42+
result,
43+
Path.Combine(IntermediateOutputPath, $"{TestProjectName}.TagHelpers.output.cache"),
44+
@"""Name"":""SimpleMvc.SimpleTagHelper""");
45+
}
46+
}
47+
}

src/Razor/Microsoft.NET.Sdk.Razor/src/DotnetToolTask.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Razor.Tools;
1111
using Microsoft.Build.Framework;
1212
using Microsoft.Build.Utilities;
13+
using Microsoft.Extensions.CommandLineUtils;
1314

1415
namespace Microsoft.AspNetCore.Razor.Tasks
1516
{
@@ -40,7 +41,7 @@ public abstract class DotNetToolTask : ToolTask
4041

4142
public string PipeName { get; set; }
4243

43-
protected override string ToolName => Path.GetDirectoryName(DotNetPath);
44+
protected override string ToolName => "dotnet";
4445

4546
// If we're debugging then make all of the stdout gets logged in MSBuild
4647
protected override MessageImportance StandardOutputLoggingImportance => DebugTool ? MessageImportance.High : base.StandardOutputLoggingImportance;
@@ -60,11 +61,7 @@ private string DotNetPath
6061
return _dotnetPath;
6162
}
6263

63-
_dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
64-
if (string.IsNullOrEmpty(_dotnetPath))
65-
{
66-
throw new InvalidOperationException("DOTNET_HOST_PATH is not set");
67-
}
64+
_dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? ToolExe;
6865

6966
return _dotnetPath;
7067
}

src/Razor/test/testassets/RestoreTestProjects/RestoreTestProjects.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
<ItemGroup>
77
<ProjectReference Include="..\SimpleMvc21\SimpleMvc21.csproj" />
8+
<ProjectReference Include="..\SimpleMvc22\SimpleMvc22.csproj" />
89
<ProjectReference Include="..\SimpleMvc31\SimpleMvc31.csproj" />
910
<ProjectReference Include="..\blazor31\blazor31.csproj" />
1011
<ProjectReference Include="..\ClassLibraryMvc21\ClassLibraryMvc21.csproj" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
namespace SimpleMvc
3+
{
4+
public class Program
5+
{
6+
public static void Main(string[] args)
7+
{
8+
// Just make sure we have a reference to MVC
9+
var t = typeof(Microsoft.AspNetCore.Mvc.IActionResult);
10+
System.Console.WriteLine(t.FullName);
11+
}
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<!--
4+
This project references a shipped version of MVC and should not reference local builds of
5+
the CodeGeneration targets, rzc, or any of the test shims.
6+
-->
7+
8+
<PropertyGroup>
9+
<TargetFramework>netcoreapp2.2</TargetFramework>
10+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
11+
<RazorSdkDirectoryRoot>$(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\</RazorSdkDirectoryRoot>
12+
</PropertyGroup>
13+
14+
<!-- Test Placeholder -->
15+
16+
<PropertyGroup Condition="'$(RunningAsTest)' == ''">
17+
<!-- We don't want to run build server when not running as tests. -->
18+
<UseRazorBuildServer>false</UseRazorBuildServer>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="Microsoft.AspNetCore.App">
23+
<!-- Avoid exporting types from real MVC that will conflict with the test shim -->
24+
<PrivateAssets>All</PrivateAssets>
25+
</PackageReference>
26+
27+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0">
28+
<!-- Avoid exporting types from real MVC that will conflict with the test shim -->
29+
<PrivateAssets>All</PrivateAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
3+
namespace SimpleMvc
4+
{
5+
public class SimpleTagHelper : TagHelper
6+
{
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<h2>
6+
Learn how to build ASP.NET apps that can run anywhere.
7+
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409">
8+
Learn More
9+
</a>
10+
</h2>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - SimpleMvc</title>
7+
8+
<environment exclude="Development">
9+
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
10+
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
11+
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
12+
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
13+
</environment>
14+
</head>
15+
<body>
16+
<nav class="navbar navbar-inverse navbar-fixed-top">
17+
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">SimpleMvc</a>
18+
</nav>
19+
<div class="container body-content">
20+
@RenderBody()
21+
</div>
22+
23+
@RenderSection("Scripts", required: false)
24+
</body>
25+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@using SimpleMvc
2+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3+
@namespace SimpleMvc21
4+
@inject DateTime TheTime
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}

0 commit comments

Comments
 (0)