@@ -72,6 +72,7 @@ import { SysPythonManager } from './managers/builtin/sysPythonManager';
72
72
import {
73
73
createNativePythonFinder ,
74
74
getNativePythonToolsPath ,
75
+ NativeEnvInfo ,
75
76
NativePythonFinder ,
76
77
} from './managers/common/nativePythonFinder' ;
77
78
import { IDisposable } from './managers/common/types' ;
@@ -566,6 +567,8 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
566
567
shellStartupVarsMgr . initialize ( ) ,
567
568
] ) ;
568
569
570
+ resolveDefaultInterpreter ( nativeFinder , envManagers , api ) ;
571
+
569
572
sendTelemetryEvent ( EventNames . EXTENSION_MANAGER_REGISTRATION_DURATION , start . elapsedTime ) ;
570
573
await terminalManager . initialize ( api ) ;
571
574
sendManagerSelectionTelemetry ( projectManager ) ;
@@ -589,6 +592,64 @@ export async function disposeAll(disposables: IDisposable[]): Promise<void> {
589
592
) ;
590
593
}
591
594
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
+
592
653
export async function deactivate ( context : ExtensionContext ) {
593
654
await disposeAll ( context . subscriptions ) ;
594
655
context . subscriptions . length = 0 ; // Clear subscriptions to prevent memory leaks
0 commit comments