Avoid using PATH to find dotnet.exe in RazorSDK tasks (#25750)

This pattern is pretty identical to what we have in other SDKs. It allows
using the SDK when dotnet isn't in the path such as the https://github.com/dotnet/aspnetcore/issues/25746.

Fixes #14432
This commit is contained in:
Pranav K 2020-09-10 16:30:55 -07:00 committed by GitHub
parent ce475cb00f
commit 5aa4a7cd41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 14 deletions

View File

@ -10,7 +10,6 @@ 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
{
@ -19,6 +18,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks
// From https://github.com/dotnet/corefx/blob/29cd6a0b0ac2993cee23ebaf36ca3d4bce6dd75f/src/System.IO.Pipes/ref/System.IO.Pipes.cs#L93.
// Using the enum value directly as this option is not available in netstandard.
private const PipeOptions PipeOptionCurrentUserOnly = (PipeOptions)536870912;
private string _dotnetPath;
private CancellationTokenSource _razorServerCts;
@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks
public string PipeName { get; set; }
protected override string ToolName => "dotnet";
protected override string ToolName => Path.GetDirectoryName(DotNetPath);
// If we're debugging then make all of the stdout gets logged in MSBuild
protected override MessageImportance StandardOutputLoggingImportance => DebugTool ? MessageImportance.High : base.StandardOutputLoggingImportance;
@ -49,17 +49,25 @@ namespace Microsoft.AspNetCore.Razor.Tasks
internal abstract string Command { get; }
protected override string GenerateFullPathToTool()
{
#if NETSTANDARD2_0
if (!string.IsNullOrEmpty(DotNetMuxer.MuxerPath))
{
return DotNetMuxer.MuxerPath;
}
#endif
protected override string GenerateFullPathToTool() => DotNetPath;
// use PATH to find dotnet
return ToolExe;
private string DotNetPath
{
get
{
if (!string.IsNullOrEmpty(_dotnetPath))
{
return _dotnetPath;
}
_dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (string.IsNullOrEmpty(_dotnetPath))
{
throw new InvalidOperationException("DOTNET_HOST_PATH is not set");
}
return _dotnetPath;
}
}
protected override string GenerateCommandLineCommands()

View File

@ -49,7 +49,7 @@
<Compile Include="..\..\Microsoft.AspNetCore.Razor.Tools\src\Client.cs">
<Link>Shared\Client.cs</Link>
</Compile>
<Compile Include="$(SharedSourceRoot)CommandLineUtils\**\*.cs" />
<Compile Include="$(SharedSourceRoot)CommandLineUtils\Utilities\ArgumentEscaper.cs" />
<Content Include="_._" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

View File

@ -98,6 +98,8 @@ Copyright (c) .NET Foundation. All rights reserved.
Debug="$(_RazorDebugTagHelperTask)"
DebugTool="$(_RazorDebugTagHelperTool)"
ToolAssembly="$(_RazorSdkToolAssembly)"
ToolExe="$(_RazorSdkDotNetHostFileName)"
ToolPath="$(_RazorSdkDotNetHostDirectory)"
UseServer="$(UseRazorBuildServer)"
ForceServer="$(_RazorForceBuildServer)"
PipeName="$(_RazorBuildServerPipeName)"
@ -149,6 +151,8 @@ Copyright (c) .NET Foundation. All rights reserved.
Debug="$(_RazorDebugGenerateCodeTask)"
DebugTool="$(_RazorDebugGenerateCodeTool)"
ToolAssembly="$(_RazorSdkToolAssembly)"
ToolExe="$(_RazorSdkDotNetHostFileName)"
ToolPath="$(_RazorSdkDotNetHostDirectory)"
UseServer="$(UseRazorBuildServer)"
ForceServer="$(_RazorForceBuildServer)"
PipeName="$(_RazorBuildServerPipeName)"

View File

@ -107,6 +107,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<RazorGenerate
Debug="$(_RazorDebugGenerateCodeTask)"
DebugTool="$(_RazorDebugGenerateCodeTool)"
ToolExe="$(_RazorSdkDotNetHostFileName)"
ToolPath="$(_RazorSdkDotNetHostDirectory)"
ToolAssembly="$(_RazorSdkToolAssembly)"
UseServer="$(UseRazorBuildServer)"
ForceServer="$(_RazorForceBuildServer)"

View File

@ -177,7 +177,9 @@ Integration with static web assets:
<MakeDir Directories="$(_ScopedCssIntermediatePath)" />
<RewriteCss
FilesToTransform="@(_ScopedCss)"
ToolAssembly="$(_RazorSdkToolAssembly)">
ToolAssembly="$(_RazorSdkToolAssembly)"
ToolExe="$(_RazorSdkDotNetHostFileName)"
ToolPath="$(_RazorSdkDotNetHostDirectory)">
</RewriteCss>
<ItemGroup>

View File

@ -36,6 +36,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<_RazorSdkToolAssembly>$(RazorSdkDirectoryRoot)tools\rzc.dll</_RazorSdkToolAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(DOTNET_HOST_PATH)' == ''">
<_RazorSdkDotNetHostDirectory>$(NetCoreRoot)</_RazorSdkDotNetHostDirectory>
<_RazorSdkDotNetHostFileName>dotnet</_RazorSdkDotNetHostFileName>
<_RazorSdkDotNetHostFileName Condition="'$(OS)' == 'Windows_NT'">dotnet.exe</_RazorSdkDotNetHostFileName>
</PropertyGroup>
<!-- Resolve the RazorLangVersion based on values imported or TFM. -->
<PropertyGroup>
<_TargetingNETCoreApp30OrLater Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND