diff --git a/src/shared/utils/getDotnetInfo.ts b/src/shared/utils/getDotnetInfo.ts index a8cd59361..1ecc0fce0 100644 --- a/src/shared/utils/getDotnetInfo.ts +++ b/src/shared/utils/getDotnetInfo.ts @@ -8,32 +8,55 @@ import { join } from 'path'; import { execChildProcess } from '../../common'; import { CoreClrDebugUtil } from '../../coreclrDebug/util'; import { DotnetInfo } from './dotnetInfo'; - -let _dotnetInfo: DotnetInfo | undefined; +import { EOL } from 'os'; // This function calls `dotnet --info` and returns the result as a DotnetInfo object. export async function getDotnetInfo(dotNetCliPaths: string[]): Promise { - if (_dotnetInfo !== undefined) { - return _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 { 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) { + const message = error instanceof Error ? error.message : `${error}`; + throw new Error(`Error running dotnet --info: ${message}`); + } +} +async function parseDotnetInfo(dotnetInfo: string, dotnetExecutablePath: string | undefined): Promise { + 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))) { @@ -62,7 +85,7 @@ export async function getDotnetInfo(dotNetCliPaths: string[]): Promise