Terminal window is reopened in different directory after restart
Apache NetBeans version
Apache NetBeans 27
What happened
After opening a project in the terminal using Open in Terminal, which starts the terminal in the project directory, then exiting NetBeans and restarting NetBeans, NetBeans automatically recreates the terminal window, however starting in the NetBeans installation directory rather than in the project directory. If NetBeans remembers the terminal windows across restarts, it should also remember the directories they were started with. Otherwise this just creates a useless terminal window, and it's quicker to use Open in Terminal on the project again.
This is on Windows with Cygwin.
Language / Project Type / NetBeans Component
Project, Terminal
How to reproduce
- Open a project in NetBeans (in my case a Java Maven project).
- In the Projects view, invoke Open in Terminal in the project's context menu. => Observe that the current directory in the terminal is the project directory.
- Exit NetBeans.
- Start NetBeans again. => A new terminal window is automatically opened, but its current directory is the NetBeans installation directory
Did this work correctly in an earlier version?
No / Don't know
Operating System
Windows, Cygwin
JDK
21
Apache NetBeans packaging
Apache NetBeans binary zip
Anything else
No response
Are you willing to submit a pull request?
No
i don't see this as a bug. This is handled by the terminal pinning functionality which is there to persist terminals. If you don't pin a terminal, it will initialize using shell defaults.
@mbien: Can you explain what you mean by terminal pinning?
My point is (1) that it serves no purpose to reopen a local terminal if it reopens in the NetBeans installation directory. I'd rather have it not reopen on restart, over reopening in the wrong directory. In practice I always have to close this useless terminal window. And (2), since the original terminal window was able to open in the correct project directory, there's no reason why it shouldn't be able to do the same after a restart. From an end-user point of view, it does seem like a bug that it doesn't.
Can you explain what you mean by terminal pinning?
right click on the terminal tab. You should see an option to pin it.
Can you explain what you mean by terminal pinning?
right click on the terminal tab. You should see an option to pin it.
Thanks, I see. This doesn't fit my usage well, however. I work with a about a dozen projects (several project groups in NetBeans), often each project a few days at a time. Pinning terminals for all long-term would not be convenient to use (too many terminals), so I would have to pin/unpin the respective terminals whenever I switch projects/project groups, specifying the directories and custom titles each time. There also doesn't seem to be a way to switch between terminals by keyboard, which makes having multiple terminals inconvenient. The dummy terminal after a restart is the lesser evil in that case, but still feels like it shouldn't be there in that dysfunctional way.
it serves no purpose to reopen a local terminal if it reopens in the NetBeans installation directory
I've never used the built-in terminal on Windows. I don't agree with most of this report being a bug, but it might make sense as an optional feature request. However, this particular aspect seems like it might need addressing. The default terminal is opening in the installation directory on Windows? On Linux or macOS this defaults to the home dir like a system opened terminal would. I would expect it to open somewhere usefully comparable on Windows.
but still feels like it shouldn't be there in that dysfunctional way.
This is the wrong perspective. For your usecase it is "dysfunctional", others just need a real terminal to work with. I routinely use the default terminal to switch to wsl, run server, ssh into other machines. Yes there are project bound actions, then I open a new terminal for that project and continue work there.
So from my POV this is not a bug, just a (deactivateable) feature: "Remember open terminals".
what might be possible is to add an option to auto-pin terminals which are opened via right click -> open terminal action, since those were initialized with a non-standard path. Or make pinning easier in some other way without having to re-enter the path again.
So from my POV this is not a bug, just a (deactivateable) feature: "Remember open terminals".
I probably wouldn't even make this configurable since opening more than one un-pinned terminal after start is likely not very useful?
What I meant is: Assuming, that the new feature is active. I open three terminals, one in "/home/demo/test", one in "/home/demo/test/target" and one in "/usr/local/". I shut down NetBeans, start it again. Now there are again three terminals all in their respective correct paths.
What I meant is: Assuming, that the new feature is active. I open three terminals, one in "/home/demo/test", one in "/home/demo/test/target" and one in "/usr/local/". I shut down NetBeans, start it again. Now there are again three terminals all in their respective correct paths.
isn't that what pinning does? So the missing link would be to make pinning easier, or let the "Open terminal in" action auto-pin?
I just want to be careful so that we don't end up with two distinct ways how to persist terminals - ideally reusing/improving existing functionality.
What I meant is: Assuming, that the new feature is active. I open three terminals, one in "/home/demo/test", one in "/home/demo/test/target" and one in "/usr/local/". I shut down NetBeans, start it again. Now there are again three terminals all in their respective correct paths.
isn't that what pinning does? So the missing link would be to make pinning easier, or let the "Open terminal in" action auto-pin?
Ah yes, sorry. You are right.
but still feels like it shouldn't be there in that dysfunctional way.
This is the wrong perspective. For your usecase it is "dysfunctional", others just need a real terminal to work with. I routinely use the default terminal to switch to wsl, run server, ssh into other machines. Yes there are project bound actions, then I open a new terminal for that project and continue work there.
So from my POV this is not a bug, just a (deactivateable) feature: "Remember open terminals".
To clarify, I'm specifically talking about the case when the terminal was opened from the project node via "Open in Terminal". To me, this means that the terminal window that is opened is specific to the project. From the wording of the menu item, the project was opened in the terminal. (Note that "Open in Terminal" only exists for project nodes). This connection to the project is lost after the restart. It's a bit as if after opening a file in the editor and then restarting NetBeans, the editor would reopen the tab that originally showed the file, but is now showing a blank document.
Note that "Open in Terminal" only exists for project nodes
any folder can be opened, the actions move into the Tools sub-menu of the context menu.
There is no link to the project, all it does is to initialize the terminal with a path.
Note that "Open in Terminal" only exists for project nodes
any folder can be opened, the actions move into the
Toolssub-menu of the context menu.There is no link to the project, all it does is to initialize the terminal with a path.
Ah, thanks for pointing that out, I hadn't thought of looking into submenus; Previously I had wondered why the option isn't available on arbitrary directories. So the connection is to the directory that was opened in the terminal. Nevertheless, the menu item is prominent for projects. My main use case for the terminal is for Git commands, so the connection to the project (local git repo and workdir) is more salient.
Regarding pinning, for my usage this has the drawback that when switching projects, I would have to both unpin and exit the terminal window, where previously I would only have to exit the terminal., Exiting/closing a pinned window doesn't unpin it, hence it would reappear after a restart, in addition to the new terminal I would have opened for the project I switched to.
I'd like to proposer the following alternative, and I would also be inclined to implement it myself if that helps: When a terminal is opened in a specific directory (in particular via "Open in Terminal"), remember the directory it was opened in. When that terminal is auto-reopened after a restart, reopen it with the remembered directory. But do not auto-pin it, so that when it is manually exited/closed, a later restart will not reopen it. A second, independent feature could be that when such a terminal is pinned, the "Directory" field in the "Pin Tab" dialog would be pre-filled with the remembered directory.
The default terminal is opening in the installation directory on Windows?
I took a closer look at that, and it seems that the terminal process simply inherits the current working directory from its parent, i.e. from the main NetBeans process. The NetBeans Windows installer always set the working directory to the installation directory in the NetBeans start menu shortcut. After the installer was recently discontinued, I just kept this the same for new installations. So the default working directory for a new terminal is just whatever working directory NetBeans was started with. If you double-click on the NetBeans executable in Windows Explorer, it would be the bin directory that contains it. On Unix the current working directory is maybe typically $HOME instead for the main NetBeans process. So I wouldn't say that the behavior is wrong here. If someone starts NetBeans with a specific current working directory, it would be plausible for them to expect terminals to open in that same directory by default.
then exiting NetBeans and restarting NetBeans, NetBeans automatically recreates the terminal window,
i just realized that I open terminals all the time without closing them and NB does only open the tabs I pinned on restart. I am wondering why it behaves differently in your case.
@mbien: What really happens is that when the TerminalContainerTopComponent was open when exiting NetBeans, it will always spawn a new non-pinned terminal when NetBeans is restarted, in addition to any pinned terminals, and even when the TC didn't contain any open terminal when NetBeans was exited. Since I always had a single non-pinned terminal open from the Projects view and didn't close it before exiting, it seemed that NetBeans was reopening that previous terminal.
When the terminal TC is not open, and the user selects Window > IDE Tools > Terminal, then it makes sense that not only the TC is opened, but also a new terminal is spawned. NetBeans probably runs the same code when it automatically reopens the remembered TC upon restart (when there is <state opened="true"/> in TerminalContainerTopComponent.wstcref). However, this is confusing when in the previous NetBeans instance the only opened terminal was via Projects > Open in Terminal.
There seems to be another bug regarding pinned terminals: When pinned terminals are automatically reopened upon restart, they are not displayed as pinned anymore (the context menu shows "Pin Tab..." instead of "Unpin Tab", and the pin icon is missing). Nevertheless, they are still pinned, in the sense that when closing them and then restarting NetBeans, they do again reappear (again not showing as pinned), in addition to the one non-pinned terminal that is opened.
In my use case, while I could manually cd to the correct directory within that reopened terminal, it's faster to close the terminal and use Open in Terminal on the project again. The subjective experience, however, is that a "useless" terminal is opened in the wrong directory, that I always have to close upon restart.