Skip to content

Commit 302e031

Browse files
committed
Add query option to search all dotnet locations
1 parent 2424986 commit 302e031

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/MSBuildLocator/DotNetSdkLocationHelper.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static class DotNetSdkLocationHelper
7070
discoveryType: DiscoveryType.DotNetSdk);
7171
}
7272

73-
public static IEnumerable<VisualStudioInstance> GetInstances(string workingDirectory, bool allowQueryAllRuntimes)
73+
public static IEnumerable<VisualStudioInstance> GetInstances(string workingDirectory, bool allowQueryAllRuntimes, bool allowAllDotnetLocations)
7474
{
7575
string? bestSdkPath;
7676
string[] allAvailableSdks;
@@ -79,7 +79,7 @@ public static IEnumerable<VisualStudioInstance> GetInstances(string workingDirec
7979
AddUnmanagedDllResolver();
8080

8181
bestSdkPath = GetSdkFromGlobalSettings(workingDirectory);
82-
allAvailableSdks = GetAllAvailableSDKs().ToArray();
82+
allAvailableSdks = GetAllAvailableSDKs(allowAllDotnetLocations).ToArray();
8383
}
8484
finally
8585
{
@@ -116,7 +116,7 @@ public static IEnumerable<VisualStudioInstance> GetInstances(string workingDirec
116116
}
117117

118118
// Returns the list of all available SDKs ordered by ascending version.
119-
static IEnumerable<string> GetAllAvailableSDKs()
119+
static IEnumerable<string> GetAllAvailableSDKs(bool allowAllDotnetLocations)
120120
{
121121
bool foundSdks = false;
122122
string[]? resolvedPaths = null;
@@ -132,6 +132,11 @@ static IEnumerable<string> GetAllAvailableSDKs()
132132
{
133133
yield return path;
134134
}
135+
136+
if (resolvedPaths.Length > 0 && !allowAllDotnetLocations)
137+
{
138+
break;
139+
}
135140
}
136141
}
137142

src/MSBuildLocator/MSBuildLocator.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public static class MSBuildLocator
5252
/// </remarks.
5353
public static bool AllowQueryAllRuntimeVersions { get; set; } = false;
5454

55+
/// <summary>
56+
/// Allow discovery of .NET SDK versions from all discovered dotnet install locations.
57+
/// </summary>
58+
/// <remarks>
59+
/// Defaults to <see langword="false"/>. Set this to <see langword="true"/> only if you do not mind behaving differently than the dotnet muxer.
60+
/// </remarks.
61+
public static bool AllowQueryAllDotnetLocations { get; set; } = false;
62+
5563
/// <summary>
5664
/// Gets a value indicating whether an instance of MSBuild can be registered.
5765
/// </summary>
@@ -200,7 +208,7 @@ private static void RegisterMSBuildPathsInternally(string[] msbuildSearchPaths)
200208
{
201209
if (string.IsNullOrWhiteSpace(msbuildSearchPaths[i]))
202210
{
203-
nullOrWhiteSpaceExceptions.Add(new ArgumentException($"Value at position {i+1} may not be null or whitespace", nameof(msbuildSearchPaths)));
211+
nullOrWhiteSpaceExceptions.Add(new ArgumentException($"Value at position {i + 1} may not be null or whitespace", nameof(msbuildSearchPaths)));
204212
}
205213
}
206214
if (nullOrWhiteSpaceExceptions.Count > 0)
@@ -266,7 +274,7 @@ private static void RegisterMSBuildPathsInternally(string[] msbuildSearchPaths)
266274

267275
AppDomain.CurrentDomain.AssemblyResolve += s_registeredHandler;
268276
#else
269-
s_registeredHandler = (_, assemblyName) =>
277+
s_registeredHandler = (_, assemblyName) =>
270278
{
271279
return TryLoadAssembly(assemblyName);
272280
};
@@ -377,7 +385,8 @@ private static IEnumerable<VisualStudioInstance> GetInstances(VisualStudioInstan
377385
#if NETCOREAPP
378386
// AllowAllRuntimeVersions was added to VisualStudioInstanceQueryOptions for fulfilling Roslyn's needs. One of the properties will be removed in v2.0.
379387
bool allowAllRuntimeVersions = AllowQueryAllRuntimeVersions || options.AllowAllRuntimeVersions;
380-
foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory, allowAllRuntimeVersions))
388+
bool allowAllDotnetLocations = AllowQueryAllDotnetLocations || options.AllowAllDotnetLocations;
389+
foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory, allowAllRuntimeVersions, allowAllDotnetLocations))
381390
yield return dotnetSdk;
382391
#endif
383392
}
@@ -404,7 +413,7 @@ private static VisualStudioInstance GetDevConsoleInstance()
404413
Version.TryParse(versionString, out version);
405414
}
406415

407-
if(version != null)
416+
if (version != null)
408417
{
409418
return new VisualStudioInstance("DEVCONSOLE", path, version, DiscoveryType.DeveloperConsole);
410419
}

src/MSBuildLocator/VisualStudioInstanceQueryOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public class VisualStudioInstanceQueryOptions
3737
/// Defaults to <see langword="false"/>. Set this to <see langword="true"/> only if your application has special logic to handle loading an incompatible SDK, such as launching a new process with the target SDK's runtime.
3838
/// </remarks.
3939
public bool AllowAllRuntimeVersions { get; set; } = false;
40+
41+
/// <summary>
42+
/// Allow discovery of .NET SDK versions from all discovered dotnet install locations.
43+
/// </summary>
44+
/// <remarks>
45+
/// Defaults to <see langword="false"/>. Set this to <see langword="true"/> only if you do not mind behaving differently than the dotnet muxer.
46+
/// </remarks.
47+
public bool AllowAllDotnetLocations { get; set; } = false;
4048
#endif
4149

4250
/// <summary>

0 commit comments

Comments
 (0)