Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): clamp SQL editor outline range to live model (#69200)

posthog[bot]90acd02a8245
diff --git a/frontend/src/scenes/data-warehouse/editor/renderQueryOutline.test.ts b/frontend/src/scenes/data-warehouse/editor/renderQueryOutline.test.ts
new file mode 100644
index 000000000000..57fa9148b72e
--- /dev/null
+++ b/frontend/src/scenes/data-warehouse/editor/renderQueryOutline.test.ts
@@ -0,0 +1,33 @@
+import type { editor, IRange } from 'monaco-editor'
+
+import { renderQueryOutline } from './sqlEditorLogic'
+
+// Minimal Monaco stand-in whose `getLineMaxColumn` throws for out-of-range lines,
+// exactly like the real editor: that throw ("Illegal value for lineNumber") is the
+// crash we're guarding against when a paste/edit shrinks the model under a stale range.
+function makeEditor(lineCount: number): editor.IStandaloneCodeEditor {
+ const model = {
+ getLineCount: () => lineCount,
+ getLineMaxColumn: (line: number) => {
+ if (line < 1 || line > lineCount) {
+ throw new Error('Illegal value for lineNumber')
+ }
+ return 20
+ },
+ }
+ return {
+ getModel: () => model,
+ getScrolledVisiblePosition: () => ({ left: 0, top: 0, height: 18 }),
+ } as unknown as editor.IStandaloneCodeEditor
+}
+
+describe('renderQueryOutline', () => {
+ it('does not throw when the cached range points past a shrunk model', () => {
+ const node = document.createElement('div')
+ // Model has 2 lines, but the cached range still spans 5 (e.g. after a paste removed lines).
+ const staleRange: IRange = { startLineNumber: 1, startColumn: 1, endLineNumber: 5, endColumn: 10 }
+
+ expect(() => renderQueryOutline(makeEditor(2), node, staleRange)).not.toThrow()
+ expect(node.style.display).toBe('block')
+ })
+})
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
index 86ed6ec3bc29..57ee7eedf603 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
@@ -113,7 +113,11 @@ export interface SqlEditorLogicProps {
// border from a className. Instead, we maintain an absolutely-positioned `div`
// inside the editor's overlay layer and recompute its bounding box from the pixel
// positions of the range's start/end on each line.
-function renderQueryOutline(editorInstance: editor.IStandaloneCodeEditor, node: HTMLElement, range: IRange): void {
+export function renderQueryOutline(
+ editorInstance: editor.IStandaloneCodeEditor,
+ node: HTMLElement,
+ range: IRange
+): void {
const model = editorInstance.getModel()
if (!model) {
node.style.display = 'none'
@@ -125,9 +129,18 @@ function renderQueryOutline(editorInstance: editor.IStandaloneCodeEditor, node:
let minTop = Infinity
let maxBottom = -Infinity
- for (let line = range.startLineNumber; line <= range.endLineNumber; line++) {
- const leftCol = line === range.startLineNumber ? range.startColumn : 1
- const rightCol = line === range.endLineNumber ? range.endColumn : model.getLineMaxColumn(line)
+ // The cached range can outlive the document it was computed against: a paste or edit
+ // that removes lines shrinks the model, but this render path runs on scroll/layout
+ // without re-clamping. Passing an out-of-range line to `getLineMaxColumn` throws
+ // "Illegal value for lineNumber", so clamp every line/column against the live model.
+ const lineCount = model.getLineCount()
+ const startLine = Math.max(1, Math.min(range.startLineNumber, lineCount))
+ const endLine = Math.max(1, Math.min(range.endLineNumber, lineCount))
+
+ for (let line = startLine; line <= endLine; line++) {
+ const lineMaxColumn = model.getLineMaxColumn(line)
+ const leftCol = Math.min(line === startLine ? range.startColumn : 1, lineMaxColumn)
+ const rightCol = line === endLine ? Math.min(range.endColumn, lineMaxColumn) : lineMaxColumn
if (leftCol >= rightCol) {
continue
}