Skip to content

restore the active source tab by document id, not tab index - #17947

Merged
kevinushey merged 2 commits into
mainfrom
bugfix/restore-active-tab-by-doc-id
Jun 11, 2026
Merged

kevinushey merged 2 commits into
mainfrom
bugfix/restore-active-tab-by-doc-id

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

The Source pane persisted the active tab across reloads as a physical tab index (the in-session display position mapped through the tabOrder_ permutation), but on restore the saved index was applied directly to the freshly built tab set, whose order comes from the persisted relativeOrder (falling back to creation time). Whenever the two orders diverge -- tabs drag-reordered during the session, or a document opened mid-session (new tabs are inserted immediately after the current tab but restored at the end, since they keep relativeOrder == 0) -- the wrong tab was selected after a reload or session restart.

This change persists the active document's ID instead (client state key activeTabDocId) and resolves it to a tab after the documents are restored:

  • Source.java: the IntStateValue for the active tab becomes a StringStateValue holding the active document ID.
  • SourceColumnManager.initialSelect() now takes a doc ID, restores the last active column, and selects the column/tab containing that document.
  • SourceColumn.initialSelect() looks the document up by ID and selects its tab.

The in-session save/restore pair (getPhysicalTabIndex() / setPhysicalTabIndex()) is untouched; only the cross-reload persistence path changes. The previously saved activeTab integer is simply ignored (one-time, the active tab falls back to the first restored document).

Includes a Playwright regression test that opens documents so the physical order diverges from the restored order, reloads, and asserts the pre-reload active document is re-selected. Verified the test fails (selects the wrong tab, exactly as reported) against the unfixed build and passes with the fix.

Not included: the complementary set_doc_order change suggested in the issue (persisting tab order when a tab is inserted at a non-terminal position). That would make the restored tab order match the pre-reload physical order, but it adds an RPC to every document open and is a separate, cosmetic symptom; the wrong-active-tab bug is fully addressed by the ID-based persistence.

Fixes #17944.

The active tab was persisted across reloads as a physical tab index,
but documents are restored in persisted relativeOrder (falling back to
creation time), which can differ from the in-session physical order --
e.g. after drag-reordering tabs, or after opening a document mid-session
(new tabs are inserted after the current tab but restored at the end).
Persist the active document's id instead and resolve it to a tab after
the documents are restored.

Addresses #17944
@kevinushey kevinushey added this to the Blue Plumbago milestone Jun 11, 2026
@kevinushey
kevinushey requested a review from gtritchie June 11, 2026 19:08
@github-actions

github-actions Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

🍎 macOS Desktop E2E · ARM64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
347 0 16 1
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · macos-26 ARM64 · commit f1d215ad4feb6335212d0ee198169e9099942911 · Run #225

@gtritchie

Copy link
Copy Markdown
Member

Code review findings

Actionable items from an automated multi-agent review. Strengths, dismissed findings, and ratings
are omitted.

Important

  1. src/gwt/.../source/SourceColumn.java:265 — the comment on initialSelect(String docId) says it
    "assumes no tabs have been reordered yet, so editors_ order matches tab order." That assumption
    is not load-bearing: display_.selectTab(i) selects by widget/logical index (= editors_
    index), and DocTabLayoutPanel.moveTab only rearranges the tab-bar DOM, so selecting by document
    id is robust to reordering regardless. The framing could mislead a future maintainer into thinking
    this code shares the old positional fragility (the bug class source pane restores the wrong active tab after reload #17944 fixes). Fix: reword to explain
    that id resolution works because it goes through the logical index, not a physical tab position.

  2. src/gwt/.../source/SourceColumnManager.java:475 — when findByDocument(docId) returns null
    (the persisted active doc was closed before reload, or wasn't restored), initialSelect returns
    early. This doesn't blank the pane (restoreDocuments auto-selects the last-restored tab), but
    the landing tab never receives onInitiallyLoaded(), so its startup checkForExternalEdit() is
    skipped, and getValue() then persists the last-restored doc — silently drifting the remembered
    active doc. Fix: on this path, default-select the active column's current/first tab so
    onInitiallyLoaded() still fires.

Suggestions

  1. e2e/rstudio/tests/panes/editor/active_tab_restore.test.ts — the doc-not-found early-return
    branches added in this PR (finding 2) have no coverage. Add a case: open A,B,C with C active,
    close C, reload, assert no crash and a sane active tab.

  2. e2e/rstudio/tests/panes/editor/active_tab_restore.test.ts:71 — the "reload anyway" fallback
    logs console.warn and proceeds if no set_client_state push carrying the doc id is seen in
    20s, so a persistence regression surfaces as a confusing downstream expect.poll timeout on
    fileD rather than at the persistence step. Fix: make it expect(pushed).toBe(true) with a
    descriptive message. Also tighten the loose match body.includes("${docId}") (line 67) to the
    activeTabDocId key, since doc ids appear in other client-state structures.

  3. e2e/rstudio/tests/panes/editor/active_tab_restore.test.ts — multi-column restore is untested,
    though the diff deliberately added cross-column resolution (findByDocument → setActive(column)
    in SourceColumnManager.initialSelect). A two-column restore test would lock that in.

  4. src/gwt/.../source/SourceColumnManager.java:470 — setActive(lastActive) is overridden by
    setActive(column) on the success path and is only operative on the fallback path. Correct as-is,
    but reads oddly; optional cleanup.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, a worthwhile-sounding tweak and the usual comment suggestions to consider.

…ed document is gone

- SourceColumnManager.initialSelect / SourceColumn.initialSelect: when the
  persisted active document no longer exists (e.g. closed before the
  reload), select the active column's currently selected tab so it still
  receives onInitiallyLoaded() (and its startup external-edit check)
- reword the SourceColumn.initialSelect comment: id resolution goes through
  the logical editors_ index, so it is robust to tab reordering by
  construction, not by assumption
- e2e: tighten the set_client_state sniff to the activeTabDocId key (doc
  ids appear in other client-state structures), and add a test for the
  closed-document fallback path
@kevinushey

Copy link
Copy Markdown
Collaborator Author

Thanks for the review -- addressed in f1d215a.

  1. Comment on SourceColumn.initialSelect: agreed, and the comment was actively wrong rather than just misleading -- selectTab() operates on the logical editors_ index and reordering only permutes tabOrder_ and the tab-bar DOM, so id resolution is robust to reordering by construction. Reworded to say so.

  2. Doc-not-found early return: fixed. SourceColumnManager.initialSelect now stays on the last active column when the persisted document is gone, and SourceColumn.initialSelect falls back to the currently selected tab, so onInitiallyLoaded() (and the startup external-edit check) still fires. Two notes: the impact was smaller than framed -- checkForExternalEdit() also runs on every editor focus, so the check was delayed, not lost -- and getValue() persisting the actually-active tab afterwards is correct behavior rather than drift (the remembered document no longer exists). The fallback is still worth having, so taken. This restructure also resolves item 6.

  3. Closed-doc test: added. One correction to the finding's premise: restore selects the first restored tab when nothing is selected (GWT TabLayoutPanel behavior), not the last. A timer push between the close and the reload can also legitimately re-persist the new active document, so the test asserts the pane lands on some open document (and not the closed one) rather than a specific file.

  4. Test fallback / loose match: took the match-tightening -- the sniff now requires the activeTabDocId key, since doc ids appear in other client-state payloads and a premature match could trigger the reload before the relevant value was persisted. Kept the best-effort warn-and-proceed rather than hard-asserting the push: the sniff is a timing optimization, and if persistence truly regresses the behavioral assertion still fails, with the warning explaining why. Hard-asserting would couple the test to the RPC mechanics and add a flake vector.

  5. Multi-column restore test: skipping for this PR -- setting up source columns in e2e is disproportionate to the thin findByDocument -> setActive(column) path it would cover. Fine as a follow-up if multi-column restore ever regresses.

  6. setActive(lastActive) readability: resolved by the item-2 restructure; the last-active column is now plainly the fallback when the document is not found.

Both e2e tests pass against the dev desktop build.

@kevinushey
kevinushey merged commit 63ec8ec into main Jun 11, 2026
10 checks passed
@kevinushey
kevinushey deleted the bugfix/restore-active-tab-by-doc-id branch June 11, 2026 23:39
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

Successfully merging this pull request may close these issues.

source pane restores the wrong active tab after reload

2 participants