Visual Studio Code, rebuilt in Rust
Rust
test(docs): fail on a cited path or symbol that is gone
rokarturd188c14771b0
diff --git a/port/tools/docs-check.test.ts b/port/tools/docs-check.test.tsnew file mode 100644index 00000000000..2e19bf9d952--- /dev/null+++ b/port/tools/docs-check.test.ts@@ -0,0 +1,39 @@+import { expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++// A repo with one crate file (`open_window`, and an unsafe block), the architecture text and the unsafe inventory given.+function check(architecture: string, inventory = '| `app/src/main.rs` | libc | a fact |') {+ const root = mkdtempSync(join(tmpdir(), 'docs-check-test-'));+ mkdirSync(join(root, 'native/crates/app/src'), { recursive: true });+ mkdirSync(join(root, 'docs'));+ writeFileSync(join(root, 'native/crates/app/src/main.rs'), 'fn open_window() {\n // SAFETY: a fact.\n unsafe { libc::getpid() };\n}\n');+ writeFileSync(join(root, 'ARCHITECTURE.md'), architecture);+ writeFileSync(join(root, 'docs/memory-safety.md'), `| File | Calls | Relies on |\n|---|---|---|\n${inventory}\n`);+ const run = Bun.spawnSync(['bun', join(import.meta.dir, 'docs-check.ts'), root]);+ rmSync(root, { recursive: true });+ return { code: run.exitCode, out: run.stdout.toString() };+}++test('live paths, symbols and links pass, from the repo root or from a crate', () => {+ const { code, out } = check('`app/src/main.rs::open_window`, `native/crates/app/src/main.rs` and [the rules](docs/memory-safety.md#rules). Upstream `src/vs/base/common/uri.ts` and `port/patches/*.patch` are not checked.');+ expect(out).toContain('every cited path and symbol exists');+ expect(code).toBe(0);+});++test('a dead path, symbol or link fails with its line', () => {+ expect(check('ok\n`app/src/gone.rs`').out).toContain('ARCHITECTURE.md:2: `app/src/gone.rs` does not exist');+ expect(check('`app/src/main.rs::close_window`').out).toContain('ARCHITECTURE.md:1: `close_window` is not in app/src/main.rs');+ expect(check('[gone](docs/gone.md)').out).toContain('ARCHITECTURE.md:1: `docs/gone.md` does not exist');+ expect(check('`app/src/gone.rs`').code).toBe(1);+});++test('a Rust file cited from inside a crate fails: it would pass unchecked', () => {+ expect(check('`workbench/keyboard.rs`').out).toContain('`workbench/keyboard.rs` does not start at the repo root or a crate');+});++test('the unsafe inventory and the files that hold unsafe code must agree', () => {+ expect(check('', '').out).toContain('app/src/main.rs holds unsafe code and is not in the inventory');+ expect(check('', '| `app/src/main.rs` | libc | a fact |\n| `app/src/safe.rs` | none | none |').out).toContain('app/src/safe.rs is in the inventory and holds no unsafe code');+});diff --git a/port/tools/docs-check.ts b/port/tools/docs-check.tsnew file mode 100644index 00000000000..2f5b78a056e--- /dev/null+++ b/port/tools/docs-check.ts@@ -0,0 +1,96 @@+#!/usr/bin/env bun+// Fails when a path, a `path::Symbol` or a link cited in ARCHITECTURE.md, AGENTS.md, CONTRIBUTING.md or docs/ no longer+// exists, and when the unsafe inventory of docs/memory-safety.md and the files that hold `unsafe` disagree.+// Prints each dead reference as file:line and exits 1 if any. Run after moving code or editing the docs:+// bun port/tools/docs-check.ts [repo root]+// A cited path starts at the repo root (`port/tools/bench`) or at a crate of native/crates (`app/src/main.rs`).+// `app/src/main.rs::open_window` also needs the word `open_window` in that file. Not checked: upstream paths+// (`src/vs/...`), build outputs under native/target, and words with a glob or a placeholder (`*`, `<name>`).+import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';+import { dirname, join } from 'node:path';++const root = process.argv[2] ?? join(import.meta.dir, '../..');+const crates = join(root, 'native/crates');+const crateNames = existsSync(crates) ? readdirSync(crates) : [];+const roots = ['native', 'port', 'docs', '.github', '.agents', ...crateNames];+const errors = new Set<string>();++const resolve = (path: string) => [join(root, path), join(crates, path)].find(existsSync);++function checkWord(word: string, where: string) {+ const [path, ...symbols] = word.split('::');+ if (!path.includes('/') || /[*<>${}]|:\/\//.test(word) || path.startsWith('native/target/')) {+ return;+ }+ if (!roots.includes(path.split('/')[0])) {+ // A Rust file cited from inside a crate (`workbench/keyboard.rs`) would pass unchecked.+ if (path.endsWith('.rs')) {+ errors.add(`${where}: \`${path}\` does not start at the repo root or a crate (app/src/...)`);+ }+ return;+ }+ const file = resolve(path);+ if (!file) {+ errors.add(`${where}: \`${path}\` does not exist`);+ return;+ }+ if (symbols.length === 0) {+ return;+ }+ const text = statSync(file).isFile() ? readFileSync(file, 'utf8') : '';+ for (const symbol of symbols) {+ if (!new RegExp(`\\b${symbol}\\b`).test(text)) {+ errors.add(`${where}: \`${symbol}\` is not in ${path}`);+ }+ }+}++function checkDoc(doc: string) {+ readFileSync(join(root, doc), 'utf8').split('\n').forEach((line, index) => {+ const where = `${doc}:${index + 1}`;+ for (const [, target] of line.matchAll(/\]\(([^)#\s]+)(?:#[^)]*)?\)/g)) {+ if (!/^[a-z]+:/.test(target) && !existsSync(join(root, dirname(doc), target))) {+ errors.add(`${where}: \`${target}\` does not exist`);+ }+ }+ for (const word of line.split(/[\s`'"()[\],;|]+/)) {+ checkWord(word.replace(/#.*$/, '').replace(/[.:]+$/, ''), where);+ }+ });+}++// The inventory: the table rows of docs/memory-safety.md that start with a path.+function checkUnsafeInventory() {+ const doc = 'docs/memory-safety.md';+ if (!existsSync(join(root, doc))) {+ return;+ }+ const listed = new Set([...readFileSync(join(root, doc), 'utf8').matchAll(/^\| `([^`]+\.rs)` \|/gm)].map(match => match[1]));+ const holders = new Set<string>();+ for (const name of readdirSync(crates, { recursive: true }) as string[]) {+ const code = name.endsWith('.rs') ? readFileSync(join(crates, name), 'utf8').replace(/\/\/.*$/gm, '') : '';+ if (/\bunsafe\b/.test(code)) {+ holders.add(name.replaceAll('\\', '/'));+ }+ }+ for (const file of holders.difference(listed)) {+ errors.add(`${doc}: ${file} holds unsafe code and is not in the inventory`);+ }+ for (const file of listed.difference(holders)) {+ errors.add(`${doc}: ${file} is in the inventory and holds no unsafe code`);+ }+}++const docsDir = join(root, 'docs');+const docs = existsSync(docsDir) ? (readdirSync(docsDir, { recursive: true }) as string[]).filter(name => name.endsWith('.md')).map(name => `docs/${name}`) : [];+for (const doc of ['ARCHITECTURE.md', 'AGENTS.md', 'CONTRIBUTING.md', ...docs]) {+ if (existsSync(join(root, doc))) {+ checkDoc(doc);+ }+}+checkUnsafeInventory();+for (const error of errors) {+ console.log(error);+}+console.log(errors.size === 0 ? 'docs-check: every cited path and symbol exists' : `docs-check: ${errors.size} dead references`);+process.exit(errors.size > 0 ? 1 : 0);