diff --git a/src/main.ts b/src/main.ts index 3d32798b70..f2afec3901 100644 --- a/src/main.ts +++ b/src/main.ts @@ -109,10 +109,20 @@ export const runEntrypoint = async ( } }; +/** Return false when the runtime's argv path is synthetic or no longer exists. */ +const isDirectEntrypoint = ( + entryPath: string | undefined, + moduleUrl: string, +): boolean => { + if (entryPath === undefined) return false; + try { + return pathToFileURL(realpathSync(entryPath)).href === moduleUrl; + } catch { + return false; + } +}; + const entryPath = process.argv[1]; -if ( - entryPath !== undefined && - pathToFileURL(realpathSync(entryPath)).href === import.meta.url -) { +if (isDirectEntrypoint(entryPath, import.meta.url)) { void runEntrypoint(); } diff --git a/tests/boundary/mcp/mainErrors.test.ts b/tests/boundary/mcp/mainErrors.test.ts index 2a6ea0f42b..c3738e5e2d 100644 --- a/tests/boundary/mcp/mainErrors.test.ts +++ b/tests/boundary/mcp/mainErrors.test.ts @@ -1,4 +1,7 @@ import type { ServeStdioOptions } from "@modelcontextprotocol/server/stdio"; +import { spawnSync } from "node:child_process"; +import { resolve } from "node:path"; +import { pathToFileURL } from "node:url"; import { describe, expect, it } from "vitest"; import { run, runEntrypoint } from "../../../src/main.js"; @@ -35,6 +38,25 @@ const dependencies = ({ }); describe("MCP runtime errors", () => { + it("imports the compiled runtime when argv[1] is a missing synthetic path", () => { + const runtimeUrl = pathToFileURL(resolve("dist/main.js")).href; + const script = [ + 'process.argv[1] = "/rea-test-missing-entrypoint/main.js";', + `await import(${JSON.stringify(runtimeUrl)});`, + 'console.log("IMPORT_OK");', + ].join("\n"); + const child = spawnSync( + process.execPath, + ["--input-type=module", "--eval", script], + { cwd: process.cwd(), encoding: "utf8", timeout: 5_000 }, + ); + + expect(child.error).toBeUndefined(); + expect(child.status).toBe(0); + expect(child.stdout).toBe("IMPORT_OK\n"); + expect(child.stderr).toBe(""); + }); + it("reports connection loss without the transport cause", async () => { const output: string[] = []; const shutdown: Array<() => void> = [];