Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): raise @codemirror/state override to 6.7.1 (PRD-7839) (#4007)
Andrew Mikofalvy80817f2f3631
diff --git a/.changeset/codemirror-state-selection-crash.md b/.changeset/codemirror-state-selection-crash.mdnew file mode 100644index 000000000..93ce2861a--- /dev/null+++ b/.changeset/codemirror-state-selection-crash.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Fixed an error thrown inside every CodeMirror-backed editor when selecting with the mouse. Double-clicking to select a word and triple-clicking to select a line both threw, and so did the handler that starts a drag from a widget. Selecting still looked like it worked, because the browser's own selection took over once the editor's handler failed. That covers source mode, plain-text documents, the Mermaid editor, the text viewer, the raw MDX fallback, and the smaller code inputs in the property panel and preview dialogs. A dependency pin held `@codemirror/state` at a version older than the editor required, so the API those selection paths call was missing at runtime.diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.mdindex 04af1169f..3793b78ff 100644--- a/THIRD_PARTY_NOTICES.md+++ b/THIRD_PARTY_NOTICES.md@@ -1316,8 +1316,8 @@ Homepage: https://code.haverbeke.berlin/codemirror/searchCopyright (C) 2018-2021 by Marijn Haverbeke <[email protected]> and others-### `@codemirror/[email protected]`-Homepage: https://github.com/codemirror/state+### `@codemirror/[email protected]`+Homepage: https://code.haverbeke.berlin/codemirror/stateCopyright (C) 2018-2021 by Marijn Haverbeke <[email protected]> and othersdiff --git a/package.json b/package.jsonindex f4a672f67..449f72092 100644--- a/package.json+++ b/package.json@@ -18,7 +18,7 @@"check:fast": "pnpm run typecheck","check:doc-links": "pnpm --dir docs run validate-link","check:drift": "pnpm run check:drift:guards && vitest run --config vitest.scripts.config.ts",- "check:drift:guards": "bash scripts/check-husky-prepare-guard.sh && bash scripts/check-knip-clean.sh && bash scripts/check-notices-clean.sh && bash scripts/check-schema-snapshot-clean.sh && bash scripts/check-i18n-drift.sh && node scripts/check-i18n-new-string-translations.mjs && node scripts/check-i18n-picker-completeness.mjs && bash scripts/check-no-major-changeset.sh",+ "check:drift:guards": "bash scripts/check-husky-prepare-guard.sh && bash scripts/check-knip-clean.sh && bash scripts/check-notices-clean.sh && bash scripts/check-schema-snapshot-clean.sh && bash scripts/check-i18n-drift.sh && node scripts/check-i18n-new-string-translations.mjs && node scripts/check-i18n-picker-completeness.mjs && bash scripts/check-no-major-changeset.sh && node scripts/check-override-floors.mjs","typecheck": "turbo run typecheck","test": "turbo run test","test:vitest-selftest": "vitest run",diff --git a/packages/app/package.json b/packages/app/package.jsonindex 79569bade..e900b5751 100644--- a/packages/app/package.json+++ b/packages/app/package.json@@ -82,7 +82,7 @@"@codemirror/legacy-modes": "^6.5.1","@codemirror/lint": "^6.9.7","@codemirror/search": "^6.6.0",- "@codemirror/state": "^6.6.0",+ "@codemirror/state": "^6.7.0","@codemirror/view": "^6.43.3","@dnd-kit/core": "^6.3.1","@dnd-kit/sortable": "^10.0.0",diff --git a/packages/app/tests/meta/error-filters.test.ts b/packages/app/tests/meta/error-filters.test.tsnew file mode 100644index 000000000..6f84ef40b--- /dev/null+++ b/packages/app/tests/meta/error-filters.test.ts@@ -0,0 +1,37 @@+import { describe, expect, it } from 'vitest';+import { filterCriticalErrors } from '../stress/_helpers/error-filters';++describe('filterCriticalErrors', () => {+ const prebundled = 'http://localhost:5173/node_modules/.vite/deps/@codemirror_state.js';++ it('keeps a runtime exception reported from a pre-bundled chunk', () => {+ const entry = {+ type: 'error',+ text: 'Uncaught TypeError: EditorSelection.undirectionalRange is not a function',+ url: prebundled,+ };+ expect(filterCriticalErrors([entry])).toEqual([entry]);+ });++ it('still drops a genuine pre-bundle load failure', () => {+ expect(+ filterCriticalErrors([+ {+ type: 'error',+ text: 'Failed to load resource: the server responded with a status of 504',+ url: prebundled,+ },+ {+ type: 'error',+ text: 'GET /@vite/client net::ERR_ABORTED 404',+ url: 'http://localhost:5173/@vite/client',+ },+ ]),+ ).toEqual([]);+ });++ it('keeps an ordinary application error with no URL', () => {+ const entry = { type: 'uncaught', text: 'Cannot read properties of undefined' };+ expect(filterCriticalErrors([entry])).toEqual([entry]);+ });+});diff --git a/packages/app/tests/stress/_helpers/error-filters.ts b/packages/app/tests/stress/_helpers/error-filters.tsindex 7e3d76ad6..98291bf0d 100644--- a/packages/app/tests/stress/_helpers/error-filters.ts+++ b/packages/app/tests/stress/_helpers/error-filters.ts@@ -5,6 +5,9 @@ export interface LogEntry {line?: number;}+const LOAD_FAILURE =+ /Failed to load resource|net::ERR_|Failed to fetch|error loading dynamically imported module|status of \d{3}/i;+const BENIGN_PREDICATES: Array<(e: LogEntry) => boolean> = [(e) => e.text.includes('favicon'),(e) => e.text.includes('HMR'),@@ -13,10 +16,10 @@ const BENIGN_PREDICATES: Array<(e: LogEntry) => boolean> = [(e) => !!e.url?.endsWith('.map'),(e) => !!e.url?.includes('.hot-update.'),- (e) => !!e.url?.includes('/@vite/'),- (e) => !!e.url?.includes('/@fs/'),- (e) => !!e.url?.includes('/@id/'),- (e) => !!e.url?.includes('/node_modules/.vite/'),+ (e) => !!e.url?.includes('/@vite/') && LOAD_FAILURE.test(e.text),+ (e) => !!e.url?.includes('/@fs/') && LOAD_FAILURE.test(e.text),+ (e) => !!e.url?.includes('/@id/') && LOAD_FAILURE.test(e.text),+ (e) => !!e.url?.includes('/node_modules/.vite/') && LOAD_FAILURE.test(e.text),(e) => e.text.includes('WebSocket is closed before the connection is established'),(e) =>diff --git a/packages/app/tests/stress/source-polish.e2e.ts b/packages/app/tests/stress/source-polish.e2e.tsindex e99caf0d5..a872d803b 100644--- a/packages/app/tests/stress/source-polish.e2e.ts+++ b/packages/app/tests/stress/source-polish.e2e.ts@@ -281,3 +281,82 @@ test.describe('§6.7 Cross-cutting', () => {expect(clipboard).toBe(docState);});});++test.describe('Mouse selection', () => {+ async function pointAt(page: Page, needle: string): Promise<{ x: number; y: number }> {+ const found: { x: number; y: number }[] = [];+ await expect+ .poll(+ async () => {+ const point = await page.evaluate((text) => {+ const content = Array.from(document.querySelectorAll('.cm-content')).find(+ (el) => el.getClientRects().length > 0,+ );+ if (!content) return null;+ const walker = document.createTreeWalker(content, NodeFilter.SHOW_TEXT);+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {+ const offset = (node.textContent ?? '').indexOf(text);+ if (offset === -1) continue;+ const range = document.createRange();+ range.setStart(node, offset);+ range.setEnd(node, offset + text.length);+ const rect = range.getBoundingClientRect();+ if (rect.width === 0) continue;+ return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };+ }+ return null;+ }, needle);+ if (point) found.push(point);+ return found.length > 0;+ },+ { message: `"${needle}" never laid out in the source view` },+ )+ .toBe(true);+ const point = found[0];+ if (!point) throw new Error(`"${needle}" never laid out in the source view`);+ return point;+ }++ const editorSelection = (page: Page) =>+ page.evaluate(() => {+ const handle = Array.from(document.querySelectorAll('.cm-content')).find(+ (el) => el.getClientRects().length > 0,+ ) as+ | (Element & {+ cmTile?: { root?: { view?: unknown } };+ cmView?: { rootView?: { view?: unknown } };+ })+ | undefined;+ const view = (handle?.cmTile?.root?.view ?? handle?.cmView?.rootView?.view) as+ | {+ state: {+ selection: { main: { from: number; to: number } };+ sliceDoc: (f: number, t: number) => string;+ };+ }+ | undefined;+ if (!view) throw new Error('no visible CodeMirror view');+ const { from, to } = view.state.selection.main;+ return view.state.sliceDoc(from, to);+ });++ test('double-click selects the word under the pointer', async ({ page, api }) => {+ await seedMarkdown(api, testDocName, 'alpha bravo charlie\n');+ await switchToSource(page);++ const point = await pointAt(page, 'bravo');+ await page.mouse.dblclick(point.x, point.y);++ await expect.poll(() => editorSelection(page)).toBe('bravo');+ });++ test('triple-click selects the whole line', async ({ page, api }) => {+ await seedMarkdown(api, testDocName, 'alpha bravo charlie\n\nsecond paragraph\n');+ await switchToSource(page);++ const point = await pointAt(page, 'bravo');+ await page.mouse.click(point.x, point.y, { clickCount: 3 });++ await expect.poll(async () => (await editorSelection(page)).trim()).toBe('alpha bravo charlie');+ });+});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex ff689b416..086800027 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -6,14 +6,36 @@ settings:overrides:'@babel/core@7': ~7.29.6- '@codemirror/state': 6.6.0+ '@hono/node-server@1': ~1.19.15+ '@opentelemetry/core@2': ~2.8.0+ brace-expansion@1: ~1.1.18+ brace-expansion@2: ~2.1.4+ builder-util-runtime@9: ~9.7.0+ dompurify@3: ~3.4.13+ [email protected]: ~0.28.1+ fast-uri@3: ~3.1.5+ fast-xml-parser@5: ~5.10.1+ hono@4: ~4.12.34+ ip-address@10: ~10.3.1+ js-yaml@3: ~3.15.1+ js-yaml@4: ~4.3.1+ lodash-es@4: ~4.18.0+ mermaid@11: ~11.16.1+ nanoid@3: ~3.3.18+ nanoid@4: ~5.1.16+ nanoid@5: ~5.1.16+ next@16: ~16.2.11+ postcss@8: ~8.5.23+ [email protected]: ~0.35.0+ tar@7: ~7.5.21+ undici@6: ~6.28.0+ undici@7: ~7.29.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@floating-ui/core': 1.7.5'@floating-ui/dom': 1.7.6'@floating-ui/react-dom': 2.1.8'@floating-ui/utils': 0.2.11- '@hono/node-server@1': ~1.19.15- '@opentelemetry/core@2': ~2.8.0'@radix-ui/number': 1.1.1'@radix-ui/primitive': 1.1.4'@radix-ui/react-accessible-icon': 1.1.7@@ -113,35 +135,13 @@ overrides:'@tiptap/starter-kit': 3.22.3'@tiptap/suggestion': 3.22.3'@types/node': ^24.7.0- brace-expansion@1: ~1.1.18- brace-expansion@2: ~2.1.4- builder-util-runtime@9: ~9.7.0- dompurify@3: ~3.4.13- [email protected]: ~0.28.1- fast-uri@3: ~3.1.5- fast-xml-parser@5: ~5.10.1- hono@4: ~4.12.34- ip-address@10: ~10.3.1- js-yaml@3: ~3.15.1- js-yaml@4: ~4.3.1- lodash-es@4: ~4.18.0mdast-util-mdx-jsx: 3.2.0- mermaid@11: ~11.16.1- nanoid@3: ~3.3.18- nanoid@4: ~5.1.16- nanoid@5: ~5.1.16- next@16: ~16.2.11- postcss@8: ~8.5.23prosemirror-model: 1.25.4prosemirror-view: 1.41.8radix-ui: 1.4.3react: 19.2.5react-dom: 19.2.5react-lite-youtube-embed: 3.5.1- [email protected]: ~0.35.0- tar@7: ~7.5.21- undici@6: ~6.28.0- undici@7: ~7.29.0patchedDependencies:'@handlewithcare/[email protected]':@@ -385,8 +385,8 @@ importers:specifier: ^6.6.0version: 6.7.1'@codemirror/state':- specifier: 6.6.0- version: 6.6.0+ specifier: 6.7.1+ version: 6.7.1'@codemirror/view':specifier: 6.43.3version: 6.43.3@@ -653,7 +653,7 @@ importers:version: 8.21.0y-codemirror.next:specifier: ^0.3.5y-indexeddb:specifier: 9.0.12version: 9.0.12(patch_hash=bc95fab9af70f704280673eafabcb5a40cc180fd5122d62e6c44cac2f7076245)([email protected])@@ -1783,8 +1783,8 @@ packages:'@codemirror/[email protected]':resolution: {integrity: sha512-uMe5UO6PamJtSHrXhhHOzSX3ReWtiJrva6GnPMwSOrZtiExb5X5eExhr2OUZQVvdxPsKpY3Ro2mFbQadpPWmHA==}- '@codemirror/[email protected]':- resolution: {integrity: sha512-4nbvra5R5EtiCzr9BTHiTLc+MLXK2QGiAVYMyi8PkQd3SR+6ixar/Q/01Fa21TBIDOZXgeWV4WppsQolSreAPQ==}+ '@codemirror/[email protected]':+ resolution: {integrity: sha512-9QzNDgE4EYDnAHfrTlR2lwiPciiOymLtwKK+8yHQzCc7GXhAP9xdEbEJFy2IWB1j9UGUl9BsgMmTo/ImA02T7A==}'@codemirror/[email protected]':resolution: {integrity: sha512-MwEwCAr/o0agJefhC2+reBv5kfOQpMcDRUNQrRYZgWlhH8IwQcerMZrpqWyUFSyO0ebgN2cnh/w87F7G4BGSng==}@@ -11554,7 +11554,7 @@ packages:resolution: {integrity: sha512-VluNu3e5HfEXybnypnsGwKAj+fKLd4iAnR7JuX1Sfyydmn1jCBS5wwEL/uS04Ch2ib0DnMAOF6ZRR/8kK3wyGw==}peerDependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3yjs: ^13.5.6@@ -12141,14 +12141,14 @@ snapshots:'@codemirror/[email protected]':dependencies:'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2'@codemirror/[email protected]':dependencies:'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2@@ -12156,7 +12156,7 @@ snapshots:dependencies:'@codemirror/autocomplete': 6.20.3'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@lezer/common': 1.5.2'@lezer/css': 1.3.4@@ -12166,7 +12166,7 @@ snapshots:'@codemirror/lang-css': 6.3.1'@codemirror/lang-javascript': 6.2.5'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2'@lezer/css': 1.3.4@@ -12177,7 +12177,7 @@ snapshots:'@codemirror/autocomplete': 6.20.3'@codemirror/language': 6.12.4'@codemirror/lint': 6.9.7- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2'@lezer/javascript': 1.5.4@@ -12192,7 +12192,7 @@ snapshots:'@codemirror/autocomplete': 6.20.3'@codemirror/lang-html': 6.4.11'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2'@lezer/markdown': 1.7.1@@ -12201,7 +12201,7 @@ snapshots:dependencies:'@codemirror/autocomplete': 6.20.3'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@lezer/common': 1.5.2'@lezer/python': 1.1.19@@ -12214,7 +12214,7 @@ snapshots:dependencies:'@codemirror/autocomplete': 6.20.3'@codemirror/language': 6.12.4- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@lezer/common': 1.5.2'@lezer/highlight': 1.2.3'@lezer/lr': 1.4.10@@ -12222,7 +12222,7 @@ snapshots:'@codemirror/[email protected]':dependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3'@lezer/common': 1.5.2'@lezer/highlight': 1.2.3@@ -12235,23 +12235,23 @@ snapshots:'@codemirror/[email protected]':dependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3crelt: 1.0.7'@codemirror/[email protected]':dependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3crelt: 1.0.7- '@codemirror/[email protected]':+ '@codemirror/[email protected]':dependencies:'@marijn/find-cluster-break': 1.0.3'@codemirror/[email protected]':dependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1crelt: 1.0.7style-mod: 4.1.3w3c-keyname: 2.2.8@@ -17159,7 +17159,7 @@ snapshots:'@codemirror/language': 6.12.4'@codemirror/lint': 6.9.7'@codemirror/search': 6.7.1- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3@@ -23095,9 +23095,9 @@ snapshots:optionalDependencies:sleep: 6.1.0- [email protected](@codemirror/[email protected])(@codemirror/[email protected])([email protected]):+ [email protected](@codemirror/[email protected])(@codemirror/[email protected])([email protected]):dependencies:- '@codemirror/state': 6.6.0+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3lib0: 0.2.117yjs: 13.6.31diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex a0cbd37db..b12f70015 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -35,6 +35,11 @@ overrides:# patch advisory on the same line self-heals without another override edit,# and nothing floats to a new minor as a side effect of a security fix.#+ # A `~` is a CEILING as well as a floor, and these are NOT covered by+ # `scripts/check-override-floors.mjs`, which reasons only about exact pins.+ # `@opentelemetry/core@2` sits under packages/app's `^2.9.0` today for that+ # reason. Check by hand when adding one.+ ## THREE entries deliberately cross a breaking boundary, because the vulnerable# version is the last release on its line and there is no in-line fix at all:# nanoid@4 -> 5.x (4.0.2 is the final 4.x; consumer@@ -87,11 +92,23 @@ overrides:'tar@7': ~7.5.21'undici@6': ~6.28.0'undici@7': ~7.29.0- # CodeMirror is the source-mode editor core, held at the same bun baseline as- # the `@tiptap/*` + `prosemirror-*` pins further down this block; a fresh pnpm- # resolution otherwise floats it within the app's `^6.6.0`/`^6.43.3` carets- # (view 6.43.6 / state 6.7.1).- '@codemirror/state': 6.6.0+ # CodeMirror is the source-mode editor core. Freezing `view` is the point of+ # this pair, for the same reason the `@tiptap/*` + `prosemirror-*` pins below+ # exist: an unpinned editor core floats on every install. `state` is pinned+ # alongside it only to keep the pair explicit, so it carries whatever version+ # the frozen `view` requires rather than a stale one of its own.+ #+ # That version has a hard floor of 6.7.0. Both `@codemirror/[email protected]` and+ # `@codemirror/[email protected]` declare `"@codemirror/state": "^6.7.0"`, and+ # view calls `EditorSelection.undirectionalRange`, which 6.6.0 does not define.+ # pnpm applies an out-of-range override silently, so pinning under that floor+ # throws `TypeError: undirectionalRange is not a function` at runtime on every+ # mouse-selection path (double-click word select, triple-click line select,+ # atomic-range selection filter, widget dragstart) in every CodeMirror-backed+ # surface in the app. `scripts/check-override-floors.mjs` now fails the build+ # rather than letting that recur. When `view` is bumped, re-read its declared+ # `@codemirror/state` range and raise this to match.+ '@codemirror/state': 6.7.1'@codemirror/view': 6.43.3# Radix UI graph pinned to the versions the bun lockfile resolved. A fresh# pnpm resolution floats the whole @radix-ui/* + floating-ui graph past thediff --git a/scripts/check-override-floors.mjs b/scripts/check-override-floors.mjsnew file mode 100644index 000000000..587737749--- /dev/null+++ b/scripts/check-override-floors.mjs@@ -0,0 +1,287 @@+#!/usr/bin/env node++import fs from 'node:fs';+import path from 'node:path';+import process from 'node:process';+import { pathToFileURL } from 'node:url';++export function parseOverrideBlockLines(yamlText) {+ const out = [];+ const lines = yamlText.split('\n');+ let inBlock = false;+ for (const line of lines) {+ if (/^overrides:\s*$/.test(line)) {+ inBlock = true;+ continue;+ }+ if (!inBlock) continue;+ if (line.trim() !== '' && !/^\s/.test(line)) break;+ const m = line.match(+ /^\s+(?:'([^']+)'|"([^"]+)"|([^\s:'"#][^:]*?))\s*:\s*(?:'([^']+)'|"([^"]+)"|(\S+?))\s*(?:#.*)?$/,+ );+ if (!m) continue;+ out.push({ key: m[1] ?? m[2] ?? m[3], value: m[4] ?? m[5] ?? m[6] });+ }+ return out;+}++const EXACT_VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;++export function parseExactOverrides(yamlText) {+ const out = new Map();+ for (const { key, value } of parseOverrideBlockLines(yamlText)) {+ if (key.includes('@', 1)) continue;+ if (!EXACT_VERSION.test(value)) continue;+ out.set(key, value);+ }+ return out;+}++export function rangeFloor(range) {+ const m = String(range)+ .trim()+ .match(/^(\^|~|>=|)\s*(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/);+ return m ? m[2] : null;+}++export function compareVersions(a, b) {+ const split = (v) => {+ const [core, ...rest] = String(v).split('-');+ return [core.split('.').map(Number), rest.length ? rest.join('-') : null];+ };+ const [ac, apre] = split(a);+ const [bc, bpre] = split(b);+ for (let i = 0; i < 3; i++) {+ if (ac[i] !== bc[i]) return ac[i] - bc[i];+ }+ if (apre === bpre) return 0;+ if (apre === null) return 1;+ if (bpre === null) return -1;+ const ai = apre.split('.');+ const bi = bpre.split('.');+ for (let i = 0; i < Math.max(ai.length, bi.length); i++) {+ const x = ai[i];+ const y = bi[i];+ if (x === undefined) return -1;+ if (y === undefined) return 1;+ const xn = /^\d+$/.test(x);+ const yn = /^\d+$/.test(y);+ if (xn && yn) {+ if (Number(x) !== Number(y)) return Number(x) - Number(y);+ continue;+ }+ if (xn !== yn) return xn ? -1 : 1;+ if (x !== y) return x < y ? -1 : 1;+ }+ return 0;+}++function* installedManifests(pnpmDir) {+ let dirs;+ try {+ dirs = fs.readdirSync(pnpmDir);+ } catch {+ return;+ }+ for (const dir of dirs) {+ if (dir.startsWith('.')) continue;+ const nm = path.join(pnpmDir, dir, 'node_modules');+ let top;+ try {+ top = fs.readdirSync(nm);+ } catch {+ continue;+ }+ for (const entry of top) {+ let names = [entry];+ if (entry.startsWith('@')) {+ try {+ names = fs.readdirSync(path.join(nm, entry)).map((s) => `${entry}/${s}`);+ } catch {+ continue;+ }+ }+ for (const name of names) {+ try {+ yield JSON.parse(fs.readFileSync(path.join(nm, name, 'package.json'), 'utf8'));+ } catch {+ }+ }+ }+ }+}++export function parsePackagePatterns(yamlText) {+ const patterns = [];+ const unparsed = [];+ let inBlock = false;+ for (const line of yamlText.split('\n')) {+ if (/^packages:\s*$/.test(line)) {+ inBlock = true;+ continue;+ }+ if (!inBlock) continue;+ if (line.trim() !== '' && !/^\s/.test(line)) break;+ const trimmed = line.trim();+ if (trimmed === '' || trimmed.startsWith('#')) continue;+ const m = line.match(/^\s+-\s*(?:'([^']+)'|"([^"]+)"|([^'"#\s]+))\s*(?:#.*)?$/);+ if (m) patterns.push(m[1] ?? m[2] ?? m[3]);+ else if (trimmed.startsWith('-')) unparsed.push(trimmed);+ }+ return { patterns, unparsed };+}++export function expandPackagePattern(root, pattern) {+ if (pattern.startsWith('!')) return null;+ if (pattern.endsWith('/*')) {+ const parent = path.join(root, pattern.slice(0, -2));+ if (pattern.slice(0, -2).includes('*')) return null;+ try {+ return fs.readdirSync(parent).map((entry) => path.join(parent, entry));+ } catch {+ return [];+ }+ }+ if (pattern.includes('*')) return null;+ return [path.join(root, pattern)];+}++function readManifest(dir) {+ try {+ return JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));+ } catch {+ return null;+ }+}++export function* workspaceManifests(root, yamlText, onUnknownPattern, onEmptyPattern) {+ const { patterns, unparsed } = parsePackagePatterns(yamlText);+ for (const line of unparsed) onUnknownPattern(line);+ const expansions = [];+ for (const pattern of patterns) {+ const dirs = expandPackagePattern(root, pattern);+ if (dirs === null) {+ onUnknownPattern(pattern);+ continue;+ }+ expansions.push([pattern, dirs]);+ }+ const rootManifest = readManifest(root);+ if (rootManifest) yield rootManifest;+ if (expansions.length === 0) onEmptyPattern('the packages: block itself');+ for (const [pattern, dirs] of expansions) {+ let members = 0;+ for (const dir of dirs) {+ const manifest = readManifest(dir);+ if (!manifest) continue;+ members += 1;+ yield manifest;+ }+ if (members === 0) onEmptyPattern(pattern);+ }+}++export const DEPENDENCY_FIELDS = ['dependencies', 'peerDependencies', 'optionalDependencies'];++export const WORKSPACE_DEPENDENCY_FIELDS = [...DEPENDENCY_FIELDS, 'devDependencies'];++const byDependantThenDep = (a, b) => a.dependant.localeCompare(b.dependant) || a.dep.localeCompare(b.dep);++export function findFloorViolations(overrides, manifests, fields = DEPENDENCY_FIELDS) {+ const seen = new Set();+ const violations = [];+ for (const manifest of manifests) {+ for (const field of fields) {+ for (const [dep, range] of Object.entries(manifest?.[field] ?? {})) {+ if (field === 'peerDependencies' && manifest?.peerDependenciesMeta?.[dep]?.optional) continue;+ const pinned = overrides.get(dep);+ if (!pinned) continue;+ const floor = rangeFloor(range);+ if (!floor) continue;+ if (compareVersions(pinned, floor) >= 0) continue;+ const dependant = manifest.version ? `${manifest.name}@${manifest.version}` : manifest.name;+ const key = `${dependant} ${field} ${dep} ${range}`;+ if (seen.has(key)) continue;+ seen.add(key);+ violations.push({+ dependant,+ field,+ dep,+ range,+ floor,+ pinned,+ });+ }+ }+ }+ return violations.sort(byDependantThenDep);+}++export function collectViolations(overrides, { installed, workspace }) {+ return [+ ...findFloorViolations(overrides, installed),+ ...findFloorViolations(overrides, workspace, WORKSPACE_DEPENDENCY_FIELDS),+ ].sort(byDependantThenDep);+}++function main() {+ const root = path.resolve(import.meta.dirname, '..');+ const workspaceFile = path.join(root, 'pnpm-workspace.yaml');+ if (!fs.existsSync(workspaceFile)) {+ console.error(`check-override-floors: no pnpm-workspace.yaml beside ${root}. The subtree layout moved.`);+ process.exit(1);+ }+ const workspaceYaml = fs.readFileSync(workspaceFile, 'utf8');+ const overrides = parseExactOverrides(workspaceYaml);+ const pnpmDir = path.join(root, 'node_modules', '.pnpm');+ if (!fs.existsSync(pnpmDir)) {+ console.error('check-override-floors: node_modules/.pnpm is missing. Run `pnpm install` first.');+ process.exit(1);+ }+ const unknownPatterns = [];+ const emptyPatterns = [];+ const installed = [...installedManifests(pnpmDir)];+ const workspace = [+ ...workspaceManifests(+ root,+ workspaceYaml,+ (p) => unknownPatterns.push(p),+ (p) => emptyPatterns.push(p),+ ),+ ];+ if (unknownPatterns.length > 0) {+ console.error(+ `check-override-floors: the packages: block declares ${unknownPatterns.join(', ')}, which this expander does not understand. It reads two shapes, \`dir/*\` and a bare \`dir\`, either quoted or bare and optionally followed by a comment. Teach it the shape or the repo's own floors go unchecked.`,+ );+ process.exit(1);+ }+ if (installed.length === 0) {+ console.error(`check-override-floors: read no package manifests under ${pnpmDir}. Refusing to report a pass.`);+ process.exit(1);+ }+ if (emptyPatterns.length > 0) {+ console.error(+ `check-override-floors: in ${workspaceFile}, ${emptyPatterns.join(' and ')} matched no workspace package. Those packages' floors left the corpus silently, and the workspace root alone declares none of the pinned packages. Refusing to report a pass.`,+ );+ process.exit(1);+ }+ const total = installed.length + workspace.length;+ const violations = collectViolations(overrides, { installed, workspace });+ if (violations.length === 0) {+ console.log(+ `check-override-floors: OK (${overrides.size} exact overrides checked against ${total} installed and workspace manifests, none below a declared floor)`,+ );+ return;+ }+ console.error('check-override-floors: an override pins a package below a floor a dependant declares.\n');+ for (const v of violations) {+ console.error(` ${v.dependant}`);+ console.error(` ${v.field}: ${v.dep}@${v.range} (floor ${v.floor}), override pins ${v.pinned}`);+ }+ console.error(+ '\nRaise the override in pnpm-workspace.yaml to satisfy the highest declared floor, then run `pnpm install`.',+ );+ process.exit(1);+}++if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main();diff --git a/scripts/check-override-floors.test.mjs b/scripts/check-override-floors.test.mjsnew file mode 100644index 000000000..280831d0c--- /dev/null+++ b/scripts/check-override-floors.test.mjs@@ -0,0 +1,369 @@+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { fileURLToPath } from 'node:url';+import { describe, expect, it } from 'vitest';+import {+ collectViolations,+ compareVersions,+ expandPackagePattern,+ findFloorViolations,+ parseExactOverrides,+ parseOverrideBlockLines,+ parsePackagePatterns,+ rangeFloor,+ WORKSPACE_DEPENDENCY_FIELDS,+ workspaceManifests,+} from './check-override-floors.mjs';++const WORKSPACE = [+ 'packages:',+ " - 'packages/*'",+ '',+ 'overrides:',+ ' # a comment',+ " 'undici@7': ~7.29.0",+ " '@codemirror/state': 6.7.1",+ " '@codemirror/view': 6.43.3",+ " '[email protected]': ~0.35.0",+ " '@radix-ui/primitive': 1.1.4",+ ' prosemirror-model: 1.25.4',+ ' react: 19.2.5',+ ' "@types/node": ^24.7.0',+ ' "quoted-exact": 2.0.1',+ ' trailing-note: 3.1.0 # kept until the upstream fix lands',+ " quoted-value: '4.2.0'",+ '',+ 'packageExtensions:',+ " 'foo': 1.0.0",+].join('\n');++describe('parseExactOverrides', () => {+ const parsed = parseExactOverrides(WORKSPACE);++ it('keeps single-quoted keys pinned to an exact version', () => {+ expect(parsed.get('@codemirror/state')).toBe('6.7.1');+ expect(parsed.get('@codemirror/view')).toBe('6.43.3');+ expect(parsed.get('@radix-ui/primitive')).toBe('1.1.4');+ });++ it('keeps BARE and double-quoted keys too', () => {+ expect(parsed.get('prosemirror-model')).toBe('1.25.4');+ expect(parsed.get('react')).toBe('19.2.5');+ expect(parsed.get('quoted-exact')).toBe('2.0.1');+ });++ it('keeps a pin that grew a trailing comment', () => {+ expect(parsed.get('trailing-note')).toBe('3.1.0');+ });++ it('keeps a quoted VALUE, which would otherwise be reclassified as a range', () => {+ expect(parsed.get('quoted-value')).toBe('4.2.0');+ });++ it('skips version-selector keys and non-exact values', () => {+ expect(parsed.has('undici@7')).toBe(false);+ expect(parsed.has('[email protected]')).toBe(false);+ expect(parsed.has('@types/node')).toBe(false);+ expect(parsed.size).toBe(8);+ });++ it('stops at the end of the overrides block', () => {+ expect(parsed.has('foo')).toBe(false);+ });++ it('returns nothing when there is no overrides block', () => {+ expect(parseExactOverrides('packages:\n - a\n').size).toBe(0);+ });+});++describe('parseOverrideBlockLines against the real pnpm-workspace.yaml', () => {+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');+ const yamlText = fs.readFileSync(path.join(root, 'pnpm-workspace.yaml'), 'utf8');++ function blockEntryLines() {+ const out = [];+ let inBlock = false;+ for (const line of yamlText.split('\n')) {+ if (/^overrides:\s*$/.test(line)) {+ inBlock = true;+ continue;+ }+ if (!inBlock) continue;+ if (line.trim() !== '' && !/^\s/.test(line)) break;+ const trimmed = line.trim();+ if (trimmed === '' || trimmed.startsWith('#')) continue;+ out.push(line);+ }+ return out;+ }++ it('reads every mapping line in the block, dropping none silently', () => {+ const lines = blockEntryLines();+ expect(lines.length).toBeGreaterThan(100);+ expect(parseOverrideBlockLines(yamlText)).toHaveLength(lines.length);+ });++ it('classifies the real block the way the header says it does', () => {+ const exact = parseExactOverrides(yamlText);+ expect(exact.get('@codemirror/state')).toMatch(/^\d+\.\d+\.\d+$/);+ expect(exact.get('@codemirror/view')).toMatch(/^\d+\.\d+\.\d+$/);+ expect(exact.get('react')).toMatch(/^\d+\.\d+\.\d+$/);+ expect(exact.has('undici@7')).toBe(false);+ expect(exact.has('@types/node')).toBe(false);+ expect(exact.has('@opentelemetry/core@2')).toBe(false);+ });+});++describe('parsePackagePatterns / expandPackagePattern', () => {+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');++ it('reads the block the real workspace uses', () => {+ expect(parsePackagePatterns("packages:\n - 'packages/*'\n - 'docs'\n\noverrides:\n a: 1.0.0\n")).toEqual({+ patterns: ['packages/*', 'docs'],+ unparsed: [],+ });+ });++ it('reads the REAL block, not just a synthetic one', () => {+ const yamlText = fs.readFileSync(path.join(root, 'pnpm-workspace.yaml'), 'utf8');+ const { patterns, unparsed } = parsePackagePatterns(yamlText);+ expect(unparsed).toEqual([]);+ expect(patterns).toEqual(['packages/*', 'docs']);+ expect(expandPackagePattern(root, 'packages/*')?.length).toBe(+ fs.readdirSync(path.join(root, 'packages')).length,+ );+ });++ it('keeps a pattern that grew a trailing comment', () => {+ expect(parsePackagePatterns("packages:\n - 'packages/*' # both roots\n").patterns).toEqual(['packages/*']);+ });++ it('reports a list entry it cannot read instead of dropping it', () => {+ expect(parsePackagePatterns('packages:\n - {weird: shape}\n - docs\n')).toEqual({+ patterns: ['docs'],+ unparsed: ['- {weird: shape}'],+ });+ });++ it('declines a shape it cannot expand instead of yielding nothing', () => {+ for (const pattern of ['packages/**', 'packages/*/src', '!packages/legacy', 'packages/*-ui']) {+ expect(expandPackagePattern('/tmp', pattern), pattern).toBeNull();+ }+ });++ it('expands a bare directory without touching the disk', () => {+ expect(expandPackagePattern('/tmp', 'docs')).toEqual([path.join('/tmp', 'docs')]);+ });++ it('yields nothing for a glob whose parent is absent, which is not a shape error', () => {+ expect(expandPackagePattern('/tmp/definitely-not-here', 'packages/*')).toEqual([]);+ });+});++describe('rangeFloor', () => {+ it('reads the floor of the four supported syntaxes', () => {+ expect(rangeFloor('^6.7.0')).toBe('6.7.0');+ expect(rangeFloor('~1.1.4')).toBe('1.1.4');+ expect(rangeFloor('>=2.0.1')).toBe('2.0.1');+ expect(rangeFloor('1.2.3')).toBe('1.2.3');+ expect(rangeFloor(' ^6.7.0 ')).toBe('6.7.0');+ });++ it('declines rather than guesses on anything else', () => {+ for (const range of ['*', 'latest', '>1.0.0 <2.0.0', '1.x', '^1.2', 'workspace:*', '', '^18 || ^19']) {+ expect(rangeFloor(range)).toBeNull();+ }+ });+});++describe('compareVersions', () => {+ it('orders by numeric component, not lexically', () => {+ expect(compareVersions('6.10.0', '6.9.0')).toBeGreaterThan(0);+ expect(compareVersions('6.6.0', '6.7.0')).toBeLessThan(0);+ expect(compareVersions('6.7.1', '6.7.1')).toBe(0);+ });++ it('sorts a prerelease below its release', () => {+ expect(compareVersions('1.0.0-rc.1', '1.0.0')).toBeLessThan(0);+ expect(compareVersions('1.0.0', '1.0.0-rc.1')).toBeGreaterThan(0);+ });++ it('compares prerelease identifiers per semver, not as whole strings', () => {+ expect(compareVersions('1.0.0-rc.10', '1.0.0-rc.2')).toBeGreaterThan(0);+ expect(compareVersions('1.0.0-rc.2', '1.0.0-rc.10')).toBeLessThan(0);+ expect(compareVersions('1.0.0-alpha', '1.0.0-alpha.1')).toBeLessThan(0);+ expect(compareVersions('1.0.0-alpha.1', '1.0.0-beta')).toBeLessThan(0);+ expect(compareVersions('1.0.0-1', '1.0.0-alpha')).toBeLessThan(0);+ expect(compareVersions('1.0.0-rc.1', '1.0.0-rc.1')).toBe(0);+ });+});++describe('findFloorViolations', () => {+ const view = {+ name: '@codemirror/view',+ version: '6.43.3',+ dependencies: { '@codemirror/state': '^6.7.0', crelt: '^1.0.6' },+ };++ it('flags an override pinned below a declared floor', () => {+ expect(findFloorViolations(new Map([['@codemirror/state', '6.6.0']]), [view])).toEqual([+ {+ dependant: '@codemirror/[email protected]',+ field: 'dependencies',+ dep: '@codemirror/state',+ range: '^6.7.0',+ floor: '6.7.0',+ pinned: '6.6.0',+ },+ ]);+ });++ it('passes once the override satisfies the floor', () => {+ expect(findFloorViolations(new Map([['@codemirror/state', '6.7.1']]), [view])).toEqual([]);+ expect(findFloorViolations(new Map([['@codemirror/state', '6.7.0']]), [view])).toEqual([]);+ });++ it('reads peerDependencies, the only place React libraries declare a floor', () => {+ const lib = { name: 'some-react-lib', version: '1.0.0', peerDependencies: { react: '^19.3.0' } };+ const found = findFloorViolations(new Map([['react', '19.2.5']]), [lib]);+ expect(found).toHaveLength(1);+ expect(found[0]).toMatchObject({ field: 'peerDependencies', dep: 'react', floor: '19.3.0' });+ });++ it('reads optionalDependencies', () => {+ const lib = { name: 'next', version: '16.2.11', optionalDependencies: { sharp: '^0.36.0' } };+ const found = findFloorViolations(new Map([['sharp', '0.35.0']]), [lib]);+ expect(found).toHaveLength(1);+ expect(found[0]).toMatchObject({ field: 'optionalDependencies', floor: '0.36.0' });+ });++ it('ignores a peer marked optional, which the dependant runs without', () => {+ const lib = {+ name: 'opt',+ version: '1.0.0',+ peerDependencies: { react: '^19.3.0' },+ peerDependenciesMeta: { react: { optional: true } },+ };+ expect(findFloorViolations(new Map([['react', '19.2.5']]), [lib])).toEqual([]);+ });++ it('ignores pinning ABOVE a dependant exact pin, which is the normal use of an override', () => {+ const radix = {+ name: '@radix-ui/react-dialog',+ version: '1.1.15',+ dependencies: { '@radix-ui/primitive': '1.1.3' },+ };+ expect(findFloorViolations(new Map([['@radix-ui/primitive', '1.1.4']]), [radix])).toEqual([]);+ });++ it('reads a workspace member devDependency, which is installed unlike a third party one', () => {+ const member = {+ name: '@inkeep/open-knowledge-app',+ version: '0.67.0',+ devDependencies: { '@tiptap/starter-kit': '^3.23.0' },+ };+ expect(findFloorViolations(new Map([['@tiptap/starter-kit', '3.22.3']]), [member])).toEqual([]);+ const found = findFloorViolations(+ new Map([['@tiptap/starter-kit', '3.22.3']]),+ [member],+ WORKSPACE_DEPENDENCY_FIELDS,+ );+ expect(found).toHaveLength(1);+ expect(found[0]).toMatchObject({ field: 'devDependencies', floor: '3.23.0' });+ });++ it('ignores packages that carry no override', () => {+ expect(findFloorViolations(new Map([['unrelated', '1.0.0']]), [view])).toEqual([]);+ });++ it('skips ranges it cannot interpret rather than reporting them', () => {+ const odd = { name: 'odd', version: '1.0.0', dependencies: { dep: '>1.0.0 <3.0.0' } };+ expect(findFloorViolations(new Map([['dep', '0.1.0']]), [odd])).toEqual([]);+ });++ it('reports each dependant once even when the manifest is seen repeatedly', () => {+ expect(findFloorViolations(new Map([['@codemirror/state', '6.6.0']]), [view, view, view])).toHaveLength(1);+ });++ it('names a versionless manifest without an undefined in it', () => {+ const [violation] = findFloorViolations(+ new Map([['pinned', '1.0.0']]),+ [{ name: 'open-knowledge', devDependencies: { pinned: '^2.0.0' } }],+ WORKSPACE_DEPENDENCY_FIELDS,+ );+ expect(violation.dependant).toBe('open-knowledge');+ });++ it('tolerates manifests with no dependency fields', () => {+ expect(findFloorViolations(new Map([['a', '1.0.0']]), [{ name: 'x', version: '1' }, null])).toEqual([]);+ });+});++describe('collectViolations', () => {+ it('gives the workspace bucket the wider field list and the installed bucket the narrower one', () => {+ const third = { name: 'third-party', version: '1.0.0', devDependencies: { pinned: '^2.0.0' } };+ const member = { name: '@inkeep/member', version: '0.1.0', devDependencies: { pinned: '^2.0.0' } };+ const found = collectViolations(new Map([['pinned', '1.0.0']]), {+ installed: [third],+ workspace: [member],+ });+ expect(found).toEqual([+ expect.objectContaining({ dependant: '@inkeep/[email protected]', field: 'devDependencies' }),+ ]);+ });+});++describe('workspaceManifests', () => {+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');+ const refuse = (value) => {+ throw new Error(`unexpected callback: ${value}`);+ };++ it('reads the workspace root alongside the members', () => {+ const yamlText = fs.readFileSync(path.join(root, 'pnpm-workspace.yaml'), 'utf8');+ const names = [...workspaceManifests(root, yamlText, refuse, refuse)].map((m) => m.name);+ expect(names).toContain('open-knowledge');+ expect(names).toContain('@inkeep/open-knowledge-app');+ });++ it('reports an empty corpus even though the root always parses', () => {+ const empty = [];+ const yielded = [+ ...workspaceManifests(root, 'packages: # roots\n - undetected\n', refuse, (p) => empty.push(p)),+ ];+ expect(yielded.map((m) => m.name)).toEqual(['open-knowledge']);+ expect(empty).toEqual(['the packages: block itself']);+ });++ it('counts members AFTER reading them, so a directory with no package.json is empty', () => {+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'override-floors-'));+ try {+ fs.mkdirSync(path.join(tmp, 'pkgs', 'half-deleted'), { recursive: true });+ const empty = [];+ const yielded = [...workspaceManifests(tmp, "packages:\n - 'pkgs/*'\n", refuse, (p) => empty.push(p))];+ expect(empty).toEqual(['pkgs/*']);+ expect(yielded).toEqual([]);+ } finally {+ fs.rmSync(tmp, { recursive: true, force: true });+ }+ });++ it('names the pattern that went empty, not just the total', () => {+ const empty = [];+ const yaml = "packages:\n - 'workspaces/*'\n - 'docs'\n";+ const names = [...workspaceManifests(root, yaml, refuse, (p) => empty.push(p))].map((m) => m.name);+ expect(empty).toEqual(['workspaces/*']);+ expect(names).toContain('@inkeep/open-knowledge-docs');+ });+});++describe('wiring', () => {+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');++ it('stays registered in the check:drift:guards chain', () => {+ const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));+ expect(pkg.scripts['check:drift:guards']).toContain('node scripts/check-override-floors.mjs');+ });++});