Skip to content

Commit 43c449f

Browse files
authored
make SdkResolver try to locate the dotnet host closest to the resolved SDK (#49772)
1 parent 766a80a commit 43c449f

File tree

3 files changed

+213
-50
lines changed

3 files changed

+213
-50
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

src/Resolvers/Microsoft.DotNet.SdkResolver/VSSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private VSSettings()
6363
}
6464

6565
// Test constructor
66-
public VSSettings(string settingsFilePath, bool disallowPrereleaseByDefault)
66+
public VSSettings(string? settingsFilePath, bool disallowPrereleaseByDefault)
6767
{
6868
_settingsFilePath = settingsFilePath;
6969
_disallowPrereleaseByDefault = disallowPrereleaseByDefault;

0 commit comments

Comments
 (0)