Skip to content

Run Selection/Line in already active python interpreter terminal rather than opening a new terminal #24960

Open

Description

@omkarchandra

Hi, my workflow setup allows me to work on SLURM allocated resources on an already configured terminal for debugging code.

So when I send code to the terminal, it triggers the opening of a new terminal python interpreter instead of running the code in the already opened terminal.

It will be very useful for me to have run the code on already configured python terminal.

Activity

  1. added
    bugIssue identified by VS Code Team member as probable bug
    and removed
    feature-requestRequest for new features or functionality
    on Apr 16, 2025
  2. anthonykim1 commented on Apr 16, 2025

    @anthonykim1

    thanks for filing this, this might be a bug or feature request. I would need to take a closer look.

  3. s-kai273 commented on May 10, 2025

    @s-kai273

    While investigating the behavior of the Run Selection/Line in Python Terminal command in the source code, I found that the existing terminal is reused to execute the selected code only if the terminal was originally opened by the same command.

    However, if the terminal was opened manually, a new terminal will be launched instead.

  4. s-kai273 commented on May 10, 2025

    @s-kai273

    omkarchandra
    I'm not sure about the exact details of how you launch the SLURM allocation, but if it's initiated from a VSCode terminal, you can achieve your desired workflow by following these steps:

    1. Open a terminal by running the Run Selection/Line in Python Terminal command (you can use any arbitrary code).

    2. Exit the Python interpreter by running exit().

    3. From the same terminal, perform the necessary steps to launch the SLURM environment.

    4. Start the Python interpreter within the SLURM environment.

    5. Now, use Run Selection/Line in Python Terminal again to run your selected code in the already configured terminal.

    I hope these tips would help you.

  5. s-kai273 commented on May 10, 2025

    @s-kai273

    However, I believe this behavior is definitely confusing.
    It would be better to improve it so that the selected code is executed in the existing terminal, regardless of how the terminal was opened.

  6. omkarchandra commented on May 16, 2025

    @omkarchandra
    Author

    Yes, thank you for the workaround recommendation.
    I wish a patch-up would be done to correct this behavior.

  7. s-kai273 commented on May 17, 2025

    @s-kai273

    Anthony Kim (@anthonykim1)
    The current behavior is implemented by the initializeRepl method in the TerminalCodeExecutionProvider class:

    public async initializeRepl(resource: Resource) {
        const terminalService = this.getTerminalService(resource);
        // Case of using the existing terminal
        if (this.replActive && (await this.replActive)) {
            await terminalService.show();
            return;
        }
        // Case of using a new terminal
        ...
    }
    

    Currently, this.replActive is initialized by the previous “Run Selection/Line” execution.
    This means the existing terminal is reused only if it was originally created by the “Run Selection/Line” command.

    However, I believe the behavior should follow these rules:

    1. Prefer using an existing terminal (only create a new one if none is available).
    2. If a Python REPL is not active in the selected terminal, it should be initialized.
    3. Then, execute the code in that terminal.

    If this approach sounds reasonable, may I go ahead and modify the code and open a PR accordingly?

  8. omkarchandra commented on May 17, 2025

    @omkarchandra
    Author

    Thank you for digging into this and suggesting a solution!
    Yes, that should be the right behaviour. Please go ahead and open a PR.
    This will make my workflow so much smoother. Thank you!

  9. anthonykim1 commented on Jun 10, 2025

    @anthonykim1

    Thanks all for your feedback!
    Shin Kinoshita (@s-kai273) And Yes absolutely :)
    Feel free to give this a try, and you can directly assign me as a reviewer.

  10. s-kai273 commented on Jun 10, 2025

    @s-kai273

    Anthony Kim (@anthonykim1)
    OK, thank you!
    I'll try it.

  11. s-kai273 commented on Jun 13, 2025

    @s-kai273

    Anthony Kim (@anthonykim1)
    Hi, I roughly implemented this feature on #25178.
    Please review it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-terminalbugIssue identified by VS Code Team member as probable bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions