Skip to content

Commit 70ecc3d

Browse files
committed
add resolution for default interpreter and respect setting
1 parent 0caad98 commit 70ecc3d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/extension.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import { SysPythonManager } from './managers/builtin/sysPythonManager';
7272
import {
7373
createNativePythonFinder,
7474
getNativePythonToolsPath,
75+
NativeEnvInfo,
7576
NativePythonFinder,
7677
} from './managers/common/nativePythonFinder';
7778
import { IDisposable } from './managers/common/types';
@@ -566,6 +567,8 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
566567
shellStartupVarsMgr.initialize(),
567568
]);
568569

570+
resolveDefaultInterpreter(nativeFinder, envManagers, api);
571+
569572
sendTelemetryEvent(EventNames.EXTENSION_MANAGER_REGISTRATION_DURATION, start.elapsedTime);
570573
await terminalManager.initialize(api);
571574
sendManagerSelectionTelemetry(projectManager);
@@ -589,6 +592,64 @@ export async function disposeAll(disposables: IDisposable[]): Promise<void> {
589592
);
590593
}
591594

595+
/**
596+
* Resolves and sets the default Python interpreter for the workspace based on the
597+
* 'python.defaultInterpreterPath' setting and the selected environment manager.
598+
* If the setting is present and no default environment manager is set (or is venv),
599+
* attempts to resolve the interpreter path using the native finder, then creates and
600+
* sets a PythonEnvironment object for the workspace.
601+
*
602+
* @param nativeFinder - The NativePythonFinder instance used to resolve interpreter paths.
603+
* @param envManagers - The EnvironmentManagers instance containing all registered managers.
604+
* @param api - The PythonEnvironmentApi for environment resolution and setting.
605+
*/
606+
async function resolveDefaultInterpreter(
607+
nativeFinder: NativePythonFinder,
608+
envManagers: EnvironmentManagers,
609+
api: PythonEnvironmentApi,
610+
) {
611+
const defaultInterpreterPath = getConfiguration('python').get<string>('defaultInterpreterPath');
612+
613+
if (defaultInterpreterPath) {
614+
const defaultManager = getConfiguration('python-envs').get<string>('defaultEnvManager', 'undefined');
615+
if (!defaultManager || defaultManager === 'ms-python.python:venv') {
616+
// if user has defaultInterpreterPath and no defaultEnvManager set then resolve the defaultInterpreterPath setting
617+
const resolved: NativeEnvInfo = await nativeFinder.resolve(defaultInterpreterPath);
618+
if (resolved && resolved.executable) {
619+
const resolvedEnv = await api.resolveEnvironment(Uri.file(resolved.executable));
620+
621+
let findEnvManager = envManagers.managers.find((m) => m.id === resolvedEnv?.envId.managerId);
622+
623+
if (!findEnvManager) {
624+
findEnvManager = envManagers.managers.find((m) => m.id === 'ms-python.python:system');
625+
}
626+
if (resolvedEnv) {
627+
const newEnv: PythonEnvironment = {
628+
envId: {
629+
id: resolvedEnv?.envId.id,
630+
managerId: resolvedEnv?.envId.managerId ?? '',
631+
},
632+
name: 'defaultInterpreterPath: ' + (resolved.version ?? ''),
633+
displayName: 'defaultInterpreterPath: ' + (resolved.version ?? ''),
634+
version: resolved.version ?? '',
635+
displayPath: defaultInterpreterPath ?? '',
636+
environmentPath: defaultInterpreterPath ? Uri.file(defaultInterpreterPath) : Uri.file(''),
637+
sysPrefix: resolved.arch ?? '',
638+
execInfo: {
639+
run: {
640+
executable: defaultInterpreterPath ?? '',
641+
},
642+
},
643+
};
644+
if (workspace.workspaceFolders?.[0] && findEnvManager) {
645+
await api.setEnvironment(workspace.workspaceFolders[0].uri, newEnv);
646+
}
647+
}
648+
}
649+
}
650+
}
651+
}
652+
592653
export async function deactivate(context: ExtensionContext) {
593654
await disposeAll(context.subscriptions);
594655
context.subscriptions.length = 0; // Clear subscriptions to prevent memory leaks

0 commit comments

Comments
 (0)