@@ -9,7 +9,7 @@ import { PVSC_EXTENSION_ID } from '../common/constants';
99import { showQuickPick } from '../common/vscodeApis/windowApis' ;
1010import { getWorkspaceFolders , onDidCloseNotebookDocument } from '../common/vscodeApis/workspaceApis' ;
1111import { PythonEnvironment } from '../pythonEnvironments/info' ;
12- import { createPythonServer , PythonServer } from './pythonServer' ;
12+ import { createPythonServer , createPythonServerEnvExt , PythonServer } from './pythonServer' ;
1313import { executeNotebookCell , openInteractiveREPL , selectNotebookKernel } from './replCommandHandler' ;
1414import { createReplController } from './replController' ;
1515import { EventName } from '../telemetry/constants' ;
@@ -18,6 +18,8 @@ import { VariablesProvider } from './variables/variablesProvider';
1818import { VariableRequester } from './variables/variableRequester' ;
1919import { getTabNameForUri } from './replUtils' ;
2020import { getWorkspaceStateValue , updateWorkspaceStateValue } from '../common/persistentState' ;
21+ import { getEnvironment , useEnvExtension } from '../envExt/api.internal' ;
22+ import { traceError , traceVerbose } from '../logging' ;
2123
2224export const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri' ;
2325let nativeRepl : NativeRepl | undefined ;
@@ -29,6 +31,8 @@ export class NativeRepl implements Disposable {
2931
3032 private interpreter ! : PythonEnvironment ;
3133
34+ private resourceUri : Uri | undefined ;
35+
3236 private disposables : Disposable [ ] = [ ] ;
3337
3438 private replController ! : NotebookController ;
@@ -42,12 +46,38 @@ export class NativeRepl implements Disposable {
4246 this . watchNotebookClosed ( ) ;
4347 }
4448
49+ /**
50+ * Get the interpreter path associated with this REPL instance.
51+ * Used to detect when interpreter has changed and REPL needs to be recreated.
52+ */
53+ public get interpreterPath ( ) : string {
54+ return this . interpreter . path ;
55+ }
56+
4557 // Static async factory method to handle asynchronous initialization
46- public static async create ( interpreter : PythonEnvironment ) : Promise < NativeRepl > {
58+ public static async create ( interpreter : PythonEnvironment , resource ?: Uri ) : Promise < NativeRepl > {
4759 const nativeRepl = new NativeRepl ( ) ;
4860 nativeRepl . interpreter = interpreter ;
61+ nativeRepl . resourceUri = resource ;
4962 await nativeRepl . setReplDirectory ( ) ;
50- nativeRepl . pythonServer = createPythonServer ( [ interpreter . path as string ] , nativeRepl . cwd ) ;
63+
64+ // Use env extension's runInBackground if available for proper environment handling
65+ if ( useEnvExtension ( ) ) {
66+ const pythonEnv = await getEnvironment ( resource ) ;
67+ if ( pythonEnv ) {
68+ traceVerbose (
69+ `Creating REPL server using environment extension for: ${ pythonEnv . execInfo . run . executable } ` ,
70+ ) ;
71+ nativeRepl . pythonServer = await createPythonServerEnvExt ( pythonEnv , nativeRepl . cwd ) ;
72+ } else {
73+ traceError ( 'Failed to get environment from env extension, falling back to direct spawn' ) ;
74+ nativeRepl . pythonServer = createPythonServer ( [ interpreter . path as string ] , nativeRepl . cwd ) ;
75+ }
76+ } else {
77+ traceVerbose ( `Creating REPL server using direct spawn for: ${ interpreter . path } ` ) ;
78+ nativeRepl . pythonServer = createPythonServer ( [ interpreter . path as string ] , nativeRepl . cwd ) ;
79+ }
80+
5181 nativeRepl . setReplController ( ) ;
5282
5383 return nativeRepl ;
@@ -69,7 +99,19 @@ export class NativeRepl implements Disposable {
6999 this . newReplSession = true ;
70100 await updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
71101 this . pythonServer . dispose ( ) ;
72- this . pythonServer = createPythonServer ( [ this . interpreter . path as string ] , this . cwd ) ;
102+
103+ // Recreate Python server - use env extension if available
104+ if ( useEnvExtension ( ) ) {
105+ const pythonEnv = await getEnvironment ( this . resourceUri ) ;
106+ if ( pythonEnv ) {
107+ this . pythonServer = await createPythonServerEnvExt ( pythonEnv , this . cwd ) ;
108+ } else {
109+ this . pythonServer = createPythonServer ( [ this . interpreter . path as string ] , this . cwd ) ;
110+ }
111+ } else {
112+ this . pythonServer = createPythonServer ( [ this . interpreter . path as string ] , this . cwd ) ;
113+ }
114+
73115 this . disposables . push ( this . pythonServer ) ;
74116 if ( this . replController ) {
75117 this . replController . dispose ( ) ;
@@ -118,7 +160,12 @@ export class NativeRepl implements Disposable {
118160 */
119161 public setReplController ( ) : NotebookController {
120162 if ( ! this . replController ) {
121- this . replController = createReplController ( this . interpreter ! . path , this . disposables , this . cwd ) ;
163+ this . replController = createReplController (
164+ this . interpreter ! . path ,
165+ this . disposables ,
166+ this . cwd ,
167+ this . pythonServer ,
168+ ) ;
122169 this . replController . variableProvider = new VariablesProvider (
123170 new VariableRequester ( this . pythonServer ) ,
124171 ( ) => this . notebookDocument ,
@@ -183,13 +230,28 @@ export class NativeRepl implements Disposable {
183230}
184231
185232/**
186- * Get Singleton Native REPL Instance
233+ * Get Singleton Native REPL Instance.
234+ * Recreates the REPL if the interpreter has changed (e.g., when using environment extension).
187235 * @param interpreter
236+ * @param disposables
237+ * @param resource - Optional resource URI used to get the environment from the env extension
188238 * @returns Native REPL instance
189239 */
190- export async function getNativeRepl ( interpreter : PythonEnvironment , disposables : Disposable [ ] ) : Promise < NativeRepl > {
240+ export async function getNativeRepl (
241+ interpreter : PythonEnvironment ,
242+ disposables : Disposable [ ] ,
243+ resource ?: Uri ,
244+ ) : Promise < NativeRepl > {
245+ // Check if interpreter has changed - if so, dispose and recreate the REPL
246+ // This serves as a fallback in case the event listener didn't catch the change
247+ if ( nativeRepl && nativeRepl . interpreterPath !== interpreter . path ) {
248+ traceVerbose ( `Interpreter changed to ${ interpreter . path } , disposing Native REPL` ) ;
249+ nativeRepl . dispose ( ) ;
250+ nativeRepl = undefined ;
251+ }
252+
191253 if ( ! nativeRepl ) {
192- nativeRepl = await NativeRepl . create ( interpreter ) ;
254+ nativeRepl = await NativeRepl . create ( interpreter , resource ) ;
193255 disposables . push ( nativeRepl ) ;
194256 }
195257 if ( nativeRepl && nativeRepl . newReplSession ) {
0 commit comments