Skip to content

Commit d401175

Browse files
committed
make SdkResolver try to locate the dotnet host closest to the resolved SDK
1 parent 1177acc commit d401175

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ private sealed class CachedState
196196
minimumVSDefinedSDKVersion);
197197
}
198198

199-
string? dotnetExe = dotnetRoot != null ?
200-
Path.Combine(dotnetRoot, Constants.DotNetExe) :
201-
null;
199+
string? dotnetExe =
200+
TryResolveDotnetExeFromSdkResolution(resolverResult)
201+
?? Path.Combine(dotnetRoot, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Constants.DotNetExe : Constants.DotNet);
202202
if (File.Exists(dotnetExe))
203203
{
204204
propertiesToAdd ??= new Dictionary<string, string?>();
@@ -288,6 +288,25 @@ private sealed class CachedState
288288
return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion, propertiesToAdd, itemsToAdd, warnings);
289289
}
290290

291+
/// <summary>Try to find the dotnet binary from the SDK resolution result upwards</summary>
292+
private static string? TryResolveDotnetExeFromSdkResolution(SdkResolutionResult resolverResult)
293+
{
294+
var expectedFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Constants.DotNetExe : Constants.DotNet;
295+
var currentDir = resolverResult.ResolvedSdkDirectory;
296+
while (currentDir != null)
297+
{
298+
var dotnetExe = Path.Combine(currentDir, expectedFileName);
299+
if (File.Exists(dotnetExe))
300+
{
301+
return dotnetExe;
302+
}
303+
304+
currentDir = Path.GetDirectoryName(currentDir);
305+
}
306+
307+
return null;
308+
}
309+
291310
private static string? GetMSbuildRuntimeVersion(string sdkDirectory, string dotnetRoot)
292311
{
293312
// 1. Get the runtime version from the MSBuild.runtimeconfig.json file

0 commit comments

Comments
 (0)