Source Control Repositories view re-expands repositories with submodules on every visibility change
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.4k
- PR merge metrics
- PR metrics pending
Description
Does this issue occur when all extensions are disabled?: Yes
- VS Code Version: 1.137.0 (645f29cc3176500b4b5762ba887cf2a7f0ffdf2c), also reproduces on `main` @ fdd5bbeb
- OS Version: macOS 26.6.2 (arm64)
No third-party extensions are involved — the repositories come from the built-in Git extension.
Steps to Reproduce:
1. Set `"scm.repositories.selectionMode": "single"` and make the **Repositories** view visible in the Source Control view container (it is `hideByDefault`, so enable it via `View: Open View... > Repositories`).
2. Open a folder that is a git repository and contains at least one initialized submodule, so the Repositories view shows the submodules nested under their parent repository.
3. Collapse the parent repository.
4. Switch to the Explorer view container and back to Source Control.
Expected: the parent repository is still collapsed.
Actual: the parent repository is expanded again. This happens on every single switch away from and back to the Source Control view container, so the collapsed state can never be kept.
### Cause
Both halves of the problem are in `src/vs/workbench/contrib/scm/browser/scmRepositoriesViewPane.ts`:
1. Every time the view becomes visible, the tree input is set with the view state that was read from storage once in `renderBody` ([line 494](https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/scm/browser/scmRepositoriesViewPane.ts#L494)). That view state is only ever written on `onWillSaveState`, so any expand/collapse the user did in the meantime is discarded when the view is shown again.
2. When the view becomes visible, all already known repositories are re-registered through `onDidAddRepository` ([line 527](https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/scm/browser/scmRepositoriesViewPane.ts#L527)). For a repository that has a `parentId` — i.e. a submodule — this ends up in `updateParentRepository`, which expands the parent unconditionally ([line 797](https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/scm/browser/scmRepositoriesViewPane.ts#L797)). So the parent is force-expanded once per submodule on every visibility change.
The same unconditional expand also runs when a repository's history item ref changes, which can re-expand a collapsed parent while the view is open.
### Suggested fix
- Capture `tree.getViewState()` when the view becomes hidden and use that as the input view state when it becomes visible again, instead of the state loaded once at startup.
- Only expand the parent repository when it actually gains its first child repository, not when existing repositories are merely re-registered.
I have a patch for this and would be happy to open a PR.
### Workaround
`"git.detectSubmodules": false`, but that removes the submodules from the Source Control view entirely.
Contributor guide
Assessment
This issue has not been assessed yet.