diff --git a/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs b/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs index 5c4478317d0..fd5f910b0b8 100644 --- a/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs +++ b/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs @@ -656,20 +656,30 @@ private static TaskHostParameters AddNetHostParamsIfNeeded( } string dotnetHostPath = getProperty(Constants.DotnetHostPathEnvVarName)?.EvaluatedValue; - string ridGraphPath = getProperty(Constants.RuntimeIdentifierGraphPath)?.EvaluatedValue; + string netCoreSdkRoot = getProperty(Constants.NetCoreSdkRoot)?.EvaluatedValue?.TrimEnd('/', '\\'); - if (string.IsNullOrEmpty(dotnetHostPath) || string.IsNullOrEmpty(ridGraphPath)) + // The NetCoreSdkRoot property got added with .NET 11, so for earlier SDKs we fall back to the RID graph path + if (string.IsNullOrEmpty(netCoreSdkRoot)) { - return currentParams; + string ridGraphPath = getProperty(Constants.RuntimeIdentifierGraphPath)?.EvaluatedValue; + if (!string.IsNullOrEmpty(ridGraphPath)) + { + netCoreSdkRoot = Path.GetDirectoryName(ridGraphPath); + } } - string msBuildAssemblyPath = Path.GetDirectoryName(ridGraphPath) ?? string.Empty; + // Both DOTNET_HOST_PATH and NetCoreSdkRoot are required to launch .NET task host. + // If both are not present, return the original parameters. + if (string.IsNullOrEmpty(dotnetHostPath) || string.IsNullOrEmpty(netCoreSdkRoot)) + { + return currentParams; + } return new TaskHostParameters( runtime: currentParams.Runtime, architecture: currentParams.Architecture, dotnetHostPath: dotnetHostPath, - msBuildAssemblyPath: msBuildAssemblyPath); + msBuildAssemblyPath: netCoreSdkRoot); } /// diff --git a/src/Build/Resources/Constants.cs b/src/Build/Resources/Constants.cs index 7d439a637c1..c1ac7f1fd1f 100644 --- a/src/Build/Resources/Constants.cs +++ b/src/Build/Resources/Constants.cs @@ -120,6 +120,11 @@ internal static class Constants /// internal const string RuntimeIdentifierGraphPath = nameof(RuntimeIdentifierGraphPath); + /// + /// The project property name used to get the root of the .NET Core SDK. + /// + internal const string NetCoreSdkRoot = nameof(NetCoreSdkRoot); + /// /// Defines the name of dotnet process based on the operating system. ///