Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): avoid runtime package resolution for bundled identity (#2481)

vercel-gh-bot-5[bot]9626748809a6
diff --git a/.changeset/calm-functions-start.md b/.changeset/calm-functions-start.md
new file mode 100644
index 000000000..e49e609cb
--- /dev/null
+++ b/.changeset/calm-functions-start.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Use build-stamped package metadata in bundled runtimes so serverless functions can start without resolving `eve/package.json` at runtime.
diff --git a/packages/eve/src/internal/application/package.ts b/packages/eve/src/internal/application/package.ts
index c018cf3d2..22e9f690b 100644
--- a/packages/eve/src/internal/application/package.ts
+++ b/packages/eve/src/internal/application/package.ts
@@ -3,31 +3,16 @@ import { createRequire } from "node:module";
import { basename, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
+import { readStampedPackageVersion } from "#internal/application/stamped-package-version.js";
import { EVE_PACKAGE_NAME } from "#internal/package-name.js";
let cachedPackageInfo: InstalledPackageInfo | undefined;
let cachedPackageLocation: PackageLocation | undefined;
-// The package build stamps the published version into `dist` so bundled
-// deployments can still report package metadata without resolving package.json.
-const BUNDLED_FALLBACK_PACKAGE_VERSION: string = "__EVE_PACKAGE_VERSION__";
const WORKFLOW_MODULE_ALIASES = {
"workflow/errors": "src/compiled/@workflow/errors/index.js",
"workflow/internal/private": "src/compiled/@workflow/core/private.js",
} as const;
-function resolveFallbackPackageVersion(): string {
- // Detect an unstamped build by the token's `__` shape — spelling the token
- // out in a comparison would get rewritten by the stamp itself.
- return BUNDLED_FALLBACK_PACKAGE_VERSION.startsWith("__")
- ? "0.0.0"
- : BUNDLED_FALLBACK_PACKAGE_VERSION;
-}
-
-const FALLBACK_PACKAGE_INFO: InstalledPackageInfo = {
- name: EVE_PACKAGE_NAME,
- version: resolveFallbackPackageVersion(),
-};
-
interface InstalledPackageInfo {
name: string;
version: string;
@@ -133,6 +118,24 @@ function isSourceCheckout(packageRoot: string): boolean {
return existsSync(join(packageRoot, "src", "internal", "application", "package.ts"));
}
+function findNearestSourceCheckoutRoot(startDirectory: string): string | undefined {
+ let currentDirectory = startDirectory;
+
+ while (true) {
+ if (isSourceCheckout(currentDirectory)) {
+ return currentDirectory;
+ }
+
+ const parentDirectory = dirname(currentDirectory);
+
+ if (parentDirectory === currentDirectory) {
+ return undefined;
+ }
+
+ currentDirectory = parentDirectory;
+ }
+}
+
function tryCreatePackageLocation(packageRoot: string): PackageLocation | undefined {
if (isSourceCheckout(packageRoot)) {
return {
@@ -233,6 +236,25 @@ function tryResolvePackageRoot(): string | undefined {
}
}
+function tryResolveLocalPackageRoot(currentModulePath: string): string | undefined {
+ try {
+ const canonicalModulePath = realpathSync.native(currentModulePath);
+ const directBuildLocation = tryResolveDirectBuildLocation(canonicalModulePath);
+
+ if (directBuildLocation !== undefined) {
+ return directBuildLocation.packageRoot;
+ }
+
+ const sourceCheckoutRoot = findNearestSourceCheckoutRoot(dirname(canonicalModulePath));
+
+ return sourceCheckoutRoot === undefined
+ ? undefined
+ : tryResolveVerifiedPackageRoot(join(sourceCheckoutRoot, "package.json"));
+ } catch {
+ return undefined;
+ }
+}
+
function rewriteSourceFilePathForBuild(relativeSourcePath: string): string {
return relativeSourcePath.replace(/\.[cm]?tsx?$/, ".js");
}
@@ -356,15 +378,26 @@ function tryReadInstalledPackageInfo(
return resolvedPackageInfo;
}
+function tryResolveSelfPackageInfo(): InstalledPackageInfo | undefined {
+ try {
+ return tryReadInstalledPackageInfo(
+ packageRequire.resolve(`${EVE_PACKAGE_NAME}/package.json`),
+ EVE_PACKAGE_NAME,
+ );
+ } catch {
+ return undefined;
+ }
+}
+
/**
- * Resolves the installed eve package identity from package.json.
+ * Resolves eve's package identity from local or build-stamped metadata.
*/
export function resolveInstalledPackageInfo(): InstalledPackageInfo {
if (cachedPackageInfo) {
return cachedPackageInfo;
}
- const packageRoot = tryResolvePackageRoot();
+ const packageRoot = tryResolveLocalPackageRoot(resolveCurrentModulePath());
const packageRootInfo =
packageRoot === undefined
? undefined
@@ -375,24 +408,19 @@ export function resolveInstalledPackageInfo(): InstalledPackageInfo {
return cachedPackageInfo;
}
- try {
- const resolvedPackageJsonPath = packageRequire.resolve(`${EVE_PACKAGE_NAME}/package.json`);
- const resolvedPackageInfo = tryReadInstalledPackageInfo(
- resolvedPackageJsonPath,
- EVE_PACKAGE_NAME,
- );
+ const stampedVersion = readStampedPackageVersion();
- if (resolvedPackageInfo) {
- cachedPackageInfo = resolvedPackageInfo;
- return cachedPackageInfo;
- }
- } catch {
- // Fall back to the package's development identity when the self package
- // cannot be resolved from bundled runtime output.
+ if (stampedVersion !== undefined) {
+ cachedPackageInfo = { name: EVE_PACKAGE_NAME, version: stampedVersion };
+ return cachedPackageInfo;
}
- cachedPackageInfo = {
- ...FALLBACK_PACKAGE_INFO,
+ // Only source builds are unstamped, such as a workspace app bundled through
+ // the `eve-source` condition. Published packages are always stamped, so
+ // serverless output never reaches this module-resolution probe.
+ cachedPackageInfo = tryResolveSelfPackageInfo() ?? {
+ name: EVE_PACKAGE_NAME,
+ version: "0.0.0",
};
return cachedPackageInfo;
diff --git a/packages/eve/src/internal/application/stamped-package-version.ts b/packages/eve/src/internal/application/stamped-package-version.ts
new file mode 100644
index 000000000..3aeefcd9d
--- /dev/null
+++ b/packages/eve/src/internal/application/stamped-package-version.ts
@@ -0,0 +1,13 @@
+// The package build stamps the published version into `dist` so bundled
+// deployments can report package metadata without resolving package.json.
+const STAMPED_PACKAGE_VERSION: string = "__EVE_PACKAGE_VERSION__";
+
+/**
+ * Returns the build-stamped eve version, or undefined for an unstamped build
+ * compiled from source.
+ */
+export function readStampedPackageVersion(): string | undefined {
+ // Detect an unstamped build by the token's `__` shape. Spelling the token
+ // out in a comparison would get rewritten by the stamp itself.
+ return STAMPED_PACKAGE_VERSION.startsWith("__") ? undefined : STAMPED_PACKAGE_VERSION;
+}
diff --git a/packages/eve/test/package-identity.test.ts b/packages/eve/test/package-identity.test.ts
index cb31f9870..83c04b8c1 100644
--- a/packages/eve/test/package-identity.test.ts
+++ b/packages/eve/test/package-identity.test.ts
@@ -1,4 +1,7 @@
+import { fileURLToPath } from "node:url";
+
import { afterEach, describe, expect, it, vi } from "vitest";
+
import { resolveInstalledPackageInfo } from "#internal/application/package.js";
import { EVE_PACKAGE_NAME } from "#internal/package-name.js";
@@ -6,6 +9,7 @@ describe("package identity", () => {
afterEach(() => {
vi.doUnmock("node:fs");
vi.doUnmock("node:module");
+ vi.doUnmock("#internal/application/stamped-package-version.js");
});
it("resolves package identity from the installed package metadata", () => {
@@ -15,8 +19,11 @@ describe("package identity", () => {
expect(installedPackageInfo.version).toMatch(/\S/);
});
- it("falls back to bundled package metadata when runtime chunks have no package root", async () => {
+ it("uses the stamped version in bundled output without runtime package resolution", async () => {
vi.resetModules();
+ const resolvePackageJson = vi.fn(() => {
+ throw new Error("Unexpected package self-resolution.");
+ });
vi.doMock("node:fs", () => ({
existsSync: () => false,
readFileSync: () => {
@@ -26,17 +33,85 @@ describe("package identity", () => {
}));
vi.doMock("node:module", () => ({
createRequire: () => ({
- resolve: () => {
- throw new Error("Package self-resolution unavailable.");
- },
+ resolve: resolvePackageJson,
}),
}));
+ vi.doMock("#internal/application/stamped-package-version.js", () => ({
+ readStampedPackageVersion: () => "1.2.3",
+ }));
const { resolveInstalledPackageInfo: resolveBundledPackageInfo } =
await import("#internal/application/package.js");
const installedPackageInfo = resolveBundledPackageInfo();
expect(installedPackageInfo.name).toBe(EVE_PACKAGE_NAME);
- expect(installedPackageInfo.version).toMatch(/\S/);
+ expect(installedPackageInfo.version).toBe("1.2.3");
+ expect(resolvePackageJson).not.toHaveBeenCalled();
+ });
+
+ it("resolves the workspace package for an unstamped source bundle", async () => {
+ vi.resetModules();
+ const workspacePackageJsonPath = "/workspace/node_modules/eve/package.json";
+ vi.doMock("node:fs", () => ({
+ existsSync: () => false,
+ readFileSync: (path: string) => {
+ if (path === workspacePackageJsonPath) {
+ return JSON.stringify({ name: EVE_PACKAGE_NAME, version: "4.5.6" });
+ }
+
+ throw new Error("File not found.");
+ },
+ realpathSync: Object.assign((path: string) => path, {
+ native: (path: string) => path,
+ }),
+ }));
+ vi.doMock("node:module", () => ({
+ createRequire: () => ({
+ resolve: () => workspacePackageJsonPath,
+ }),
+ }));
+ vi.doMock("#internal/application/stamped-package-version.js", () => ({
+ readStampedPackageVersion: () => undefined,
+ }));
+
+ const { resolveInstalledPackageInfo: resolveSourcePackageInfo } =
+ await import("#internal/application/package.js");
+
+ expect(resolveSourcePackageInfo()).toEqual({
+ name: EVE_PACKAGE_NAME,
+ version: "4.5.6",
+ });
+ });
+
+ it("does not use metadata from a surrounding package that does not own the module", async () => {
+ vi.resetModules();
+ const packageJsonPath = fileURLToPath(new URL("../package.json", import.meta.url));
+ const realpathSync = Object.assign((path: string) => path, {
+ native: (path: string) => path,
+ });
+ const readPackageJson = vi.fn((path: string) => {
+ if (path === packageJsonPath) {
+ return JSON.stringify({ name: EVE_PACKAGE_NAME, version: "9.9.9" });
+ }
+
+ throw new Error("File not found.");
+ });
+ vi.doMock("node:fs", () => ({
+ existsSync: () => false,
+ readFileSync: readPackageJson,
+ realpathSync,
+ }));
+ vi.doMock("#internal/application/stamped-package-version.js", () => ({
+ readStampedPackageVersion: () => "1.2.3",
+ }));
+
+ const { resolveInstalledPackageInfo: resolveBundledPackageInfo } =
+ await import("#internal/application/package.js");
+
+ expect(resolveBundledPackageInfo()).toEqual({
+ name: EVE_PACKAGE_NAME,
+ version: "1.2.3",
+ });
+ expect(readPackageJson).not.toHaveBeenCalled();
});
});