fix(find-browse): guard main() with import.meta.main

Round 7 of windows-free-tests fixes (and a genuine bug fix beyond Windows).

browse/src/find-browse.ts called main() unconditionally at module load.
main() calls process.exit(1) when no compiled `browse` binary exists at the
known install paths. Any test that imports `locateBinary` from this module
then exits the entire test process before any tests run.

This affected the windows-free-tests CI lane because the runner intentionally
doesn't compile the browse binary (only server-node.mjs is built — full
binary compilation is slow and not needed for the curated subset). It would
also affect any Mac/Linux contributor who runs tests in a fresh checkout
before running ./setup, though the symptom is rarer there.

Fix: wrap `main()` in `if (import.meta.main) { main() }`. The CLI invocation
(via the find-browse binary or `bun run browse/src/find-browse.ts`) still
runs main() and emits the path. Imports get only the named exports.

Verified locally:
  - `bun run browse/src/find-browse.ts` still prints the binary path.
  - `import { locateBinary } from '...'` no longer exits the process.
  - `bun test browse/test/find-browse.test.ts` passes 4/4 (was crashing
    at module load).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-04-28 00:09:42 -07:00
parent 9cc846d0fe
commit 9e6b850f50
+9 -1
View File
@@ -58,4 +58,12 @@ function main() {
console.log(bin);
}
main();
// Only run main() when this module is the entry point. Without this guard,
// any test that imports `locateBinary` from this file would have main() fire
// at module-load time, calling process.exit(1) when no compiled binary
// exists — killing the test process before any test runs. Surfaced on the
// windows-free-tests CI lane where the runner has no compiled browse
// binary (intentional — that lane only builds server-node.mjs).
if (import.meta.main) {
main();
}