Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/extension/common/executablePath.ts
Original file line number Diff line number Diff line change
@@ -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 `<path>.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;
}
16 changes: 13 additions & 3 deletions src/extension/common/legacyPython.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -163,8 +164,17 @@ export async function legacyResolveEnvironment(
): Promise<ResolvedEnvironment | undefined> {
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;
}

Expand Down Expand Up @@ -197,7 +207,7 @@ export async function legacyGetInterpreterDetails(resource?: Uri): Promise<Legac
traceLog(
`legacyGetInterpreterDetails: executable='${environment.executable.uri.fsPath}' resource='${resource?.fsPath}'`,
);
return { path: [environment?.executable.uri.fsPath], resource };
return { path: [restoreWindowsExeSuffix(environment.executable.uri.fsPath)], resource };
}
traceLog('legacyGetInterpreterDetails: No executable found');
return { path: undefined, resource };
Expand Down
29 changes: 29 additions & 0 deletions src/test/unittest/common/executablePath.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { expect } from 'chai';
import { restoreWindowsExeSuffix } from '../../../extension/common/executablePath';

suite('restoreWindowsExeSuffix', () => {
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);
});
});