microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.83k stars 29.49k forks source link

Untitled workspaces are not deduplicated from restored windows #234232

Open nipatriknilsson opened 1 week ago

nipatriknilsson commented 1 week ago

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

When you open code with multiple workspaces on the command line, you get two code windows opened after the first time. Demonstration code in bash:

mkdir -p Documents/a
mkdir -p Documents/b
mkdir -p Documents/c
touch Documents/a/1.txt
touch Documents/b/2.txt
touch Documents/c/3.txt
code --password-store=basic --wait --user-data-dir test1 Documents/a # works
code --password-store=basic --wait --user-data-dir test2 Documents/b Documents/c # works, opens in one window
code --password-store=basic --wait --user-data-dir test1 Documents/a # works
code --password-store=basic --wait --user-data-dir test2 Documents/b Documents/c # opens the same workspaces in two windows, should only open one window

After each code session opens, close it to proceed. The last line opens two windows, although it is exactly the same as the second code line.

bpasero commented 1 week ago

I can reproduce, we are actually not having the exact same workspace in multiple windows (which is prohibited generally) but we simply create a new ad-hoc untitled workspace and open that.

I think what is missing here is a way on startup to figure out if a previous untitled workspace with the same folders is already being restored and then use that instead of opening another untitled workspace.

bpasero commented 1 week ago

Opening this up for help wanted. The entry point for deciding which windows to open is here:

https://github.com/microsoft/vscode/blob/8d1d1373cd9b8408222b51958e9688827a9e27ce/src/vs/platform/windows/electron-main/windowsMainService.ts#L286

nipatriknilsson commented 1 week ago

[...] untitled workspace with the same folders is already being restored [...]

There is another use case here:

mkdir -p Documents/a
mkdir -p Documents/b
mkdir -p Documents/c
mkdir -p Documents/d
touch Documents/a/1.txt
touch Documents/b/2.txt
touch Documents/c/3.txt
touch Documents/d/4.txt
code --password-store=basic --wait --user-data-dir test1 Documents/a
code --password-store=basic --wait --user-data-dir test2 Documents/b Documents/c
code --password-store=basic --wait --user-data-dir test1 Documents/a
code --password-store=basic --wait --user-data-dir test2 Documents/b Documents/c
code --password-store=basic --wait --user-data-dir test2 Documents/b Documents/c Documents/d # add a new folder to the workspace

The last line adds a new folder to the untitled workspace.

bpasero commented 1 week ago

We do have a --add option to add a folder to the last opened workspace. I think in your example I would not expect that to happen automatically without this parameter.

Image

nipatriknilsson commented 1 week ago

We do have a --add option to add a folder to the last opened workspace. I think in your example I would not expect that to happen automatically without this parameter.

Image

Another way of solving it would be a "--list-workspaces" option:

code --password-store=basic --wait --user-data-dir test1 --list-workspaces

If that option is given create user-data-dir if it doesn't exist, print workspaces (or nothing) and exit. Then I can handle all cases of above, by bash logic.

bpasero commented 1 week ago

This would actually have to be addressed somewhere here where we know which paths to restore from the last session and then right after decide on creating untitled workspace:

https://github.com/microsoft/vscode/blob/7385318ece6073a4159dd545a6243d1835c4eaff/src/vs/platform/windows/electron-main/windowsMainService.ts#L756-L781