Skip to content

Remove deleted cells from the NotebookDocuments cell map - #1886

Merged
Dirk Bäumer (dbaeumer) merged 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-notebook-deleted-cells
Oct 14, 2026
Merged

Dirk Bäumer (dbaeumer) merged 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-notebook-deleted-cells

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown
Contributor

When a notebookDocument/didChange deletes a cell (structure.didClose), NotebookDocuments splices the cell out of the notebook and closes its text document. It never removes the cell from its internal cell map, though. updateCellMap only re-adds the cells that are left. So after deleting cell2 from a notebook with [cell1, cell2]:

  • before: getNotebookCell(cell2) still returns the cell, and findNotebookDocumentForCell(cell2) still returns the notebook, even though cellTextDocuments.get(cell2) is undefined. The stale entry also survives notebookDocument/didClose, because the close handler only clears the cells still in the notebook. Each deleted cell therefore keeps its closed notebook reachable.
  • after: both lookups return undefined.

The fix collects the removed cells and then drops their map entries before the map is refreshed for the remaining cells, so cells.removed in the change event still reports them. The deletion happens after the cell text documents are closed, so findNotebookDocumentForCell still works inside cellTextDocuments.onDidClose listeners. A cell that is closed and re-inserted in the same change is added back by updateCellMap.

  • Ran: npm run compile:server and cd server && npm test. 15 passing. The new test fails on main with getNotebookCell(cell2) returning the deleted cell.
  • Checked: cd server && npm run lint is clean.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused fix preserves event behavior and includes regression coverage for the reported stale mapping.

0 open findings

What changed in this PR

Fixes stale notebook-cell mappings after cell deletion.

Changes:

  • Removes closed cells from the internal cell map before refreshing it.
  • Adds regression coverage for removed-cell lookup and change events.
File Description
server/​src/​common/​notebook.ts Deletes stale cell-map entries during notebook changes.
server/​src/​node/​test/​notebookdocuments.test.ts Verifies deleted cells are no longer resolvable.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dbaeumer
Dirk Bäumer (dbaeumer) merged commit f7dda14 into microsoft:main Oct 14, 2026
5 checks passed
@dbaeumer Dirk Bäumer (dbaeumer) added this to the 3.18.5 milestone Oct 21, 2026
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.

5 participants