From 78d44383c86c9367337947af7af1dc44f047870e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 8 Dec 2020 14:17:29 -0800 Subject: [PATCH] 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) --- .../MvcBuildIntegrationTest22.cs | 47 +++++++++++++++++++ .../src/DotnetToolTask.cs | 9 ++-- .../RestoreTestProjects.csproj | 1 + .../test/testassets/SimpleMvc22/Program.cs | 13 +++++ .../testassets/SimpleMvc22/SimpleMvc22.csproj | 33 +++++++++++++ .../testassets/SimpleMvc22/SimpleTagHelper.cs | 8 ++++ .../SimpleMvc22/Views/Home/Index.cshtml | 10 ++++ .../SimpleMvc22/Views/Shared/_Layout.cshtml | 25 ++++++++++ .../SimpleMvc22/Views/_ViewImports.cshtml | 4 ++ .../SimpleMvc22/Views/_ViewStart.cshtml | 3 ++ 10 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/MvcBuildIntegrationTest22.cs create mode 100644 src/Razor/test/testassets/SimpleMvc22/Program.cs create mode 100644 src/Razor/test/testassets/SimpleMvc22/SimpleMvc22.csproj create mode 100644 src/Razor/test/testassets/SimpleMvc22/SimpleTagHelper.cs create mode 100644 src/Razor/test/testassets/SimpleMvc22/Views/Home/Index.cshtml create mode 100644 src/Razor/test/testassets/SimpleMvc22/Views/Shared/_Layout.cshtml create mode 100644 src/Razor/test/testassets/SimpleMvc22/Views/_ViewImports.cshtml create mode 100644 src/Razor/test/testassets/SimpleMvc22/Views/_ViewStart.cshtml diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/MvcBuildIntegrationTest22.cs b/src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/MvcBuildIntegrationTest22.cs new file mode 100644 index 0000000000..1e60d0b8d7 --- /dev/null +++ b/src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/MvcBuildIntegrationTest22.cs @@ -0,0 +1,47 @@ +// 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.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Testing; + +namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests +{ + public class MvcBuildIntegrationTest22 : MvcBuildIntegrationTestLegacy + { + public MvcBuildIntegrationTest22(LegacyBuildServerTestFixture buildServer) + : base(buildServer) + { + } + + public override string TestProjectName => "SimpleMvc22"; + public override string TargetFramework => "netcoreapp2.2"; + + [ConditionalFact] + [OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)] + public async Task BuildProject_UsingDesktopMSBuild() + { + using var _ = CreateTestProject(); + + // Build + // This is a regression test for https://github.com/dotnet/aspnetcore/issues/28333. We're trying to ensure + // building in Desktop when DOTNET_HOST_PATH is not configured continues to work. + // Explicitly unset it to verify a value is not being picked up as an ambient value. + var result = await DotnetMSBuild("Build", args: "/p:DOTNET_HOST_PATH=", msBuildProcessKind: MSBuildProcessKind.Desktop); + + Assert.BuildPassed(result); + Assert.FileExists(result, OutputPath, OutputFileName); + Assert.FileExists(result, OutputPath, $"{TestProjectName}.pdb"); + Assert.FileExists(result, OutputPath, $"{TestProjectName}.Views.dll"); + Assert.FileExists(result, OutputPath, $"{TestProjectName}.Views.pdb"); + + // Verify RazorTagHelper works + Assert.FileExists(result, IntermediateOutputPath, $"{TestProjectName}.TagHelpers.input.cache"); + Assert.FileExists(result, IntermediateOutputPath, $"{TestProjectName}.TagHelpers.output.cache"); + Assert.FileContains( + result, + Path.Combine(IntermediateOutputPath, $"{TestProjectName}.TagHelpers.output.cache"), + @"""Name"":""SimpleMvc.SimpleTagHelper"""); + } + } +} diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/src/DotnetToolTask.cs b/src/Razor/Microsoft.NET.Sdk.Razor/src/DotnetToolTask.cs index 7aee011260..f72cb7791e 100644 --- a/src/Razor/Microsoft.NET.Sdk.Razor/src/DotnetToolTask.cs +++ b/src/Razor/Microsoft.NET.Sdk.Razor/src/DotnetToolTask.cs @@ -10,6 +10,7 @@ using System.Threading; using Microsoft.AspNetCore.Razor.Tools; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +using Microsoft.Extensions.CommandLineUtils; namespace Microsoft.AspNetCore.Razor.Tasks { @@ -40,7 +41,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks public string PipeName { get; set; } - protected override string ToolName => Path.GetDirectoryName(DotNetPath); + protected override string ToolName => "dotnet"; // If we're debugging then make all of the stdout gets logged in MSBuild protected override MessageImportance StandardOutputLoggingImportance => DebugTool ? MessageImportance.High : base.StandardOutputLoggingImportance; @@ -60,11 +61,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks return _dotnetPath; } - _dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH"); - if (string.IsNullOrEmpty(_dotnetPath)) - { - throw new InvalidOperationException("DOTNET_HOST_PATH is not set"); - } + _dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? ToolExe; return _dotnetPath; } diff --git a/src/Razor/test/testassets/RestoreTestProjects/RestoreTestProjects.csproj b/src/Razor/test/testassets/RestoreTestProjects/RestoreTestProjects.csproj index d6d2bcc427..741109414c 100644 --- a/src/Razor/test/testassets/RestoreTestProjects/RestoreTestProjects.csproj +++ b/src/Razor/test/testassets/RestoreTestProjects/RestoreTestProjects.csproj @@ -5,6 +5,7 @@ + diff --git a/src/Razor/test/testassets/SimpleMvc22/Program.cs b/src/Razor/test/testassets/SimpleMvc22/Program.cs new file mode 100644 index 0000000000..8a287099ac --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/Program.cs @@ -0,0 +1,13 @@ + +namespace SimpleMvc +{ + public class Program + { + public static void Main(string[] args) + { + // Just make sure we have a reference to MVC + var t = typeof(Microsoft.AspNetCore.Mvc.IActionResult); + System.Console.WriteLine(t.FullName); + } + } +} diff --git a/src/Razor/test/testassets/SimpleMvc22/SimpleMvc22.csproj b/src/Razor/test/testassets/SimpleMvc22/SimpleMvc22.csproj new file mode 100644 index 0000000000..e5f191fae7 --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/SimpleMvc22.csproj @@ -0,0 +1,33 @@ + + + + + + netcoreapp2.2 + false + $(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\ + + + + + + + false + + + + + + All + + + + + All + + + + diff --git a/src/Razor/test/testassets/SimpleMvc22/SimpleTagHelper.cs b/src/Razor/test/testassets/SimpleMvc22/SimpleTagHelper.cs new file mode 100644 index 0000000000..7cc52922d9 --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/SimpleTagHelper.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace SimpleMvc +{ + public class SimpleTagHelper : TagHelper + { + } +} diff --git a/src/Razor/test/testassets/SimpleMvc22/Views/Home/Index.cshtml b/src/Razor/test/testassets/SimpleMvc22/Views/Home/Index.cshtml new file mode 100644 index 0000000000..9acd890016 --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/Views/Home/Index.cshtml @@ -0,0 +1,10 @@ +@{ + ViewData["Title"] = "Home Page"; +} + +

+ Learn how to build ASP.NET apps that can run anywhere. + + Learn More + +

diff --git a/src/Razor/test/testassets/SimpleMvc22/Views/Shared/_Layout.cshtml b/src/Razor/test/testassets/SimpleMvc22/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000000..022b6fa718 --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/Views/Shared/_Layout.cshtml @@ -0,0 +1,25 @@ + + + + + + @ViewData["Title"] - SimpleMvc + + + + + + + + +
+ @RenderBody() +
+ + @RenderSection("Scripts", required: false) + + diff --git a/src/Razor/test/testassets/SimpleMvc22/Views/_ViewImports.cshtml b/src/Razor/test/testassets/SimpleMvc22/Views/_ViewImports.cshtml new file mode 100644 index 0000000000..9619bedf5e --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/Views/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@using SimpleMvc +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@namespace SimpleMvc21 +@inject DateTime TheTime diff --git a/src/Razor/test/testassets/SimpleMvc22/Views/_ViewStart.cshtml b/src/Razor/test/testassets/SimpleMvc22/Views/_ViewStart.cshtml new file mode 100644 index 0000000000..a5f10045db --- /dev/null +++ b/src/Razor/test/testassets/SimpleMvc22/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "_Layout"; +}