Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

heavy: bootstrap Docker dependency layer survives new postinstall imports

garrytan6443ee82ec1f
diff --git a/scripts/postinstall.ts b/scripts/postinstall.ts
index 42e1788e3d..870cf8cb11 100644
--- a/scripts/postinstall.ts
+++ b/scripts/postinstall.ts
@@ -20,12 +20,23 @@
// to quote). It NEVER fails the install: every path exits 0.
import { which } from 'bun';
-import { unsupportedBunMessage } from '../src/core/runtime-version.ts';
+
+// Repo modules load dynamically inside try/catch: a static import of a file
+// that is not on disk (a Docker dependency layer, a partial checkout) would
+// exit 1 before any line here runs and fail the whole install.
+async function runtimeRefusalMessage(): Promise<string | null> {
+ try {
+ const { unsupportedBunMessage } = await import('../src/core/runtime-version.ts');
+ return unsupportedBunMessage();
+ } catch {
+ return null;
+ }
+}
// The install itself runs on the user's Bun, so this is the first place an
// upgrade onto a newer Bun floor can say so. Nothing else here would work:
// every gbrain command refuses until Bun is upgraded.
-const runtimeRefusal = unsupportedBunMessage();
+const runtimeRefusal = await runtimeRefusalMessage();
if (runtimeRefusal) {
console.error(`[gbrain] Installed, but gbrain will not start on this Bun.\n${runtimeRefusal}`);
process.exit(0);
diff --git a/test/scripts/postinstall-standalone.test.ts b/test/scripts/postinstall-standalone.test.ts
new file mode 100644
index 0000000000..b2fe6c494d
--- /dev/null
+++ b/test/scripts/postinstall-standalone.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, test } from 'bun:test';
+import { spawnSync } from 'node:child_process';
+import { copyFileSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, resolve } from 'node:path';
+
+const REPO = resolve(import.meta.dir, '..', '..');
+
+describe('scripts/postinstall.ts exits 0 on every path', () => {
+ test('runs from a directory holding only package.json and the script (Docker dependency layer)', () => {
+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-postinstall-standalone-'));
+ try {
+ mkdirSync(join(dir, 'scripts'));
+ copyFileSync(join(REPO, 'package.json'), join(dir, 'package.json'));
+ copyFileSync(join(REPO, 'scripts', 'postinstall.ts'), join(dir, 'scripts', 'postinstall.ts'));
+ const result = spawnSync(process.execPath, ['--no-env-file', join(dir, 'scripts', 'postinstall.ts')], {
+ cwd: dir,
+ env: { HOME: dir, GBRAIN_HOME: dir, PATH: '/usr/bin:/bin' },
+ encoding: 'utf8',
+ timeout: 30_000,
+ });
+ expect(result.status, result.stderr + result.stdout).toBe(0);
+ expect(result.stderr).toContain('postinstall skipped');
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+});
diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile
index fd814abdfb..3aa716c98a 100644
--- a/tests/docker/Dockerfile
+++ b/tests/docker/Dockerfile
@@ -17,10 +17,13 @@ RUN apt-get update \
WORKDIR /app
-# Dependency layer first for build caching. postinstall.ts must be present
-# before `bun install` (package.json wires it as the postinstall hook; it
-# exits 0 on every path by contract).
+# Dependency layer first for build caching. Lifecycle scripts stay on
+# (@electric-sql/pglite is a trustedDependency), so postinstall.ts must be
+# present before `bun install`; it loads every repo module dynamically and
+# exits 0 on every path, so it needs nothing else from src/. patches/ is
+# copied so a future `patchedDependencies` entry cannot break this layer.
COPY package.json bun.lock bunfig.toml ./
+COPY patches/ patches/
COPY scripts/postinstall.ts scripts/postinstall.ts
RUN bun install --frozen-lockfile