From 06a9b587198980ca084bece4429e2d4cefccd4c5 Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Thu, 12 Nov 2026 17:27:21 -0600 Subject: [PATCH] fix: make packaged spellchecking independent of build paths --- .github/workflows/ci.yml | 3 + CHANGELOG.md | 1 + README.md | 5 ++ package.json | 3 +- scripts/build-spell.mjs | 68 ++++++++++++++++++++++ scripts/packagedSpellSmoke.mjs | 100 +++++++++++++++++++++++++++++++++ src/extension/panelSession.ts | 9 +-- src/spell/runtime.mts | 24 ++++++++ src/spell/spellDiagnostics.ts | 51 +++++++---------- 9 files changed, 229 insertions(+), 35 deletions(-) create mode 100644 scripts/build-spell.mjs create mode 100644 scripts/packagedSpellSmoke.mjs create mode 100644 src/spell/runtime.mts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c4d4042..7399685 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,9 @@ jobs: - name: Verify packaged VSIX run: node scripts/verify-universal-vsix.mjs + - name: Smoke packaged spelling without checkout access + run: node scripts/packagedSpellSmoke.mjs + - name: Audit production dependencies run: npm audit --omit=dev --audit-level=low diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c56540..1103dce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Add CI for typecheck, tests, build, universal VSIX packaging, and production audit; set the public `atomicdotdev` publisher metadata and 1.0.0 version. - Fix release preparation on the `release` branch, synchronize and commit lockfile versions, and accept dated changelog headings without requiring a horizontal-rule separator. - Use an empty regular Git configuration file for release regression fixtures instead of an OS null-device path, which Git for Windows rejects. +- Fix packaged spellchecking by shipping its JavaScript engine and dictionary/configuration dependency tree, resolving it from the installed extension, and awaiting defaults before merging project settings; verify archived diagnostics, corrections, and imported project dictionaries without checkout access. ## 0.2.12 - Package one universal, JavaScript-only VSIX; Atomic's platform library is supplied by the separate Atomic installation on the extension host. diff --git a/README.md b/README.md index 8194ebd..38d240d 100644 --- a/README.md +++ b/README.md @@ -106,12 +106,17 @@ npm run test:atomic:graph npm run package:vsix # Recheck an existing archive (defaults to the current manifest's release filename). npm run verify:vsix +# Check archived spell diagnostics, corrections, and imported project dictionaries. +# Runs Node with checkout filesystem access denied; no browser/native runtime needed. +node scripts/packagedSpellSmoke.mjs ``` The extension build bundles only JavaScript/protobuf dependencies as `dist/nodeatomic/index.mjs`. It checks lazy import, codec access and missing-runtime rejection in **Node**, not Bun. With an explicit runtime override it additionally checks actual native dispatch; an import alone is not evidence that a library loaded. The default build does not copy, remove or overwrite native libraries. `.vscodeignore` excludes legacy `dist/native/**` and native filenames even if old build artifacts remain on disk. `package:vsix` deliberately sets no platform target. It validates the generated archive's identity/version, rejects platform-targeted manifests and native payloads, and runs the exact archived consumer from an isolated temporary directory without `node_modules`. Native dispatch is also checked when a runtime override is present. This guards against accidental sibling-checkout dependencies and stale native assets. `verify:vsix` accepts an archive path as an optional argument. Neither command publishes to the Marketplace. +Spellchecking ships its JavaScript engine, package metadata, configuration, and dictionaries under `dist/spell`; these files are loaded from the installed extension rather than bundled build-machine paths. Workspace `cspell.json` imports and relative custom dictionaries remain supported. CI exercises the exact archived spell runtime in a separate Node process that cannot read the checkout. The spelling smoke accepts an optional VSIX path. + **Universal packaging is not a claim of tested support on every host.** The external producer currently supplies macOS ARM64, Linux x64 glibc, and Windows x64 runtimes. Validate the same VSIX inside the actual VS Code/Electron extension host on each platform before claiming compatibility. Node-only integration tests do not establish Electron compatibility. Remote hosts need their own matching runtime. Browser preview does not exercise this panel. External Atomic installers and self-update must preserve the complete CLI/library pair; this extension does not implement those installation paths. Alternatively, open this repository in VS Code and press **F5** to launch its Extension Development Host. Open `examples/sherpa.intent.md` there as **Atomic Intent Ticket**. diff --git a/package.json b/package.json index fba926c..c89ea9f 100644 --- a/package.json +++ b/package.json @@ -939,9 +939,10 @@ "typecheck:webview": "tsc -p webview/tsconfig.json --noEmit", "build": "bun run build:webview && bun run build:extension", "build:webview": "bun scripts/build-webview.mjs", - "build:extension": "bun scripts/clean-dist.mjs && bun run build:extension:graph && bun run build:extension:atomic && bun run build:extension:main && bun run build:extension:export-runtime && bun run build:extension:puppeteer-runtime", + "build:extension": "bun scripts/clean-dist.mjs && bun run build:extension:graph && bun run build:extension:atomic && bun run build:extension:spell && bun run build:extension:main && bun run build:extension:export-runtime && bun run build:extension:puppeteer-runtime", "build:extension:graph": "bun scripts/build-graph.mjs", "build:extension:atomic": "bun scripts/build-nodeatomic.mjs", + "build:extension:spell": "bun scripts/build-spell.mjs", "build:extension:main": "bun build src/extension.ts --target=node --format=cjs --minify --outfile=dist/extension.js -e vscode", "build:extension:main:debug": "bun build src/extension.ts --target=node --format=cjs --minify --outfile=dist/extension.js -e vscode --sourcemap=inline", "build:extension:export-runtime": "bun build src/export/runtime.ts --target=node --format=cjs --minify --outfile=dist/export-runtime.js", diff --git a/scripts/build-spell.mjs b/scripts/build-spell.mjs new file mode 100644 index 0000000..e59a1ec --- /dev/null +++ b/scripts/build-spell.mjs @@ -0,0 +1,68 @@ +import { cp, mkdir, readFile, rm } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const projectRoot = fileURLToPath(new URL('../', import.meta.url)); +const nodeModules = path.join(projectRoot, 'node_modules'); +const outdir = path.join(projectRoot, 'dist', 'spell'); + +// Resolve package directories without package exports restrictions: cspell is +// import-only, and several dictionary packages do not export package.json. +async function findPackage(name, from, optional = false) { + const require = createRequire(path.join(from, 'package.json')); + for (const base of require.resolve.paths(name) ?? []) { + const directory = path.join(base, name); + try { + const manifest = JSON.parse(await readFile(path.join(directory, 'package.json'), 'utf8')); + return { directory, manifest }; + } catch (error) { + if (error.code !== 'ENOENT' && error.code !== 'ENOTDIR') throw error; + } + } + if (optional) return undefined; + throw new Error(`Cannot find spell runtime dependency ${name} from ${from}`); +} + +await rm(outdir, { recursive: true, force: true }); +await mkdir(outdir, { recursive: true }); +const result = await Bun.build({ + entrypoints: [path.join(projectRoot, 'src', 'spell', 'runtime.mts')], + target: 'node', + format: 'esm', + external: ['cspell-lib'], + outdir, + naming: 'runtime.mjs' +}); +if (!result.success) throw new AggregateError(result.logs, 'Spell runtime build failed'); + +// Preserve the installed dependency layout, including nested version conflicts. +// Copy data and package metadata as well as JS; dictionary/config imports are +// dynamic and cannot be discovered reliably by a JavaScript bundler. +const pending = [await findPackage('cspell-lib', projectRoot)]; +const copied = new Set(); +while (pending.length) { + const { directory, manifest } = pending.pop(); + if (copied.has(directory)) continue; + copied.add(directory); + const relative = path.relative(nodeModules, directory); + if (relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) { + throw new Error(`Spell runtime dependency is outside node_modules: ${directory}`); + } + await cp(directory, path.join(outdir, 'node_modules', relative), { + recursive: true, + dereference: true, + filter: (source) => source === directory || path.basename(source) !== 'node_modules' + }); + const optionalDependencies = manifest.optionalDependencies ?? {}; + const dependencies = { + ...manifest.dependencies, + ...manifest.peerDependencies, + ...optionalDependencies + }; + for (const name of Object.keys(dependencies)) { + const optional = name in optionalDependencies || manifest.peerDependenciesMeta?.[name]?.optional; + const dependency = await findPackage(name, directory, optional); + if (dependency) pending.push(dependency); + } +} diff --git a/scripts/packagedSpellSmoke.mjs b/scripts/packagedSpellSmoke.mjs new file mode 100644 index 0000000..9650adb --- /dev/null +++ b/scripts/packagedSpellSmoke.mjs @@ -0,0 +1,100 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { createWriteStream } from 'node:fs'; +import { mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import { tmpdir } from 'node:os'; +import { dirname, join, resolve } from 'node:path'; +import { pipeline } from 'node:stream/promises'; +import { fileURLToPath } from 'node:url'; + +const tooling = createRequire(import.meta.resolve('@vscode/vsce')); +const { open } = tooling('yauzl'); + +async function extractSpellRuntime(archive, directory) { + await new Promise((resolveArchive, reject) => { + open(archive, { lazyEntries: true, strictFileNames: true }, (error, zip) => { + if (error) return reject(error); + const fail = (error) => { zip.close(); reject(error); }; + zip.on('error', fail); + zip.on('end', resolveArchive); + zip.on('entry', (entry) => { + const prefix = 'extension/dist/spell/'; + if (!entry.fileName.startsWith(prefix) || entry.fileName.endsWith('/')) return zip.readEntry(); + const destination = join(directory, entry.fileName.slice(prefix.length)); + zip.openReadStream(entry, async (error, stream) => { + if (error) return fail(error); + try { + await mkdir(dirname(destination), { recursive: true }); + await pipeline(stream, createWriteStream(destination)); + zip.readEntry(); + } catch (error) { fail(error); } + }); + }); + zip.readEntry(); + }); + }); +} + +// Run the real archived engine in a separate Node process. Filesystem permissions +// make the checkout unreadable even when it still exists on the build machine. +const worker = ` +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { pathToFileURL } from 'node:url'; +const [directory, sourceManifest] = process.argv.slice(2); +await assert.rejects(readFile(sourceManifest), { code: 'ERR_ACCESS_DENIED' }); +const { checkDocument } = await import(pathToFileURL(join(directory, 'spell/runtime.mjs')).href); +const document = (project, text) => ({ uri: pathToFileURL(join(directory, project, 'source.md')).href, text, languageId: 'markdown' }); +const plain = await checkDocument(document('plain', 'This is a misspelled wrod.'), true); +assert.equal(plain.checked, true); +assert.equal(plain.errors?.length ?? 0, 0); +assert.deepEqual(plain.issues.map(({ text, offset, length, suggestions }) => ({ text, offset, length, suggestion: suggestions?.[0] })), + [{ text: 'wrod', offset: 21, length: 4, suggestion: 'word' }]); +const correct = await checkDocument(document('plain', 'This is a correctly spelled word.'), false); +assert.equal(correct.checked, true); +assert.deepEqual(correct.issues, []); +const unconfigured = await checkDocument(document('plain', 'The quzzlekorp wrod.'), false); +assert.deepEqual(unconfigured.issues.map(issue => issue.text), ['quzzlekorp', 'wrod']); +const configured = await checkDocument(document('project', 'The quzzlekorp wrod.'), true); +assert.equal(configured.checked, true); +assert.equal(configured.errors?.length ?? 0, 0); +assert.deepEqual(configured.issues.map(issue => ({ text: issue.text, suggestion: issue.suggestions?.[0] })), [{ text: 'wrod', suggestion: 'word' }]); +console.log('Packaged spelling passed: diagnostics, corrections, imported project dictionary; checkout reads denied.'); +`; + +export async function smokePackagedSpelling(archive) { + const directory = await realpath(await mkdtemp(join(tmpdir(), 'sherpa-packaged-spell-'))); + try { + await writeFile(join(directory, 'package.json'), JSON.stringify({ private: true, type: 'module' })); + // Stop configuration discovery at the fixture, not at unrelated parent directories. + await writeFile(join(directory, 'cspell.json'), JSON.stringify({ version: '0.2' })); + await extractSpellRuntime(archive, join(directory, 'spell')); + await mkdir(join(directory, 'plain'), { recursive: true }); + await mkdir(join(directory, 'project/config'), { recursive: true }); + await writeFile(join(directory, 'project/cspell.json'), JSON.stringify({ version: '0.2', import: ['./config/spelling.json'] })); + await writeFile(join(directory, 'project/config/spelling.json'), JSON.stringify({ + version: '0.2', dictionaryDefinitions: [{ name: 'project-words', path: './words.txt' }], dictionaries: ['project-words'] + })); + await writeFile(join(directory, 'project/config/words.txt'), 'quzzlekorp\n'); + const entry = join(directory, 'smoke.mjs'); + await writeFile(entry, worker); + const env = { ...process.env, HOME: directory, USERPROFILE: directory, APPDATA: directory, LOCALAPPDATA: directory, + XDG_CONFIG_HOME: directory, XDG_DATA_HOME: directory, NODE_OPTIONS: '', NODE_PATH: '' }; + const permissionFlag = process.allowedNodeEnvironmentFlags.has('--permission') ? '--permission' : '--experimental-permission'; + const result = spawnSync(process.execPath, [ + permissionFlag, `--allow-fs-read=${directory}`, entry, directory, + fileURLToPath(new URL('../package.json', import.meta.url)) + ], { cwd: directory, env, encoding: 'utf8', timeout: 60_000 }); + assert.equal(result.status, 0, `Packaged spelling failed: ${result.error?.message ?? result.signal ?? ''}\n${result.stdout}\n${result.stderr}`); + console.log(result.stdout.trim()); + } finally { + await rm(directory, { recursive: true, force: true }); + } +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const pkg = JSON.parse(await readFile(new URL('../package.json', import.meta.url), 'utf8')); + await smokePackagedSpelling(process.argv[2] ?? `releases/${pkg.name}-${pkg.version}.vsix`); +} diff --git a/src/extension/panelSession.ts b/src/extension/panelSession.ts index a4de7a4..5085a4f 100644 --- a/src/extension/panelSession.ts +++ b/src/extension/panelSession.ts @@ -554,7 +554,7 @@ export function createPanelSessionController(params: PanelSessionControllerParam const runSpellCheck = async (generation: number): Promise => { try { const diagnostics = document.textDocument - ? await collectMeoSpellDiagnostics(document.textDocument) + ? await collectMeoSpellDiagnostics(document.textDocument, context.extensionUri) : []; if (disposed || generation !== spellCheckGeneration) { return; @@ -1025,7 +1025,7 @@ export function createPanelSessionController(params: PanelSessionControllerParam return; } case 'requestDiagnosticSuggestions': { - const response = await resolveDiagnosticSuggestions(document, raw); + const response = await resolveDiagnosticSuggestions(document, raw, context.extensionUri); await postToWebview(response); return; } @@ -1407,7 +1407,8 @@ function serializeDiagnostics(document: MarkdownCustomDocument): SerializedDiagn async function resolveDiagnosticSuggestions( document: MarkdownCustomDocument, - request: RequestDiagnosticSuggestionsMessage + request: RequestDiagnosticSuggestionsMessage, + extensionUri: vscode.Uri ): Promise { const emptyResponse: DiagnosticSuggestionsResultMessage = { type: 'diagnosticSuggestionsResult', @@ -1465,7 +1466,7 @@ async function resolveDiagnosticSuggestions( if (suggestions.length === 0 && request.source === MEO_SPELL_DIAGNOSTIC_SOURCE) { const spellSuggestions = document.textDocument - ? await collectMeoSpellSuggestions(document.textDocument, requestedRange.from, requestedRange.to) + ? await collectMeoSpellSuggestions(document.textDocument, requestedRange.from, requestedRange.to, extensionUri) : []; for (const suggestion of spellSuggestions) { if (seen.has(suggestion)) { diff --git a/src/spell/runtime.mts b/src/spell/runtime.mts new file mode 100644 index 0000000..c951151 --- /dev/null +++ b/src/spell/runtime.mts @@ -0,0 +1,24 @@ +import { + getDefaultSettings, + mergeSettings, + searchForConfig, + spellCheckDocument +} from 'cspell-lib'; + +// Keep cspell and its configuration/dictionary packages as native modules: their +// module-relative resolution must use the installed extension, not build paths. +export async function checkDocument( + document: Parameters[0], + generateSuggestions: boolean +): ReturnType { + const [defaults, localConfig] = await Promise.all([ + getDefaultSettings(), + searchForConfig(document.uri) + ]); + const settings = localConfig ? mergeSettings(defaults, localConfig) : defaults; + return spellCheckDocument( + document, + { generateSuggestions }, + generateSuggestions ? { ...settings, numSuggestions: 1, suggestionsTimeout: 750 } : settings + ); +} diff --git a/src/spell/spellDiagnostics.ts b/src/spell/spellDiagnostics.ts index 104f81e..0072b4d 100644 --- a/src/spell/spellDiagnostics.ts +++ b/src/spell/spellDiagnostics.ts @@ -1,17 +1,18 @@ import * as vscode from 'vscode'; -import { - getDefaultSettings, - mergeSettings, - searchForConfig, - spellCheckDocument, - type CSpellUserSettings, - type ValidationIssue -} from 'cspell-lib'; +import { pathToFileURL } from 'node:url'; +import type { ValidationIssue } from 'cspell-lib' with { 'resolution-mode': 'import' }; +import type * as SpellRuntime from './runtime.mjs' with { 'resolution-mode': 'import' }; export const MEO_SPELL_DIAGNOSTIC_SOURCE = 'MEO Spell'; const maxSpellCheckTextLength = 1_000_000; const maxSpellSuggestions = 1; +let spellRuntime: Promise | undefined; + +function loadSpellRuntime(extensionUri: vscode.Uri): Promise { + spellRuntime ??= import(pathToFileURL(vscode.Uri.joinPath(extensionUri, 'dist', 'spell', 'runtime.mjs').fsPath).href); + return spellRuntime; +} export function shouldRunMeoSpellCheck(document: vscode.TextDocument): boolean { if (document.uri.scheme !== 'file') { @@ -41,21 +42,23 @@ export function hasExternalSpellDiagnostics(document: vscode.TextDocument): bool }); } -export async function collectMeoSpellDiagnostics(document: vscode.TextDocument): Promise { +export async function collectMeoSpellDiagnostics( + document: vscode.TextDocument, + extensionUri: vscode.Uri +): Promise { if (!shouldRunMeoSpellCheck(document) || hasExternalSpellDiagnostics(document)) { return []; } const text = document.getText().replace(/\r\n?/g, '\n'); - const settings = await resolveCSpellSettings(document); - const result = await spellCheckDocument( + const { checkDocument } = await loadSpellRuntime(extensionUri); + const result = await checkDocument( { uri: document.uri.toString(), text, languageId: document.languageId || 'markdown' }, - { generateSuggestions: false }, - settings + false ); if (!result.checked) { @@ -68,26 +71,22 @@ export async function collectMeoSpellDiagnostics(document: vscode.TextDocument): export async function collectMeoSpellSuggestions( document: vscode.TextDocument, normalizedFrom: number, - normalizedTo: number + normalizedTo: number, + extensionUri: vscode.Uri ): Promise { if (!shouldRunMeoSpellCheck(document)) { return []; } const text = document.getText().replace(/\r\n?/g, '\n'); - const settings = await resolveCSpellSettings(document); - const result = await spellCheckDocument( + const { checkDocument } = await loadSpellRuntime(extensionUri); + const result = await checkDocument( { uri: document.uri.toString(), text, languageId: document.languageId || 'markdown' }, - { generateSuggestions: true }, - { - ...settings, - numSuggestions: maxSpellSuggestions, - suggestionsTimeout: 750 - } + true ); if (!result.checked) { @@ -105,14 +104,6 @@ export async function collectMeoSpellSuggestions( return uniqueSuggestions(issue?.suggestions ?? [], issue?.text ?? '').slice(0, maxSpellSuggestions); } -async function resolveCSpellSettings(document: vscode.TextDocument): Promise { - const localConfig = await searchForConfig(document.uri.fsPath); - if (!localConfig) { - return getDefaultSettings(); - } - return mergeSettings(getDefaultSettings(), localConfig); -} - function uniqueSuggestions(suggestions: string[], original: string): string[] { const seen = new Set(); const normalizedOriginal = original.toLowerCase();