diff --git a/packages/builder/src/services/CompilerService.ts b/packages/builder/src/services/CompilerService.ts index 81efae94..21188450 100644 --- a/packages/builder/src/services/CompilerService.ts +++ b/packages/builder/src/services/CompilerService.ts @@ -31,6 +31,12 @@ function tokenizeFlags(flags: string): string[] { ); } +/** + * Sizes the PTY before the compiler starts. `script` opens it 0x0 when stdin + * is not a terminal, and `compactc` draws no progress row below the last line. + */ +const SIZE_PTY = 'stty rows 500 cols 200 2>/dev/null'; + /** * Spawn `compact` under a pseudo-terminal using the `script` command so that * `compactc` emits its full progress UI (circuit constraint lines with k/rows @@ -41,7 +47,7 @@ function tokenizeFlags(flags: string): string[] { * a temp file, which is read after the process exits. * * Handles both macOS and Linux `script` syntax: - * - macOS: `script -q [args...]` + * - macOS: `script -q sh -c "" sh [args...]` * - Linux: `script -qec " [args...]" ` * * util-linux `script` exits 0 whatever the child did unless `-e` is given. @@ -62,14 +68,22 @@ async function spawnWithPty( let scriptArgs: string[]; if (platform() === 'darwin') { - // macOS: script -q [args...] - scriptArgs = ['-q', tmpFile, 'compact', ...args]; + // macOS: script -q sh -c "" sh [args...] + scriptArgs = [ + '-q', + tmpFile, + 'sh', + '-c', + `${SIZE_PTY}; exec compact "$@"`, + 'sh', + ...args, + ]; } else { // Linux: script -qec "" const cmd = ['compact', ...args] .map((a) => (a.includes(' ') ? `'${a}'` : a)) .join(' '); - scriptArgs = ['-qec', cmd, tmpFile]; + scriptArgs = ['-qec', `${SIZE_PTY}; ${cmd}`, tmpFile]; } const proc = spawn('script', scriptArgs, { diff --git a/packages/builder/test/CompilerServicePty.test.ts b/packages/builder/test/CompilerServicePty.test.ts new file mode 100644 index 00000000..8b14bf9a --- /dev/null +++ b/packages/builder/test/CompilerServicePty.test.ts @@ -0,0 +1,33 @@ +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { delimiter, join } from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { CompilerService } from '../src/Compiler.js'; + +describe('CompilerService under a PTY', () => { + let binDir: string; + let savedPath: string | undefined; + + beforeEach(() => { + binDir = mkdtempSync(join(tmpdir(), 'fake-compact-')); + // Stands in for `compact`: prints the size of the terminal it runs in. + writeFileSync(join(binDir, 'compact'), '#!/bin/sh\nstty size\n'); + chmodSync(join(binDir, 'compact'), 0o755); + savedPath = process.env.PATH; + process.env.PATH = `${binDir}${delimiter}${savedPath}`; + }); + + afterEach(() => { + process.env.PATH = savedPath; + rmSync(binDir, { recursive: true, force: true }); + }); + + it('gives the compiler a 500-row, 200-column terminal', async () => { + const { stdout } = await new CompilerService().compileFile( + 'Any.compact', + '', + ); + + expect(stdout).toMatch(/^500 200\r?$/m); + }); +});