Skip to content

Visual Studio Code, rebuilt in Rust

Rust
4.5 KB
Raw Blame
#!/usr/bin/env bun
// Fails when a path, a `path::Symbol` or a link cited in ARCHITECTURE.md, AGENTS.md, CONTRIBUTING.md, GLOSSARY.md or
// docs/ no longer exists, when the unsafe inventory of docs/memory-safety.md and the files that hold `unsafe` disagree,
// and when a crate of native/crates has no row in the crate table of ARCHITECTURE.md.
// 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) || /^native\/target(\/|$)/.test(path)) {
		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`);
	}
}

function checkCrateTable() {
	const doc = 'ARCHITECTURE.md';
	if (!existsSync(crates) || !existsSync(join(root, doc))) {
		return;
	}
	const text = readFileSync(join(root, doc), 'utf8');
	for (const entry of readdirSync(crates, { withFileTypes: true })) {
		if (entry.isDirectory() && !text.includes(`| \`${entry.name}\` |`)) {
			errors.add(`${doc}: the crate ${entry.name} has no row in the crate table`);
		}
	}
}

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', 'GLOSSARY.md', ...docs]) {
	if (existsSync(join(root, doc))) {
		checkDoc(doc);
	}
}
checkUnsafeInventory();
checkCrateTable();
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);