A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: limit private variable rename to current cell (#7814)
Myles Scolnicka7c1e8a7ee24
diff --git a/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts b/frontend/src/components/app-config/__tests__/get-dirty-values.test.tsindex ff37f06c8d..9c29c1fb53 100644--- a/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts+++ b/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts@@ -1,4 +1,4 @@-/* Copyright 2024 Marimo. All rights reserved. */+/* Copyright 2026 Marimo. All rights reserved. */import { describe, expect, test } from "vitest";import { getDirtyValues } from "../user-config-form";diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsxindex a8f08291cd..5cd3d1e7cd 100644--- a/frontend/src/components/app-config/user-config-form.tsx+++ b/frontend/src/components/app-config/user-config-form.tsx@@ -69,7 +69,7 @@ export function getDirtyValues<T extends FieldValues>(dirtyFields: Partial<Record<keyof T, unknown>>,): Partial<T> {const result: Partial<T> = {};- for (const key of Object.keys(dirtyFields) as Array<keyof T>) {+ for (const key of Object.keys(dirtyFields) as (keyof T)[]) {const dirty = dirtyFields[key];if (dirty === true) {result[key] = values[key];diff --git a/frontend/src/components/editor/chrome/types.ts b/frontend/src/components/editor/chrome/types.tsindex 801f60dd1e..7c127f3a71 100644--- a/frontend/src/components/editor/chrome/types.ts+++ b/frontend/src/components/editor/chrome/types.ts@@ -194,10 +194,8 @@ export function isPanelHidden(if (panel.hidden) {return true;}- if (panel.requiredCapability) {- if (!capabilities[panel.requiredCapability]) {- return true;- }+ if (panel.requiredCapability && !capabilities[panel.requiredCapability]) {+ return true;}return false;}diff --git a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.tsindex 7ca6727ae7..e3c55bf05d 100644--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts@@ -756,6 +756,129 @@ describe("NotebookLanguageServerClient", () => {// Verify that the import cell was not changedexpect(mockView1.state.doc.toString()).toBe("import marimo as mo");});++ it("should only rename private variables in the current cell (issue #7810)", async () => {+ const props = {+ workspaceFolders: null,+ capabilities: {+ textDocument: {+ rename: {+ prepareSupport: true,+ },+ },+ },+ languageId: "python",+ transport: {+ sendData: vi.fn(),+ subscribe: vi.fn(),+ connect: vi.fn(),+ transportRequestManager: {+ send: vi.fn(),+ },+ } as any,+ };++ // Setup editor views - both cells have a private variable _x+ const cell1Code = "_x = 1\nprint(_x)";+ const cell2Code = "_x = 2\nprint(_x)";++ const mockView1 = new EditorView({+ doc: cell1Code,+ extensions: [+ languageAdapterState.init(() => new PythonLanguageAdapter()),+ languageMetadataField.init(() => ({})),+ languageServerWithClient({+ client: mockClient as unknown as LanguageServerClient,+ documentUri: CellDocumentUri.of(Cells.cell1),+ ...props,+ }),+ ],+ });++ const mockView2 = new EditorView({+ doc: cell2Code,+ extensions: [+ languageAdapterState.init(() => new PythonLanguageAdapter()),+ languageMetadataField.init(() => ({})),+ languageServerWithClient({+ client: mockClient as unknown as LanguageServerClient,+ documentUri: CellDocumentUri.of(Cells.cell2),+ ...props,+ }),+ ],+ });++ (notebookClient as any).getNotebookEditors = () => ({+ [Cells.cell1]: mockView1,+ [Cells.cell2]: mockView2,+ });++ // Update the mock to return the correct codes+ vi.spyOn(store, "get").mockImplementation((atom) => {+ if (atom === topologicalCodesAtom) {+ return {+ cellIds: [Cells.cell1, Cells.cell2],+ codes: {+ [Cells.cell1]: cell1Code,+ [Cells.cell2]: cell2Code,+ },+ };+ }+ return undefined;+ });++ // Setup rename params - renaming _x in cell1+ const renameParams: LSP.RenameParams = {+ textDocument: { uri: CellDocumentUri.of(Cells.cell1) },+ position: { line: 0, character: 0 }, // position of '_x'+ newName: "_y",+ };++ // Open a document first to set up the lens+ await notebookClient.textDocumentDidOpen({+ textDocument: {+ uri: CellDocumentUri.of(Cells.cell1),+ languageId: "python",+ version: 1,+ text: cell1Code,+ },+ });++ // Mock the response from the language server+ // The LSP server would rename _x in BOTH cells (since it sees the merged doc)+ const mockRenameResponse: LSP.WorkspaceEdit = {+ documentChanges: [+ {+ textDocument: {+ uri: "file:///__marimo_notebook__.py",+ version: 1,+ },+ edits: [+ {+ range: {+ start: { line: 0, character: 0 },+ end: { line: 3, character: 10 },+ },+ // LSP renames _x to _y in both cells+ newText: "_y = 1\nprint(_y)\n_y = 2\nprint(_y)",+ },+ ],+ },+ ],+ };++ mockClient.textDocumentRename = vi+ .fn()+ .mockResolvedValue(mockRenameResponse);++ // Call rename+ await notebookClient.textDocumentRename(renameParams);++ // The fix: only cell1 should be renamed, cell2 should remain unchanged+ // because private variables are cell-local in marimo+ expect(mockView1.state.doc.toString()).toBe("_y = 1\nprint(_y)");+ expect(mockView2.state.doc.toString()).toBe("_x = 2\nprint(_x)");+ });});describe("diagnostics handling", () => {diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.tsindex 95b5b06674..6ec52e9133 100644--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts@@ -9,6 +9,7 @@ import { invariant } from "@/utils/invariant";import { Logger } from "@/utils/Logger";import { LRUCache } from "@/utils/lru";import { Objects } from "@/utils/objects";+import { getPositionAtWordBounds } from "../completion/hints";import { topologicalCodesAtom } from "../copilot/getCodes";import {getEditorCodeAsPython,@@ -22,6 +23,14 @@ import {} from "./types";import { getLSPDocument } from "./utils";+/**+ * Check if a variable name is private (starts with underscore but not dunder).+ * Private variables in marimo are cell-local and should not be renamed across cells.+ */+function isPrivateVariable(name: string): boolean {+ return name.startsWith("_") && !name.startsWith("__");+}+class Snapshotter {private documentVersion = 0;private readonly getNotebookCode: () => {@@ -433,15 +442,46 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {// Update the code in the plugins manuallyconst editors = this.getNotebookEditors();- for (const [cellId, ev] of Objects.entries(editors)) {- const newCode = editsToNewCode.get(cellId);++ // Check if this is a private variable rename (should only affect current cell)+ // Private variables in marimo are cell-local and should not be renamed across cells+ const originEditor = editors[cellId];+ let isPrivateRename = false;+ if (originEditor) {+ // Convert LSP position (line, character) to CodeMirror position+ const line = originEditor.state.doc.line(params.position.line + 1);+ const cmPosition = line.from + params.position.character;+ const { startToken, endToken } = getPositionAtWordBounds(+ originEditor.state.doc,+ cmPosition,+ );+ const originalName = originEditor.state.doc.sliceString(+ startToken,+ endToken,+ );+ isPrivateRename = isPrivateVariable(originalName);+ if (isPrivateRename) {+ Logger.debug(+ "[lsp] Private variable rename detected, limiting to current cell",+ originalName,+ );+ }+ }++ for (const [currentCellId, ev] of Objects.entries(editors)) {+ // For private variable renames, only update the originating cell+ if (isPrivateRename && currentCellId !== cellId) {+ continue;+ }++ const newCode = editsToNewCode.get(currentCellId);if (newCode == null) {- Logger.warn("No new code for cell", cellId);+ Logger.warn("No new code for cell", currentCellId);continue;}if (!ev) {- Logger.warn("No view for plugin", cellId);+ Logger.warn("No view for plugin", currentCellId);continue;}diff --git a/frontend/src/core/kernel/__tests__/handlers.test.ts b/frontend/src/core/kernel/__tests__/handlers.test.tsindex 2cb7634e6a..a43e70d8e6 100644--- a/frontend/src/core/kernel/__tests__/handlers.test.ts+++ b/frontend/src/core/kernel/__tests__/handlers.test.ts@@ -142,8 +142,8 @@ describe("buildCellData", () => {cell2: "y = 2",},last_execution_time: {- cell1: 1234567890,- cell2: 1234567891,+ cell1: 1_234_567_890,+ cell2: 1_234_567_891,},app_config: {width: "normal",diff --git a/frontend/src/core/kernel/state.ts b/frontend/src/core/kernel/state.tsindex 414bf5eaec..e3109db6d0 100644--- a/frontend/src/core/kernel/state.ts+++ b/frontend/src/core/kernel/state.ts@@ -1,3 +1,4 @@+/* Copyright 2026 Marimo. All rights reserved. */import { atom } from "jotai";import { waitFor } from "../state/jotai";diff --git a/frontend/src/core/network/__tests__/requests-lazy.test.ts b/frontend/src/core/network/__tests__/requests-lazy.test.tsindex 3ce5e46c25..d4d55f6367 100644--- a/frontend/src/core/network/__tests__/requests-lazy.test.ts+++ b/frontend/src/core/network/__tests__/requests-lazy.test.ts@@ -1,4 +1,4 @@-/* Copyright 2024 Marimo. All rights reserved. */+/* Copyright 2026 Marimo. All rights reserved. */import { beforeEach, describe, expect, it, vi } from "vitest";import type { RuntimeManager } from "../../runtime/runtime";diff --git a/frontend/src/core/network/requests-lazy.ts b/frontend/src/core/network/requests-lazy.tsindex 25bd597ead..c507fa8164 100644--- a/frontend/src/core/network/requests-lazy.ts+++ b/frontend/src/core/network/requests-lazy.ts@@ -1,4 +1,4 @@-/* Copyright 2024 Marimo. All rights reserved. */+/* Copyright 2026 Marimo. All rights reserved. */import { NoKernelConnectedError } from "@/utils/errors";import { Logger } from "@/utils/Logger";@@ -152,7 +152,7 @@ export function createLazyRequests(`Dropping request: ${key}, since not connected to a kernel.`,);// Silently drop the request- return Promise.resolve();+ return;case "throwError":throw new NoKernelConnectedError();diff --git a/frontend/src/css/globals.css b/frontend/src/css/globals.cssindex 729cd3ffbd..2c5f8c1a89 100644--- a/frontend/src/css/globals.css+++ b/frontend/src/css/globals.css@@ -184,8 +184,10 @@--shadow-2xl-solid: 10px 12px 0px 0px var(--base-shadow-darker), 0 0px 8px 0px hsl(0deg 0% 90% / 50%);/* Solid shadows with lighter shade color */+/* biome-ignore format: definition needs to be oneline or breaks variants */--shadow-sm-solid-shade: 2px 2px 0px 0px var(--base-shadow), 0px 0px 2px 0px hsl(0deg 0% 50% / 20%);+/* biome-ignore format: definition needs to be oneline or breaks variants */--shadow-md-solid-shade: 4px 4px 0px 0px var(--base-shadow), 0 0px 2px 0px hsl(0deg 0% 60% / 50%);}