Skip to content

Commit

Permalink
Improve error details when dotnet --info fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Aug 20, 2023
1 parent 061fc6d commit 99f9b44
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/shared/utils/getDotnetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,54 @@ import { join } from 'path';
import { execChildProcess } from '../../common';
import { CoreClrDebugUtil } from '../../coreclrDebug/util';
import { DotnetInfo } from './dotnetInfo';
import { EOL } from 'os';

// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInfo> {
const dotnetExecutablePath = getDotNetExecutablePath(dotNetCliPaths);

const data = await runDotnetInfo(dotnetExecutablePath);
const dotnetInfo = await parseDotnetInfo(data, dotnetExecutablePath);
return dotnetInfo;
}

export function getDotNetExecutablePath(dotNetCliPaths: string[]): string | undefined {
const dotnetExeName = `dotnet${CoreClrDebugUtil.getPlatformExeExtension()}`;
let dotnetExecutablePath: string | undefined;

for (const dotnetPath of dotNetCliPaths) {
const dotnetFullPath = join(dotnetPath, dotnetExeName);
if (CoreClrDebugUtil.existsSync(dotnetFullPath)) {
dotnetExecutablePath = dotnetFullPath;
break;
}
}
return dotnetExecutablePath;
}

async function runDotnetInfo(dotnetExecutablePath: string | undefined): Promise<string> {
try {
const env = {
...process.env,
DOTNET_CLI_UI_LANGUAGE: 'en-US',
};
const data = await execChildProcess(`${dotnetExecutablePath ?? 'dotnet'} --info`, process.cwd(), env);
return data;
} catch (error) {
throw new Error(`Error running dotnet --info: ${error}`);
}
}

async function parseDotnetInfo(dotnetInfo: string, dotnetExecutablePath: string | undefined): Promise<DotnetInfo> {
try {
const cliPath = dotnetExecutablePath;
const fullInfo = data;
const fullInfo = dotnetInfo;

let version: string | undefined;
let runtimeId: string | undefined;
let architecture: string | undefined;

let lines = data.replace(/\r/gm, '').split('\n');
let lines = dotnetInfo.replace(/\r/gm, '').split('\n');
for (const line of lines) {
let match: RegExpMatchArray | null;
if ((match = /^\s*Version:\s*([^\s].*)$/.exec(line))) {
Expand Down Expand Up @@ -68,22 +96,8 @@ export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInf
}

throw new Error('Failed to parse dotnet version information');
} catch {
// something went wrong with spawning 'dotnet --info'
throw new Error('A valid dotnet installation could not be found');
}
}

export function getDotNetExecutablePath(dotNetCliPaths: string[]): string | undefined {
const dotnetExeName = `dotnet${CoreClrDebugUtil.getPlatformExeExtension()}`;
let dotnetExecutablePath: string | undefined;

for (const dotnetPath of dotNetCliPaths) {
const dotnetFullPath = join(dotnetPath, dotnetExeName);
if (CoreClrDebugUtil.existsSync(dotnetFullPath)) {
dotnetExecutablePath = dotnetFullPath;
break;
}
} catch (error) {
const message = error instanceof Error ? error.message : `${error}`;
throw new Error(`Error parsing dotnet --info: ${message}, raw info was:${EOL}${dotnetInfo}`);
}
return dotnetExecutablePath;
}

0 comments on commit 99f9b44

Please sign in to comment.