Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/builder/src/services/CompilerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <file> <command> [args...]`
* - macOS: `script -q <file> sh -c "<command>" sh [args...]`
* - Linux: `script -qec "<command> [args...]" <file>`
*
* util-linux `script` exits 0 whatever the child did unless `-e` is given.
Expand All @@ -62,14 +68,22 @@ async function spawnWithPty(
let scriptArgs: string[];

if (platform() === 'darwin') {
// macOS: script -q <file> <command> [args...]
scriptArgs = ['-q', tmpFile, 'compact', ...args];
// macOS: script -q <file> sh -c "<command>" sh [args...]
scriptArgs = [
'-q',
tmpFile,
'sh',
'-c',
`${SIZE_PTY}; exec compact "$@"`,
'sh',
...args,
];
} else {
// Linux: script -qec "<command>" <file>
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, {
Expand Down
33 changes: 33 additions & 0 deletions packages/builder/test/CompilerServicePty.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading