Files
gstack/test/build-script-shell-compat.test.ts
Garry Tan 8a24e896f1 fix(build): extract package.json build to scripts/build.sh for Windows Bun compat (#1538, #1537, #1530, #1457, #1561)
Bun's Windows shell parser rejects multiple constructs the inline
package.json build chain used: brace groups `{ cmd; }`, subshells with
redirection `( git ... ) > path/.version`, and (in Bun 1.3.x) subshells
near redirections in general. Every Windows install + every
auto-upgrade since v1.34.2.0 has failed on `bun run build`.

Extracts the build chain to scripts/build.sh and the .version writes to
scripts/write-version-files.sh. POSIX-portable, no Bun shell parsing
involved. Also adds Windows-specific bun.exe handling for non-ASCII
PATHs (a separate Windows footgun where Bun's --compile fails when the
binary lives under a path with non-ASCII chars).

Updates test/build-script-shell-compat.test.ts to assert the new shape:
no subshells with redirections anywhere in the build chain, and build
delegates to scripts/build.sh which delegates .version writes.

Contributed by @Charlie-El via #1544. Supersedes #1531 (@scarson, fixed
in build helper), #1480 (@mikepsinn, partial overlap), #1460
(@realcarsonterry, brace-group fix subsumed) — credit retained.
Closes #1538, #1537, #1530, #1457, #1561.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 20:59:34 -07:00

55 lines
2.1 KiB
TypeScript

import { describe, test, expect } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
const ROOT = path.resolve(import.meta.dir, '..');
const PKG = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8')) as {
scripts: Record<string, string>;
};
const BUILD_SCRIPT = fs.readFileSync(path.join(ROOT, 'scripts', 'build.sh'), 'utf-8');
// Strip single-quoted strings so JS code emitted as `echo '{ ... }'` doesn't
// trip the shell-brace-group check. Conservative: only `'...'` segments.
function stripSingleQuoted(s: string): string {
return s.replace(/'[^']*'/g, "''");
}
describe('package.json build scripts — POSIX shell compat (D-1460)', () => {
// Bun's Windows shell parser doesn't grok bash brace groups `{ cmd; }`.
// Bun 1.3.x on Windows also rejects subshells when the subshell or the
// command inside it uses redirection, so redirected commands must be direct.
test('no bash brace groups in any npm script', () => {
const offending: { script: string; pattern: string }[] = [];
for (const [name, body] of Object.entries(PKG.scripts)) {
const stripped = stripSingleQuoted(body);
const match = stripped.match(/\{\s+[^}]*;\s*\}/);
if (match) {
offending.push({ script: name, pattern: match[0] });
}
}
expect(offending).toEqual([]);
});
test('build script has no subshells with redirections', () => {
const offending: { script: string; pattern: string }[] = [];
for (const [name, body] of Object.entries({ build: PKG.scripts.build ?? '' })) {
const matches = [
...body.matchAll(/\([^)]*[<>][^)]*\)/g),
...body.matchAll(/\([^)]*\)\s*[<>]/g),
];
for (const match of matches) {
offending.push({ script: name, pattern: match[0] });
}
}
expect(offending).toEqual([]);
});
test('build script delegates .version writes to a shell script', () => {
// Bun rejects `( git ... ) > path/.version`.
const build = PKG.scripts.build ?? '';
expect(build).not.toMatch(/>\s*\S+\/\.version/);
expect(build).toBe('bash scripts/build.sh');
expect(BUILD_SCRIPT).toContain('bash scripts/write-version-files.sh');
});
});