diff --git a/src/extension/common/executablePath.ts b/src/extension/common/executablePath.ts new file mode 100644 index 00000000..8062f4d0 --- /dev/null +++ b/src/extension/common/executablePath.ts @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import * as fs from 'fs'; +import * as path from 'path'; + +/** + * On Windows, the legacy Python extension API can return an interpreter path without the + * `.exe` suffix (e.g. `.venv\Scripts\python`), which does not exist on disk. If the given path + * has no extension, does not exist, and `.exe` does exist, return the `.exe` path. + * Otherwise return the path unchanged. + */ +export function restoreWindowsExeSuffix( + executable: string, + platform: NodeJS.Platform = process.platform, + exists: (p: string) => boolean = fs.existsSync, +): string { + if (platform !== 'win32' || path.extname(executable) !== '' || exists(executable)) { + return executable; + } + const withExe = `${executable}.exe`; + return exists(withExe) ? withExe : executable; +} diff --git a/src/extension/common/legacyPython.ts b/src/extension/common/legacyPython.ts index f8d01526..820dcd2f 100644 --- a/src/extension/common/legacyPython.ts +++ b/src/extension/common/legacyPython.ts @@ -14,6 +14,7 @@ import { import { EventEmitter, extensions, Uri, Disposable, Extension } from 'vscode'; import { createDeferred } from './utils/async'; import { traceError, traceLog } from './log/logging'; +import { restoreWindowsExeSuffix } from './executablePath'; /** * Interface for the Python extension API. @@ -163,8 +164,17 @@ export async function legacyResolveEnvironment( ): Promise { const api = await legacyGetPythonExtensionEnviromentAPI(); traceLog(`legacyResolveEnvironment: Resolving environment '${typeof env === 'string' ? env : (env as any).path}'`); - const resolved = api.environments.resolveEnvironment(env); - resolved.then((r) => traceLog(`legacyResolveEnvironment: Resolved executable='${r?.executable.uri?.fsPath}'`)); + const resolved = await api.environments.resolveEnvironment(env); + const execUri = resolved?.executable.uri; + if (resolved && execUri) { + // The legacy API can drop the `.exe` suffix on Windows, so restore it when the file exists. + const fixedPath = restoreWindowsExeSuffix(execUri.fsPath); + if (fixedPath !== execUri.fsPath) { + traceLog(`legacyResolveEnvironment: Restored '.exe' suffix, executable='${fixedPath}'`); + return { ...resolved, executable: { ...resolved.executable, uri: Uri.file(fixedPath) } }; + } + } + traceLog(`legacyResolveEnvironment: Resolved executable='${execUri?.fsPath}'`); return resolved; } @@ -197,7 +207,7 @@ export async function legacyGetInterpreterDetails(resource?: Uri): Promise { + const noExe = 'C:/proj/.venv/Scripts/python'; + const onlyExe = (p: string) => p === `${noExe}.exe`; + + test('restores .exe on Windows when only the .exe file exists', () => { + expect(restoreWindowsExeSuffix(noExe, 'win32', onlyExe)).to.equal(`${noExe}.exe`); + }); + test('keeps path when it exists as given', () => { + expect(restoreWindowsExeSuffix(noExe, 'win32', () => true)).to.equal(noExe); + }); + test('keeps path that already has an extension', () => { + expect(restoreWindowsExeSuffix(`${noExe}.exe`, 'win32', () => false)).to.equal(`${noExe}.exe`); + }); + test('keeps path when .exe does not exist', () => { + expect(restoreWindowsExeSuffix(noExe, 'win32', () => false)).to.equal(noExe); + }); + test('does nothing on non-Windows platforms', () => { + expect(restoreWindowsExeSuffix('/proj/.venv/bin/python', 'linux', () => true)).to.equal( + '/proj/.venv/bin/python', + ); + expect(restoreWindowsExeSuffix(noExe, 'darwin', onlyExe)).to.equal(noExe); + }); +});