Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(server): run the CLI on Node versions without import.meta.main (#7141)
Carlos Jimenez3fd50643311d
diff --git a/apps/server/src/bin.ts b/apps/server/src/bin.tsindex 3370a2299..8d2ee75ac 100644--- a/apps/server/src/bin.ts+++ b/apps/server/src/bin.ts@@ -12,6 +12,7 @@ import { connectCommand } from "./cli/connect.ts";import { pairCommand } from "./cli/pair.ts";import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";import { sharedServerCommandFlags } from "./cli/config.ts";+import { isEntrypoint } from "./entrypoint.ts";import { projectCommand } from "./cli/project.ts";import { runServerCommand, serveCommand, startCommand } from "./cli/server.ts";import { serviceCommand } from "./cli/service.ts";@@ -63,7 +64,13 @@ export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>export const cli = makeCli();-if (import.meta.main) {+if (+ isEntrypoint({+ moduleUrl: import.meta.url,+ entryPath: process.argv[1],+ runtimeMain: import.meta.main,+ })+) {Command.run(cli, { version: packageJson.version }).pipe(Effect.scoped,Effect.provide(CliRuntimeLayer),diff --git a/apps/server/src/entrypoint.test.ts b/apps/server/src/entrypoint.test.tsnew file mode 100644index 000000000..56f2c1197--- /dev/null+++ b/apps/server/src/entrypoint.test.ts@@ -0,0 +1,89 @@+// @effect-diagnostics nodeBuiltinImport:off - entrypoint detection is a Node filesystem boundary.+import * as NodeFS from "node:fs";+import * as NodeOS from "node:os";+import * as NodePath from "node:path";+import * as NodeURL from "node:url";++import { describe, expect, it } from "vite-plus/test";++import { isEntrypoint } from "./entrypoint.ts";++const makeTempDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-entrypoint-test-"));++describe("isEntrypoint", () => {+ it("uses the runtime answer when Node provides one", () => {+ // Node 22.18+ and 24.2+ populate `import.meta.main`; nothing else is consulted.+ expect(+ isEntrypoint({+ moduleUrl: "file:///somewhere/bin.mjs",+ entryPath: "/elsewhere/other.mjs",+ runtimeMain: true,+ }),+ ).toBe(true);+ expect(+ isEntrypoint({+ moduleUrl: "file:///somewhere/bin.mjs",+ entryPath: "/somewhere/bin.mjs",+ runtimeMain: false,+ }),+ ).toBe(false);+ });++ it("matches the entrypoint path when the runtime has no import.meta.main", () => {+ // Node 22.16, 22.17 and 23.11 are inside `engines.node` but leave it undefined.+ const dir = makeTempDir();+ const entry = NodePath.join(dir, "bin.mjs");+ NodeFS.writeFileSync(entry, "");++ expect(+ isEntrypoint({+ moduleUrl: NodeURL.pathToFileURL(entry).href,+ entryPath: entry,+ runtimeMain: undefined,+ }),+ ).toBe(true);+ });++ it("matches through a symlinked entrypoint, as npm and npx install it", () => {+ const dir = makeTempDir();+ const real = NodePath.join(dir, "bin.mjs");+ const link = NodePath.join(dir, "t3");+ NodeFS.writeFileSync(real, "");+ NodeFS.symlinkSync(real, link);++ expect(+ isEntrypoint({+ moduleUrl: NodeURL.pathToFileURL(real).href,+ entryPath: link,+ runtimeMain: undefined,+ }),+ ).toBe(true);+ });++ it("stays false for an imported module that is not the entrypoint", () => {+ // This is what keeps `bin.test.ts` from launching the CLI on import.+ const dir = makeTempDir();+ const entry = NodePath.join(dir, "bin.mjs");+ const imported = NodePath.join(dir, "cli.mjs");+ NodeFS.writeFileSync(entry, "");+ NodeFS.writeFileSync(imported, "");++ expect(+ isEntrypoint({+ moduleUrl: NodeURL.pathToFileURL(imported).href,+ entryPath: entry,+ runtimeMain: undefined,+ }),+ ).toBe(false);+ });++ it("stays false when there is no entrypoint argument", () => {+ expect(+ isEntrypoint({+ moduleUrl: "file:///somewhere/bin.mjs",+ entryPath: undefined,+ runtimeMain: undefined,+ }),+ ).toBe(false);+ });+});diff --git a/apps/server/src/entrypoint.ts b/apps/server/src/entrypoint.tsnew file mode 100644index 000000000..1ac083ec5--- /dev/null+++ b/apps/server/src/entrypoint.ts@@ -0,0 +1,38 @@+// @effect-diagnostics nodeBuiltinImport:off+// Entrypoint detection runs before any Effect runtime is built, so it stays on+// Node built-ins.+import * as NodeFS from "node:fs";+import * as NodeURL from "node:url";++/**+ * Whether the module identified by `moduleUrl` is the process entrypoint.+ *+ * `import.meta.main` answers this directly, but it only exists on Node 22.18++ * and 24.2+. This package's `engines.node` range also accepts 22.16, 22.17 and+ * 23.11, where it is `undefined`: an `if (import.meta.main)` guard never runs,+ * so the process loads every module and exits 0 without output. Fall back to+ * comparing the entrypoint path on those versions.+ */+export const isEntrypoint = (input: {+ readonly moduleUrl: string;+ readonly entryPath: string | undefined;+ readonly runtimeMain: boolean | undefined;+}): boolean => {+ if (input.runtimeMain !== undefined) {+ return input.runtimeMain;+ }+ if (input.entryPath === undefined || input.entryPath === "") {+ return false;+ }+ if (input.moduleUrl === NodeURL.pathToFileURL(input.entryPath).href) {+ return true;+ }+ // npm and npx install the CLI as a symlink. Without `--preserve-symlinks` the+ // module URL is the resolved real path while `process.argv[1]` keeps the link+ // path, so the comparison above misses.+ try {+ return input.moduleUrl === NodeURL.pathToFileURL(NodeFS.realpathSync(input.entryPath)).href;+ } catch {+ return false;+ }+};diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.tsindex 00fb4e410..68f3c3467 100644--- a/apps/server/src/serviceLauncher.ts+++ b/apps/server/src/serviceLauncher.ts@@ -27,6 +27,7 @@ import {SERVICE_STATE_FILE,SERVICE_STOP_MARKER_FILE,} from "./cloud/serviceProtocol.ts";+import { isEntrypoint } from "./entrypoint.ts";const HANDOFF_DELAY_MS = 2_000;const PREPARED_TIMEOUT_MS = 120_000;@@ -611,7 +612,13 @@ async function main(): Promise<void> {await new Launcher(baseDir, state).run();}-if (import.meta.main) {+if (+ isEntrypoint({+ moduleUrl: import.meta.url,+ entryPath: process.argv[1],+ runtimeMain: import.meta.main,+ })+) {main().catch((cause: unknown) => {const error = cause instanceof Error ? cause : new Error(String(cause));process.stderr.write(`[service-launcher] ${error.message}\n`);