Skip to content
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

Fix dotnet path resolution when using snap installed packages #6515

Merged
merged 1 commit into from
Oct 11, 2023
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
52 changes: 28 additions & 24 deletions src/lsptoolshost/dotnetRuntimeExtensionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import { PlatformInformation } from '../shared/platform';
import { commonOptions } from '../shared/options';
import { existsSync } from 'fs';
import { CSharpExtensionId } from '../constants/csharpExtensionId';
import { promisify } from 'util';
import { exec } from 'child_process';
import { getDotnetInfo } from '../shared/utils/getDotnetInfo';
import { readFile, realpath } from 'fs/promises';
import { readFile } from 'fs/promises';
import { RuntimeInfo } from '../shared/utils/dotnetInfo';

export const DotNetRuntimeVersion = '7.0';

Expand Down Expand Up @@ -62,7 +61,7 @@ export class DotnetRuntimeExtensionResolver implements IHostExecutableResolver {
dotnetRuntimePath = path.dirname(dotnetInfo.path);
}

const dotnetExecutableName = this.platformInfo.isWindows() ? 'dotnet.exe' : 'dotnet';
const dotnetExecutableName = this.getDotnetExecutableName();
const dotnetExecutablePath = path.join(dotnetRuntimePath, dotnetExecutableName);
if (!existsSync(dotnetExecutablePath)) {
throw new Error(`Cannot find dotnet path '${dotnetExecutablePath}'`);
Expand Down Expand Up @@ -151,39 +150,40 @@ export class DotnetRuntimeExtensionResolver implements IHostExecutableResolver {
}

const coreRuntimeVersions = dotnetInfo.Runtimes['Microsoft.NETCore.App'];
let foundRuntimeVersion = false;
for (const version of coreRuntimeVersions) {
let matchingRuntime: RuntimeInfo | undefined = undefined;
for (const runtime of coreRuntimeVersions) {
// We consider a match if the runtime is greater than or equal to the required version since we roll forward.
if (semver.gt(version, requiredRuntimeVersion)) {
foundRuntimeVersion = true;
if (semver.gt(runtime.Version, requiredRuntimeVersion)) {
matchingRuntime = runtime;
break;
}
}

if (!foundRuntimeVersion) {
if (!matchingRuntime) {
throw new Error(
`No compatible .NET runtime found. Minimum required version is ${this.minimumDotnetRuntimeVersion}.`
);
}

// Find the location of the dotnet on path.
const command = this.platformInfo.isWindows() ? 'where' : 'which';
const whereOutput = await promisify(exec)(`${command} dotnet`);
if (!whereOutput.stdout) {
throw new Error(`Unable to find dotnet from ${command}.`);
}

// There could be multiple paths output from where. Take the first since that is what we used to run dotnet --info.
const path = whereOutput.stdout.trim().replace(/\r/gm, '').split('\n')[0];
if (!existsSync(path)) {
throw new Error(`dotnet path does not exist: ${path}`);
// The .NET install layout is a well known structure on all platforms.
// See https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#net-core-install-layout
//
// Therefore we know that the runtime path is always in <install root>/shared/<runtime name>
// and the dotnet executable is always at <install root>/dotnet(.exe).
Comment on lines +171 to +172
Copy link
Member

Choose a reason for hiding this comment

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

Should we be renaming this function? (findDotnetFromPath)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is still valid - this is still from the dotnet on path (via executing the dotnet commands)

//
// Since dotnet --list-runtimes will always use the real assembly path to output the runtime folder (no symlinks!)
// we know the dotnet executable will be two folders up in the install root.
const runtimeFolderPath = matchingRuntime.Path;
const installFolder = path.dirname(path.dirname(runtimeFolderPath));
const dotnetExecutablePath = path.join(installFolder, this.getDotnetExecutableName());
if (!existsSync(dotnetExecutablePath)) {
throw new Error(
`dotnet executable path does not exist: ${dotnetExecutablePath}, dotnet installation may be corrupt.`
);
}

this.channel.appendLine(`Using dotnet configured on PATH`);
Copy link
Member

Choose a reason for hiding this comment

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

Does this message need to change? Or should it now log the path we created?

Copy link
Member Author

Choose a reason for hiding this comment

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

the path will be logged in the caller anway, and we're still using the dotnet from the path, so think this is fine.


// If dotnet is just a symlink, resolve it to the actual executable so
// callers will be able to get the actual directory containing the exe.
return await realpath(path);
return dotnetExecutablePath;
} catch (e) {
this.channel.appendLine(
'Failed to find dotnet info from path, falling back to acquire runtime via ms-dotnettools.vscode-dotnet-runtime'
Expand Down Expand Up @@ -236,4 +236,8 @@ export class DotnetRuntimeExtensionResolver implements IHostExecutableResolver {
throw new Error(`Unknown extension target platform: ${targetPlatform}`);
}
}

private getDotnetExecutableName(): string {
return this.platformInfo.isWindows() ? 'dotnet.exe' : 'dotnet';
}
}
7 changes: 6 additions & 1 deletion src/shared/utils/dotnetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as semver from 'semver';

type RuntimeVersionMap = { [runtime: string]: semver.SemVer[] };
type RuntimeVersionMap = { [runtime: string]: RuntimeInfo[] };
export interface DotnetInfo {
CliPath?: string;
FullInfo: string;
Expand All @@ -15,3 +15,8 @@ export interface DotnetInfo {
Architecture?: string;
Runtimes: RuntimeVersionMap;
}

export interface RuntimeInfo {
Version: semver.SemVer;
Path: string;
}
16 changes: 12 additions & 4 deletions src/shared/utils/getDotnetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as semver from 'semver';
import { join } from 'path';
import { execChildProcess } from '../../common';
import { CoreClrDebugUtil } from '../../coreclrDebug/util';
import { DotnetInfo } from './dotnetInfo';
import { DotnetInfo, RuntimeInfo } from './dotnetInfo';
import { EOL } from 'os';

// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
Expand Down Expand Up @@ -69,7 +69,7 @@ async function parseDotnetInfo(dotnetInfo: string, dotnetExecutablePath: string
}
}

const runtimeVersions: { [runtime: string]: semver.SemVer[] } = {};
const runtimeVersions: { [runtime: string]: RuntimeInfo[] } = {};
const listRuntimes = await execChildProcess('dotnet --list-runtimes', process.cwd(), process.env);
lines = listRuntimes.split(/\r?\n/);
for (const line of lines) {
Expand All @@ -78,9 +78,17 @@ async function parseDotnetInfo(dotnetInfo: string, dotnetExecutablePath: string
const runtime = match[1];
const runtimeVersion = match[2];
if (runtime in runtimeVersions) {
runtimeVersions[runtime].push(semver.parse(runtimeVersion)!);
runtimeVersions[runtime].push({
Version: semver.parse(runtimeVersion)!,
Path: match[3],
});
} else {
runtimeVersions[runtime] = [semver.parse(runtimeVersion)!];
runtimeVersions[runtime] = [
{
Version: semver.parse(runtimeVersion)!,
Path: match[3],
},
];
}
}
}
Expand Down