Skip to content

Commit f5411d3

Browse files
nagilsonCopilot
andauthored
Check dotnet path without which or where first (#2234)
* Check dotnet without which or where first Which and where can be slow to find. For the common case which we want to make fast, we can just check 'dotnet' first and see if that works. Note: Ideally, this function would only return a string, but that would require writing a wrapper around this return type just for this function and does not follow the other existing patterns. With respect to tests: This should be covered by the existing 'find path' tests. * Utilize a finally block Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent de07136 commit f5411d3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

vscode-dotnet-runtime-extension/src/extension.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,17 @@ export function activate(vsCodeContext: vscode.ExtensionContext, extensionContex
515515
const validator = new DotnetConditionValidator(workerContext, utilContext);
516516
const finder = new DotnetPathFinder(workerContext, utilContext);
517517

518+
const dotnetOnShellSpawn = (await finder.findDotnetFastFromListOnly())?.[0] ?? '';
519+
if (dotnetOnShellSpawn)
520+
{
521+
const validatedShellSpawn = await getPathIfValid(dotnetOnShellSpawn, validator, commandContext);
522+
if (validatedShellSpawn)
523+
{
524+
loggingObserver.dispose();
525+
return { dotnetPath: validatedShellSpawn };
526+
}
527+
}
528+
518529
const dotnetsOnPATH = await finder.findRawPathEnvironmentSetting();
519530
for (const dotnetPath of dotnetsOnPATH ?? [])
520531
{

vscode-dotnet-runtime-library/src/Acquisition/DotnetPathFinder.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ export class DotnetPathFinder implements IDotnetPathFinder
9292
return undefined;
9393
}
9494

95+
/**
96+
* @remarks This only checks dotnet --list-runtimes or --list-sdks, so it can be more performant for the base case.
97+
* This allows skipping which, where, and also does not rely on --info which is slower because it shells to the SDK instead of just being the host.
98+
* @returns The path to the dotnet executable, which may be a symlink to the actual executable.
99+
*/
100+
public async findDotnetFastFromListOnly(): Promise<string[] | undefined>
101+
{
102+
const oldLookup = process.env.DOTNET_MULTILEVEL_LOOKUP;
103+
try {
104+
process.env.DOTNET_MULTILEVEL_LOOKUP = '0'; // make it so --list-runtimes only finds the runtimes on that path: https://learn.microsoft.com/en-us/dotnet/core/compatibility/deployment/7.0/multilevel-lookup#reason-for-change
105+
const finalPath = await this.getTruePath(['dotnet']);
106+
return this.returnWithRestoringEnvironment(finalPath, 'DOTNET_MULTILEVEL_LOOKUP', oldLookup);
107+
} finally {
108+
process.env.DOTNET_MULTILEVEL_LOOKUP = oldLookup; // Ensure the environment variable is always restored
109+
}
110+
}
111+
95112
/**
96113
*
97114
* @returns A set of the path environment variable(s) for which or where dotnet, which may need to be converted to the actual path if it points to a polymorphic executable.

0 commit comments

Comments
 (0)