The Open Framework for Building Agents
fix(eve): fix withEve build in npm (non-pnpm) workspaces (#830)
Ben Pankowd9d322693bb1
diff --git a/.changeset/withEve-hoisted-eve-binary.md b/.changeset/withEve-hoisted-eve-binary.mdnew file mode 100644index 000000000..68d1773ae--- /dev/null+++ b/.changeset/withEve-hoisted-eve-binary.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Fix `withEve` producing a broken Vercel build command in npm workspaces, where the `eve` module is located in the workspace root.diff --git a/packages/eve/src/public/next/index.integration.test.ts b/packages/eve/src/public/next/index.integration.test.tsindex 4dde0753c..e88eef348 100644--- a/packages/eve/src/public/next/index.integration.test.ts+++ b/packages/eve/src/public/next/index.integration.test.ts@@ -4,6 +4,17 @@ import { join } from "node:path";import { afterEach, describe, expect, it, vi } from "vitest";+vi.mock("./resolve-eve-binary.js", async () => {+ const { join } = await import("node:path");+ return {+ // Pin resolution to the conventional app-local path so build-command+ // assertions stay deterministic without a real eve install on disk. The+ // real resolver is exercised in resolve-eve-binary.integration.test.ts.+ resolveEveBinaryPath: (nextRoot: string) =>+ join(nextRoot, "node_modules", "eve", "bin", "eve.js"),+ };+});+import { withEve, type EveNextConfig, type EveNextRewriteSections } from "./index.js";interface TestConfig extends EveNextConfig {diff --git a/packages/eve/src/public/next/index.test.ts b/packages/eve/src/public/next/index.test.tsindex 95b1ea9b0..845c05209 100644--- a/packages/eve/src/public/next/index.test.ts+++ b/packages/eve/src/public/next/index.test.ts@@ -1,5 +1,16 @@import { afterEach, describe, expect, it, vi } from "vitest";+vi.mock("./resolve-eve-binary.js", async () => {+ const { join } = await import("node:path");+ return {+ // Pin resolution to the conventional app-local path so the default+ // build-command assertion is deterministic without touching the+ // filesystem. The real resolver is covered by its own integration test.+ resolveEveBinaryPath: (nextRoot: string) =>+ join(nextRoot, "node_modules", "eve", "bin", "eve.js"),+ };+});+vi.mock("./vercel-output-config.js", () => ({ensureEveVercelOutputConfig: vi.fn(async (input: {diff --git a/packages/eve/src/public/next/index.ts b/packages/eve/src/public/next/index.tsindex ef9418e99..a19cb4ede 100644--- a/packages/eve/src/public/next/index.ts+++ b/packages/eve/src/public/next/index.ts@@ -1,8 +1,9 @@-import { isAbsolute, join, relative, resolve } from "node:path";+import { isAbsolute, relative, resolve } from "node:path";import type { NextConfig } from "next";import { EVE_ROUTE_PREFIX } from "#protocol/routes.js";+import { resolveEveBinaryPath } from "./resolve-eve-binary.js";import { resolveEveDestinationPrefix } from "./server.js";import { ensureEveVercelOutputConfig } from "./vercel-output-config.js";@@ -330,7 +331,7 @@ function createDefaultBuildCommand(input: {readonly nextRoot: string;}): string {const eveBinaryPath = toPosixPath(- relative(input.agentRoot, join(input.nextRoot, "node_modules", "eve", "bin", "eve.js")),+ relative(input.agentRoot, resolveEveBinaryPath(input.nextRoot)),);return `node ${quoteShellArg(eveBinaryPath)} build`;}diff --git a/packages/eve/src/public/next/resolve-eve-binary.integration.test.ts b/packages/eve/src/public/next/resolve-eve-binary.integration.test.tsnew file mode 100644index 000000000..a1067eec8--- /dev/null+++ b/packages/eve/src/public/next/resolve-eve-binary.integration.test.ts@@ -0,0 +1,44 @@+import { mkdir, mkdtemp, realpath, symlink, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";++import { describe, expect, it } from "vitest";++import { resolveEveBinaryPath } from "./resolve-eve-binary.js";++// Writes a minimal eve package at `dir` and returns its realpath'd bin path+// (createRequire canonicalizes symlinks, and macOS routes tmpdir through one).+async function writeEvePackage(dir: string): Promise<string> {+ await mkdir(join(dir, "bin"), { recursive: true });+ await writeFile(join(dir, "package.json"), JSON.stringify({ name: "eve", version: "0.0.0" }));+ await writeFile(join(dir, "bin", "eve.js"), "#!/usr/bin/env node\n");+ return join(await realpath(dir), "bin", "eve.js");+}++describe("resolveEveBinaryPath", () => {+ it("resolves eve hoisted to the workspace root (npm workspaces)", async () => {+ const workspaceRoot = await mkdtemp(join(tmpdir(), "eve-resolve-"));+ const expected = await writeEvePackage(join(workspaceRoot, "node_modules", "eve"));++ // The app has no eve under its own node_modules; npm hoisted it up.+ const appRoot = join(workspaceRoot, "apps", "web");+ await mkdir(appRoot, { recursive: true });+ await writeFile(join(appRoot, "package.json"), JSON.stringify({ name: "web" }));++ // realpath both sides so Windows short (8.3) paths compare equal.+ expect(await realpath(resolveEveBinaryPath(appRoot))).toBe(expected);+ });++ it("resolves eve through pnpm's virtual-store symlink", async () => {+ const appRoot = await mkdtemp(join(tmpdir(), "eve-resolve-"));+ await writeFile(join(appRoot, "package.json"), JSON.stringify({ name: "web" }));++ // pnpm installs the real package under .pnpm and symlinks node_modules/eve+ // to it; the resolver must follow the link to the store.+ const storeRoot = join(appRoot, "node_modules", ".pnpm", "[email protected]", "node_modules", "eve");+ const expected = await writeEvePackage(storeRoot);+ await symlink(storeRoot, join(appRoot, "node_modules", "eve"), "junction");++ expect(await realpath(resolveEveBinaryPath(appRoot))).toBe(expected);+ });+});diff --git a/packages/eve/src/public/next/resolve-eve-binary.ts b/packages/eve/src/public/next/resolve-eve-binary.tsnew file mode 100644index 000000000..65258edd8--- /dev/null+++ b/packages/eve/src/public/next/resolve-eve-binary.ts@@ -0,0 +1,22 @@+import { createRequire } from "node:module";+import { dirname, join } from "node:path";++/**+ * Resolves the absolute path to the installed eve binary from the app's+ * perspective.+ *+ * Uses module resolution rather than assuming an app-local `node_modules/eve`:+ * npm workspaces hoist eve to the workspace root, so the app-local path does+ * not exist there, while pnpm symlinks it app-locally. eve does not export+ * `./bin/eve.js`, but it does export `./package.json`, so we resolve that and+ * derive the bin path from the package root. Falls back to the conventional+ * app-local path when eve cannot be resolved (e.g. before install).+ */+export function resolveEveBinaryPath(nextRoot: string): string {+ try {+ const require = createRequire(join(nextRoot, "package.json"));+ return join(dirname(require.resolve("eve/package.json")), "bin", "eve.js");+ } catch {+ return join(nextRoot, "node_modules", "eve", "bin", "eve.js");+ }+}