@@ -38,12 +38,15 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
3838 ) { }
3939
4040 public async executeFile ( file : Uri , options ?: { newTerminalPerFile : boolean } ) {
41- await this . setCwdForFileExecution ( file , options ) ;
41+ // If we're currently in a REPL, force creation of a new terminal for file execution
42+ const finalOptions = await this . shouldForceNewTerminalForFileExecution ( options ) ;
43+
44+ await this . setCwdForFileExecution ( file , finalOptions ) ;
4245 const { command, args } = await this . getExecuteFileArgs ( file , [
4346 file . fsPath . fileToCommandArgumentForPythonExt ( ) ,
4447 ] ) ;
4548
46- await this . getTerminalService ( file , options ) . sendCommand ( command , args ) ;
49+ await this . getTerminalService ( file , finalOptions ) . sendCommand ( command , args ) ;
4750 }
4851
4952 public async execute ( code : string , resource ?: Uri ) : Promise < void > {
@@ -124,6 +127,28 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
124127 public async getExecuteFileArgs ( resource ?: Uri , executeArgs : string [ ] = [ ] ) : Promise < PythonExecInfo > {
125128 return this . getExecutableInfo ( resource , executeArgs ) ;
126129 }
130+
131+ /**
132+ * Check if we should force creation of a new terminal for file execution.
133+ * This is needed when the current terminal is in Python REPL mode to avoid
134+ * sending the file execution command to the Python interpreter instead of the shell.
135+ */
136+ private async shouldForceNewTerminalForFileExecution (
137+ options ?: { newTerminalPerFile : boolean }
138+ ) : Promise < { newTerminalPerFile : boolean } > {
139+ // If already set to create new terminal, respect that
140+ if ( options ?. newTerminalPerFile ) {
141+ return { newTerminalPerFile : true } ;
142+ }
143+
144+ // If we have an active REPL, force creation of a new terminal
145+ if ( this . replActive && ( await this . replActive ) ) {
146+ return { newTerminalPerFile : true } ;
147+ }
148+
149+ // Otherwise, use original options or default to false
150+ return options || { newTerminalPerFile : false } ;
151+ }
127152 private getTerminalService ( resource : Resource , options ?: { newTerminalPerFile : boolean } ) : ITerminalService {
128153 return this . terminalServiceFactory . getTerminalService ( {
129154 resource,
0 commit comments