Skip to content
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

[Bug] Code Runner-created terminal is not reused after a terminal in VSCode is closed #1126

Open
the-chenergy opened this issue Apr 9, 2024 · 2 comments

Comments

@the-chenergy
Copy link

  • VS Code Version: 1.88.0
  • OS Version: macOS 14.4.1 (23E224)
  • Code Runner Version: 0.12.2

Describe the bug
Normally, when I trigger a code run (using the "Run Code" button or the Ctrl+Alt+N shortcut), if the code-runner.runInTerminal setting is set to true, then Code Runner will create a new terminal window inside the Terminal panel only if there isn't one created by Code Runner yet, and it will reuse the same terminal for future code runs. Reusing existing terminal is a great feature because, without it, I would end up with tons of terminals I don't want.

However, if there is already a Code Runner-created terminal and I close an existing one that's not Code Runner-created, then the next time I run code, Code Runner will fail to reuse the existing one and create a new one.

To Reproduce
Steps to reproduce the behavior:

  1. Open VSCode and open some runnable file (e.g. Python file)
  2. Run code (by either clicking the "Run Code" button on editor's top-right corner or pressing Ctrl+Alt+N)
    • Observe that Code Runner creates a new terminal window in the Terminal panel to run the opened code, which is expected
  3. Create a new terminal window manually (by clicking the plus button on Terminal panel's top-right corner)
  4. Run code again
    • Observe that Code Runner reuses the old terminal window that it created in step 2, which is also expected
  5. Close the newest terminal created in step 3 (by selecting the new terminal in the tab/selector view, which defaults on the right side of the Terminal panel, and clicking the bin icon next to the tab item)
  6. Run code again
    • Observe that Code Runner fails to recognize the existing terminal that it created in step 2 and creates a new terminal, which is unexpected

Actual behavior
After any terminal is closed, Code Runner creates a new terminal the next time I run code

Expected behavior
Code Runner should always reuse the terminal it created, if there is one, regardless what other windows I created or closed

Video demo

Observe that the last time I run code, Code Runner creates a new terminal when the one it created before still exists.

demo.mp4
@the-chenergy
Copy link
Author

I think the fix should be around here:

vscode.window.onDidCloseTerminal(() => {
codeManager.onDidCloseTerminal();
});

VSCode's API function onDidCloseTerminal seems to have one argument in the callback, the terminal being closed. I found an example usage of onDidCloseTerminal here in VSCode's tests.

So perhaps we can just check if the terminal that's closing matches the one recorded in CodeManager._terminal before setting that to null:

public onDidCloseTerminal(): void {
this._terminal = null;
}

@HoseynAAmiri
Copy link

I just installed it and saw a similar effect. If you mess around with the terminals, it will add another one. If you have nothing, then run it via code runner, it keeps using it. I think it is like it remembers the index of the terminal and if it couldn't find it, it will create a new one even though the previously made terminal is somewhere there. It would be nice if it could remember its one and only generated terminal, or use the existing one that is most likely the relevant one to its own.

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

No branches or pull requests

2 participants