Skip to content

Commit 88a9c6d

Browse files
committed
add resolution for default interpreter and respect setting
1 parent 8ee4040 commit 88a9c6d

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

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';
@@ -568,6 +569,8 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
568569
shellStartupVarsMgr.initialize(),
569570
]);
570571

572+
resolveDefaultInterpreter(nativeFinder, envManagers, api);
573+
571574
sendTelemetryEvent(EventNames.EXTENSION_MANAGER_REGISTRATION_DURATION, start.elapsedTime);
572575
await terminalManager.initialize(api);
573576
sendManagerSelectionTelemetry(projectManager);
@@ -591,6 +594,64 @@ export async function disposeAll(disposables: IDisposable[]): Promise<void> {
591594
);
592595
}
593596

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

0 commit comments

Comments
 (0)