|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * ALI-1135: what `align mcp --setup` WRITES must be spawnable by a client that does not use a |
| 4 | + * shell. `node smoke-mcp-config-spawn.mjs <installed-cli-package-dir>`. |
| 5 | + * |
| 6 | + * The install smoke already asserts that `align mcp --setup` exits 0, and that a bare `align` |
| 7 | + * completes an MCP handshake when spawned THROUGH a shell. Neither of those can see this |
| 8 | + * defect. The setup step writes no config at all on CI, because the harness exports a synthetic |
| 9 | + * HOME and detectEditors() then finds no editor; and the handshake step passes |
| 10 | + * `shell: process.platform === 'win32'`, which is precisely the thing VS Code does not do. |
| 11 | + * |
| 12 | + * So this drives the write itself, through the INSTALLED package's own writer, and then spawns |
| 13 | + * exactly what landed in the file with `shell: false` - the way a client spawns it. On Windows |
| 14 | + * an npm global install exposes `align.cmd`, which no shell-less spawn can launch, so a config |
| 15 | + * naming a bare `align` fails here and a `cmd /c align` one does not. |
| 16 | + * |
| 17 | + * MEASURED, rather than assumed. A temporary probe on this branch spawned the OLD form |
| 18 | + * (`command: "align"`, shell:false) on both legs of this matrix: |
| 19 | + * windows-latest -> spawned=false, `spawn align ENOENT` (the defect, reproduced) |
| 20 | + * ubuntu-latest -> spawned=true (the control: POSIX unaffected) |
| 21 | + * It was reverted rather than kept, because a permanent assertion that a bare `align` FAILS |
| 22 | + * would go red the day npm ships a real executable, which would be good news. |
| 23 | + * |
| 24 | + * Exit codes: 0 on a config that spawns and answers `initialize`; 1 on a bad config, a spawn |
| 25 | + * failure or a protocol failure; 2 on usage; 124 on timeout. |
| 26 | + */ |
| 27 | +import { spawn } from 'node:child_process'; |
| 28 | +import { existsSync, mkdtempSync, readFileSync } from 'node:fs'; |
| 29 | +import { tmpdir } from 'node:os'; |
| 30 | +import path from 'node:path'; |
| 31 | +import { pathToFileURL } from 'node:url'; |
| 32 | + |
| 33 | +const pkgDir = process.argv[2]; |
| 34 | +if (!pkgDir) { |
| 35 | + console.error('usage: smoke-mcp-config-spawn.mjs <installed-cli-package-dir>'); |
| 36 | + process.exit(2); |
| 37 | +} |
| 38 | + |
| 39 | +// A missing subject passes any assertion below for the wrong reason, so it is fatal rather |
| 40 | +// than a failure (tdd.md, "make a missing subject FATAL, not a test"). |
| 41 | +const writerPath = path.join(pkgDir, 'dist', 'lib', 'mcp-setup.js'); |
| 42 | +if (!existsSync(writerPath)) { |
| 43 | + console.error(`FATAL: ${writerPath} does not exist - the installed package has no MCP writer to drive`); |
| 44 | + process.exit(2); |
| 45 | +} |
| 46 | + |
| 47 | +const TIMEOUT_MS = 30_000; |
| 48 | +const fail = (msg) => { console.error(`smoke-mcp-config-spawn: ${msg}`); process.exit(1); }; |
| 49 | + |
| 50 | +/** Can this harness SEE an unspawnable command? If not, its pass below means nothing. */ |
| 51 | +function negativeControl() { |
| 52 | + return new Promise((resolve) => { |
| 53 | + const bogus = spawn('align-no-such-binary-ali1135', ['mcp'], { stdio: 'ignore', shell: false }); |
| 54 | + bogus.on('error', () => resolve(true)); |
| 55 | + bogus.on('spawn', () => { bogus.kill(); resolve(false); }); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +const { writeMcpConfig } = await import(pathToFileURL(writerPath).href); |
| 60 | + |
| 61 | +if (!(await negativeControl())) { |
| 62 | + fail('negative control did not fire: a command that cannot exist spawned anyway, so a clean pass here proves nothing'); |
| 63 | +} |
| 64 | +console.log('smoke-mcp-config-spawn: negative control OK - an unspawnable command is detected'); |
| 65 | + |
| 66 | +const dir = mkdtempSync(path.join(tmpdir(), 'align-mcp-cfg-')); |
| 67 | +const configPath = path.join(dir, 'mcp.json'); |
| 68 | +writeMcpConfig({ name: 'VS Code', configPath, format: 'vscode' }, 'local'); |
| 69 | + |
| 70 | +const entry = JSON.parse(readFileSync(configPath, 'utf8')).servers?.align; |
| 71 | +if (!entry) fail(`no servers.align entry in the config that was just written (${configPath})`); |
| 72 | +console.log(`smoke-mcp-config-spawn: wrote ${JSON.stringify(entry)}`); |
| 73 | + |
| 74 | +// The command has to be spawnable AND still run our server. A config that spawns something |
| 75 | +// else would satisfy the handshake-free half of this check and ship a broken setup. |
| 76 | +const argv = [entry.command, ...(entry.args ?? [])].join(' '); |
| 77 | +if (!argv.includes('align') || !argv.includes('mcp')) fail(`the written entry does not invoke align mcp: ${argv}`); |
| 78 | + |
| 79 | +const child = spawn(entry.command, entry.args, { stdio: ['pipe', 'pipe', 'inherit'], shell: false }); |
| 80 | + |
| 81 | +let timedOut = false; |
| 82 | +const timer = setTimeout(() => { |
| 83 | + timedOut = true; |
| 84 | + console.error(`smoke-mcp-config-spawn: no initialize response within ${TIMEOUT_MS / 1000}s`); |
| 85 | + child.kill('SIGKILL'); |
| 86 | + setTimeout(() => process.exit(124), 2000).unref(); |
| 87 | +}, TIMEOUT_MS); |
| 88 | + |
| 89 | +let done = false; |
| 90 | +function finish(code) { |
| 91 | + if (done) return; |
| 92 | + done = true; |
| 93 | + clearTimeout(timer); |
| 94 | + try { child.kill(); } catch { /* already gone */ } |
| 95 | + process.exit(code); |
| 96 | +} |
| 97 | + |
| 98 | +child.on('error', (err) => { |
| 99 | + console.error( |
| 100 | + `smoke-mcp-config-spawn: the config align just wrote is NOT spawnable on ${process.platform}: ` + |
| 101 | + `spawn ${entry.command} failed with ${err.message}`, |
| 102 | + ); |
| 103 | + finish(1); |
| 104 | +}); |
| 105 | + |
| 106 | +child.on('exit', (code) => { |
| 107 | + if (timedOut) finish(124); |
| 108 | + else if (!done) { console.error(`smoke-mcp-config-spawn: server exited early (code ${code})`); finish(1); } |
| 109 | +}); |
| 110 | + |
| 111 | +let buffer = ''; |
| 112 | +child.stdout.on('data', (chunk) => { |
| 113 | + buffer += chunk.toString(); |
| 114 | + let idx; |
| 115 | + while ((idx = buffer.indexOf('\n')) !== -1) { |
| 116 | + const line = buffer.slice(0, idx).trim(); |
| 117 | + buffer = buffer.slice(idx + 1); |
| 118 | + if (!line) continue; |
| 119 | + let msg; |
| 120 | + try { |
| 121 | + msg = JSON.parse(line); |
| 122 | + } catch { |
| 123 | + console.error(`smoke-mcp-config-spawn: non-JSON on stdout: ${line.slice(0, 200)}`); |
| 124 | + finish(1); |
| 125 | + return; |
| 126 | + } |
| 127 | + if (msg.id === 1) { |
| 128 | + if (!msg.result) { console.error(`smoke-mcp-config-spawn: initialize returned no result: ${line.slice(0, 200)}`); finish(1); return; } |
| 129 | + console.log(`smoke-mcp-config-spawn: OK - the written config spawned without a shell and answered initialize on ${process.platform}`); |
| 130 | + finish(0); |
| 131 | + } |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +child.stdin.write(`${JSON.stringify({ |
| 136 | + jsonrpc: '2.0', |
| 137 | + id: 1, |
| 138 | + method: 'initialize', |
| 139 | + params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'align-config-spawn-smoke', version: '0.0.0' } }, |
| 140 | +})}\n`); |
0 commit comments