Skip to content

Fix "Run Python File" executing in Python REPL instead of shell #25160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 10, 2025

Problem

When a user has a Python REPL active in a terminal (after running python) and then clicks "Run Python File", the command gets sent to the Python interpreter instead of creating a new shell terminal. This causes the file execution command to be typed into the Python REPL rather than executed at the shell level.

Root Cause

The terminal service was reusing existing terminals without checking if they were currently in Python REPL mode. The executeFile method would send commands to whatever terminal was available, regardless of its current state.

Solution

Modified TerminalCodeExecutionProvider.executeFile() to detect when a terminal is in Python REPL mode and force creation of a new terminal for file execution in that case.

Key Changes

  1. Added REPL state detection in shouldForceNewTerminalForFileExecution() method:

    • Checks if replActive is true (indicating Python REPL is running)
    • Forces newTerminalPerFile: true when REPL is active
    • Preserves existing behavior when not in REPL mode
  2. Modified executeFile() method to use the REPL detection logic before terminal operations

  3. Added comprehensive test to verify the fix works correctly

Example Scenario

Before:

$ python
>>> # User clicks "Run Python File" button
>>> python /path/to/script.py  # This gets typed into Python REPL (wrong!)

After:

$ python
>>> # User clicks "Run Python File" button
# New terminal opens automatically:
$ python /path/to/script.py  # Executes in new shell terminal (correct!)

Compatibility

  • ✅ Zero breaking changes - existing behavior preserved when not in REPL
  • ✅ Respects existing newTerminalPerFile setting if already enabled
  • ✅ Automatically applies to Django Shell execution (inherits from base class)
  • ✅ All existing tests pass + new test coverage added

Fixes #25159.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] "Run Python File" while python interpreter is in terminal will type the run command into the python interpreter Fix "Run Python File" executing in Python REPL instead of shell Jun 10, 2025
@Copilot Copilot AI requested a review from anthonykim1 June 10, 2025 04:50
Copilot finished work on behalf of anthonykim1 June 10, 2025 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Run Python File" while python interpreter is in terminal will type the run command into the python interpreter
2 participants