Skip to content

findPath API Finds Extension Managed Runtimes #2293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vscode-dotnet-runtime-extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vscode-dotnet-runtime-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,21 @@ export function activate(vsCodeContext: vscode.ExtensionContext, extensionContex
}
}

if (commandContext.acquireContext.mode === 'runtime' || commandContext.acquireContext.mode === 'aspnetcore')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our SDKs are global, so they will be picked up by the existing logic.

{
const extensionManagedRuntimeRecordPaths = await finder.findExtensionManagedRuntimes();
const filteredExtensionManagedRuntimeRecordPaths = validator.filterValidPaths(extensionManagedRuntimeRecordPaths, commandContext);
for (const dotnetPath of filteredExtensionManagedRuntimeRecordPaths ?? [])
{
const validatedExistingManagedPath = await getPathIfValid(dotnetPath.path, validator, commandContext);
if (validatedExistingManagedPath)
{
loggingObserver.dispose();
return { dotnetPath: dotnetPath.path };
}
}
}

const dotnetOnROOT = await finder.findDotnetRootPath(commandContext.acquireContext.architecture);
const validatedRoot = await getPathIfValid(dotnetOnROOT, validator, commandContext);
if (validatedRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,42 @@ suite('DotnetCoreAcquisitionExtension End to End', function ()
}
}).timeout(standardTimeoutTime);

test('Find dotnet PATH Command works with extension-managed runtime installations', async () =>
{
// First install a runtime that we'll try to find
const version = '7.0';
const runtimePath = await installRuntime(version, 'runtime', os.arch());
assert.exists(runtimePath, 'Runtime should be installed successfully');

const originalPath = process.env.PATH;
try
{
// Filter PATH to remove any existing dotnet installations
process.env.PATH = process.env.PATH?.split(getPathSeparator())
.filter((x: string) => !(includesPathWithLikelyDotnet(x)))
.join(getPathSeparator());

const findPathContext: IDotnetFindPathContext = {
acquireContext: {
version,
requestingExtensionId,
mode: 'runtime',
architecture: os.arch()
},
versionSpecRequirement: 'latestPatch'
};

// Then verify we can find the extension-managed runtime
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.findPath', findPathContext);
assert.exists(result, 'Should find a runtime');
assert.exists(result!.dotnetPath, 'Should find a runtime path');
assert.equal(result!.dotnetPath.toLowerCase(), runtimePath.toLowerCase(), 'Should find the correct runtime path');
} finally
{
process.env.PATH = originalPath;
}
}).timeout(standardTimeoutTime);

test('Install SDK Globally E2E (Requires Admin)', async () =>
{
// We only test if the process is running under ADMIN because non-admin requires user-intervention.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DOTNET_INFORMATION_CACHE_DURATION_MS } from './CacheTimeConstants';
import { IAcquisitionWorkerContext } from './IAcquisitionWorkerContext';
import { IDotnetConditionValidator } from './IDotnetConditionValidator';
import { IDotnetListInfo } from './IDotnetListInfo';
import { InstallRecordWithPath } from './InstallRecordWithPath';
import * as versionUtils from './VersionUtilities';

type simplifiedVersionSpec = 'equal' | 'greater_than_or_equal' | 'less_than_or_equal' |
Expand Down Expand Up @@ -172,35 +173,11 @@ Please set the PATH to a dotnet host that matches the architecture ${requirement

if (availableMinor === requestedMinor && requestedPatch)
{
const availablePatchStr: string | null = requirement.acquireContext.mode !== 'sdk' ?
versionUtils.getRuntimePatchVersionString(availableVersion, this.workerContext.eventStream, this.workerContext)
:
(() =>
{
const band = versionUtils.getSDKCompleteBandAndPatchVersionString(availableVersion, this.workerContext.eventStream, this.workerContext);
if (band)
{
return band;
}
return null;
})();
const availablePatch = availablePatchStr ? Number(availablePatchStr) : null;

const availableBandStr: string | null = requirement.acquireContext.mode === 'sdk' ?
(() =>
{
const featureBand = versionUtils.getFeatureBandFromVersion(availableVersion, this.workerContext.eventStream, this.workerContext, false);
if (featureBand)
{
return featureBand;
}
return null;
})() : null;
const availableBand = availableBandStr ? Number(availableBandStr) : null;
const availablePatch = this.getPatchOrFeatureBandWithPatch(availableVersion, requirement);

switch (adjustedVersionSpec)
{
// the 'availablePatch' must exist, since the version is from --list-runtimes or --list-sdks.
// the 'availablePatch' must exist, since the version is from --list-runtimes or --list-sdks, or our internal tracking of installs.
case 'equal':
return availablePatch === requestedPatch;
case 'greater_than_or_equal':
Expand All @@ -209,6 +186,7 @@ Please set the PATH to a dotnet host that matches the architecture ${requirement
case 'less_than_or_equal':
return availablePatch! <= requestedPatch;
case 'latestPatch':
const availableBand = this.getFeatureBand(availableVersion, requirement);
const requestedBandStr = requirement.acquireContext.mode === 'sdk' ? versionUtils.getFeatureBandFromVersion(requestedVersion, this.workerContext.eventStream, this.workerContext, false) ?? null : null;
const requestedBand = requestedBandStr ? Number(requestedBandStr) : null;
return availablePatch! >= requestedPatch && (availableBand ? availableBand === requestedBand : true);
Expand All @@ -226,7 +204,10 @@ Please set the PATH to a dotnet host that matches the architecture ${requirement
return availableMinor <= requestedMinor;
case 'latestPatch':
case 'latestFeature':
return false
const availableBand = this.getFeatureBand(availableVersion, requirement);
const requestedBandStr = requirement.acquireContext.mode === 'sdk' ? versionUtils.getFeatureBandFromVersion(requestedVersion, this.workerContext.eventStream, this.workerContext, false) ?? null : null;
const requestedBand = requestedBandStr ? Number(requestedBandStr) : null;
return availableMinor === requestedMinor && (availableBand ? availableBand === requestedBand : true);
}
}
}
Expand All @@ -247,6 +228,45 @@ Please set the PATH to a dotnet host that matches the architecture ${requirement
}
}

private getFeatureBand(availableVersion: string, requirement: IDotnetFindPathContext): number | null
{
const availableBandStr: string | null = requirement.acquireContext.mode === 'sdk' ?
(() =>
{
const featureBand = versionUtils.getFeatureBandFromVersion(availableVersion, this.workerContext.eventStream, this.workerContext, false);
if (featureBand)
{
return featureBand;
}
return null;
})() : null;
return availableBandStr ? Number(availableBandStr) : null;
}

private getPatchOrFeatureBandWithPatch(availableVersion: string, requirement: IDotnetFindPathContext): number | null
{
const availablePatchStr: string | null = requirement.acquireContext.mode !== 'sdk' ?
versionUtils.getRuntimePatchVersionString(availableVersion, this.workerContext.eventStream, this.workerContext)
:
(() =>
{
Comment on lines +250 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this style intended?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is auto-formatted within the style guidelines set by the project, so in a way, yes..
even though I agree it looks ugly.

const band = versionUtils.getSDKCompleteBandAndPatchVersionString(availableVersion, this.workerContext.eventStream, this.workerContext);
if (band)
{
return band;
}
return null;
})();

const availablePatch = availablePatchStr ? Number(availablePatchStr) : null;
return availablePatch;
}

public filterValidPaths(recordPaths: InstallRecordWithPath[], requirement: IDotnetFindPathContext): InstallRecordWithPath[]
{
return recordPaths.filter(installInfo => this.stringVersionMeetsRequirement(installInfo.installRecord.dotnetInstall.version, requirement.acquireContext.version, requirement));
}

private stringArchitectureMeetsRequirement(outputArchitecture: string, requiredArchitecture: string | null | undefined): boolean
{
return !requiredArchitecture || !outputArchitecture || FileUtilities.dotnetInfoArchToNodeArch(outputArchitecture, this.workerContext.eventStream) === requiredArchitecture;
Expand Down
16 changes: 14 additions & 2 deletions vscode-dotnet-runtime-library/src/Acquisition/DotnetPathFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { IFileUtilities } from '../Utils/IFileUtilities';
import { EnvironmentVariableIsDefined, getDotnetExecutable, getOSArch, getPathSeparator } from '../Utils/TypescriptUtilities';
import { DOTNET_INFORMATION_CACHE_DURATION_MS, SYS_CMD_SEARCH_CACHE_DURATION_MS } from './CacheTimeConstants';
import { DotnetConditionValidator } from './DotnetConditionValidator';
import { InstallRecordWithPath } from './InstallRecordWithPath';
import { InstallTrackerSingleton } from './InstallTrackerSingleton';
import { RegistryReader } from './RegistryReader';

export class DotnetPathFinder implements IDotnetPathFinder
Expand Down Expand Up @@ -92,6 +94,13 @@ export class DotnetPathFinder implements IDotnetPathFinder
return undefined;
}

public async findExtensionManagedRuntimes(): Promise<InstallRecordWithPath[]>
{
const installedVersions = await InstallTrackerSingleton.getInstance(this.workerContext.eventStream, this.workerContext.extensionState).getExistingInstalls(this.workerContext.installDirectoryProvider);
const installedVersionRuntimeHostPaths = installedVersions.map(install => ({ installRecord: install, path: path.join(this.workerContext.installDirectoryProvider.getInstallDir(install.dotnetInstall.installId), getDotnetExecutable()) }));
return installedVersionRuntimeHostPaths ?? [];
}

/**
* @remarks This only checks dotnet --list-runtimes or --list-sdks, so it can be more performant for the base case.
* 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.
Expand All @@ -100,11 +109,14 @@ export class DotnetPathFinder implements IDotnetPathFinder
public async findDotnetFastFromListOnly(): Promise<string[] | undefined>
{
const oldLookup = process.env.DOTNET_MULTILEVEL_LOOKUP;
try {
try
{
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
const finalPath = await this.getTruePath(['dotnet']);
return this.returnWithRestoringEnvironment(finalPath, 'DOTNET_MULTILEVEL_LOOKUP', oldLookup);
} finally {
}
finally
{
process.env.DOTNET_MULTILEVEL_LOOKUP = oldLookup; // Ensure the environment variable is always restored
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* The .NET Foundation licenses this file to you under the MIT license.
*--------------------------------------------------------------------------------------------*/

import { InstallRecordWithPath } from './InstallRecordWithPath';

export interface IDotnetPathFinder
{
findDotnetRootPath(requestedArchitecture : string): Promise<string | undefined>;
findRawPathEnvironmentSetting(tryUseTrueShell : boolean): Promise<string[] | undefined>;
findRealPathEnvironmentSetting(tryUseTrueShell : boolean): Promise<string[] | undefined>;
findHostInstallPaths(requestedArchitecture : string): Promise<string[] | undefined>;
findDotnetRootPath(requestedArchitecture: string): Promise<string | undefined>;
findRawPathEnvironmentSetting(tryUseTrueShell: boolean): Promise<string[] | undefined>;
findRealPathEnvironmentSetting(tryUseTrueShell: boolean): Promise<string[] | undefined>;
findHostInstallPaths(requestedArchitecture: string): Promise<string[] | undefined>;
findExtensionManagedRuntimes(): Promise<InstallRecordWithPath[]>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*---------------------------------------------------------------------------------------------
* Licensed to the .NET Foundation under one or more agreements.
* The .NET Foundation licenses this file to you under the MIT license.
*--------------------------------------------------------------------------------------------*/

import { InstallRecord } from './InstallRecord';

/**
* Represents a .NET installation record along with its filesystem path
*/
export interface InstallRecordWithPath
{
installRecord: InstallRecord;
path: string;
}
2 changes: 2 additions & 0 deletions vscode-dotnet-runtime-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './Acquisition/IInstallationDirectoryProvider';
export * from './Acquisition/IJsonInstaller';
export * from './Acquisition/InstallationValidator';
export * from './Acquisition/InstallRecord';
export * from './Acquisition/InstallRecordWithPath';
export * from './Acquisition/IVersionResolver';
export * from './Acquisition/JsonInstaller';
export * from './Acquisition/LinuxGlobalInstaller';
Expand Down Expand Up @@ -67,3 +68,4 @@ export * from './Utils/TypescriptUtilities';
export * from './Utils/VSCodeEnvironment';
export * from './Utils/WebRequestWorkerSingleton';
export * from './VSCodeExtensionContext';

Loading