@@ -72,6 +72,7 @@ import { SysPythonManager } from './managers/builtin/sysPythonManager';
7272import {
7373 createNativePythonFinder ,
7474 getNativePythonToolsPath ,
75+ NativeEnvInfo ,
7576 NativePythonFinder ,
7677} from './managers/common/nativePythonFinder' ;
7778import { 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,75 @@ 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+ traceInfo ( `resolveDefaultInterpreter setting exists; found defaultEnvManager: ${ defaultManager } ` ) ;
618+ if ( ! defaultManager || defaultManager === 'ms-python.python:venv' ) {
619+ try {
620+ const resolved : NativeEnvInfo = await nativeFinder . resolve ( defaultInterpreterPath ) ;
621+ if ( resolved && resolved . executable ) {
622+ const resolvedEnv = await api . resolveEnvironment ( Uri . file ( resolved . executable ) ) ;
623+ traceInfo ( `[resolveDefaultInterpreter] API resolved environment: ${ JSON . stringify ( resolvedEnv ) } ` ) ;
624+
625+ let findEnvManager = envManagers . managers . find ( ( m ) => m . id === resolvedEnv ?. envId . managerId ) ;
626+ if ( ! findEnvManager ) {
627+ findEnvManager = envManagers . managers . find ( ( m ) => m . id === 'ms-python.python:system' ) ;
628+ }
629+ if ( resolvedEnv ) {
630+ const newEnv : PythonEnvironment = {
631+ envId : {
632+ id : resolvedEnv ?. envId . id ,
633+ managerId : resolvedEnv ?. envId . managerId ?? '' ,
634+ } ,
635+ name : 'defaultInterpreterPath: ' + ( resolved . version ?? '' ) ,
636+ displayName : 'defaultInterpreterPath: ' + ( resolved . version ?? '' ) ,
637+ version : resolved . version ?? '' ,
638+ displayPath : defaultInterpreterPath ?? '' ,
639+ environmentPath : defaultInterpreterPath ? Uri . file ( defaultInterpreterPath ) : Uri . file ( '' ) ,
640+ sysPrefix : resolved . arch ?? '' ,
641+ execInfo : {
642+ run : {
643+ executable : defaultInterpreterPath ?? '' ,
644+ } ,
645+ } ,
646+ } ;
647+ if ( workspace . workspaceFolders ?. [ 0 ] && findEnvManager ) {
648+ traceInfo (
649+ `[resolveDefaultInterpreter] Setting environment for workspace: ${ workspace . workspaceFolders [ 0 ] . uri . fsPath } ` ,
650+ ) ;
651+ await api . setEnvironment ( workspace . workspaceFolders [ 0 ] . uri , newEnv ) ;
652+ }
653+ }
654+ } else {
655+ traceWarn (
656+ `[resolveDefaultInterpreter] NativeFinder did not resolve an executable for path: ${ defaultInterpreterPath } ` ,
657+ ) ;
658+ }
659+ } catch ( err ) {
660+ traceError ( `[resolveDefaultInterpreter] Error resolving default interpreter: ${ err } ` ) ;
661+ }
662+ }
663+ }
664+ }
665+
594666export async function deactivate ( context : ExtensionContext ) {
595667 await disposeAll ( context . subscriptions ) ;
596668 context . subscriptions . length = 0 ; // Clear subscriptions to prevent memory leaks
0 commit comments