Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): avoid ESM path global initialization TDZ (#1327)

Colton Paddenc1bf6d42b8ee
diff --git a/.changeset/fix-node-esm-compat-banner-tdz.md b/.changeset/fix-node-esm-compat-banner-tdz.md
new file mode 100644
index 000000000..0ba647543
--- /dev/null
+++ b/.changeset/fix-node-esm-compat-banner-tdz.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Fix `eve dev` startup for bundled extensions whose output declares `__filename` without `__dirname`.
diff --git a/packages/eve/src/internal/node-esm-compat-banner.test.ts b/packages/eve/src/internal/node-esm-compat-banner.test.ts
index fc457a89b..9b7a4b71d 100644
--- a/packages/eve/src/internal/node-esm-compat-banner.test.ts
+++ b/packages/eve/src/internal/node-esm-compat-banner.test.ts
@@ -52,6 +52,17 @@ describe("buildNodeEsmCompatBanner", () => {
expect(banner).not.toContain('from "node:path"');
});
+ it("does not read a chunk-provided __filename before it initializes", () => {
+ const chunk = ["const __filename = '/x/file.js';", 'export const value = "noop";'].join("\n");
+
+ const banner = buildNodeEsmCompatBanner(chunk);
+
+ expect(banner).not.toContain("const __filename");
+ expect(banner).toContain(
+ "const __dirname = __eveDirname(__eveFileURLToPath(import.meta.url));",
+ );
+ });
+
it("omits the require shim when the chunk binds require", () => {
const chunk = [
'import { createRequire } from "node:module";',
diff --git a/packages/eve/src/internal/node-esm-compat-banner.ts b/packages/eve/src/internal/node-esm-compat-banner.ts
index 5705e2f83..82f73126f 100644
--- a/packages/eve/src/internal/node-esm-compat-banner.ts
+++ b/packages/eve/src/internal/node-esm-compat-banner.ts
@@ -36,6 +36,9 @@ const BANNER_LINES: readonly BannerLine[] = [
},
];
+const FILENAME_BANNER_LINE = BANNER_LINES[0]!;
+const DIRNAME_BANNER_LINE = BANNER_LINES[1]!;
+
const REQUIRE_LINE: BannerLine = {
importLine: 'import { createRequire as __eveCreateRequire } from "node:module";',
declarationLine: "const require = __eveCreateRequire(import.meta.url);",
@@ -62,6 +65,8 @@ export function buildNodeEsmCompatBanner(
const imports: string[] = [];
const declarations: string[] = [];
+ const chunkProvidesFilename = FILENAME_BANNER_LINE.bindingPattern.test(code);
+ const chunkProvidesDirname = DIRNAME_BANNER_LINE.bindingPattern.test(code);
for (const line of lines) {
if (line.bindingPattern.test(code)) {
@@ -69,7 +74,16 @@ export function buildNodeEsmCompatBanner(
}
imports.push(line.importLine);
- declarations.push(line.declarationLine);
+ declarations.push(
+ line === DIRNAME_BANNER_LINE && chunkProvidesFilename
+ ? "const __dirname = __eveDirname(__eveFileURLToPath(import.meta.url));"
+ : line.declarationLine,
+ );
+ }
+
+ if (chunkProvidesFilename && !chunkProvidesDirname) {
+ // The chunk's binding is initialized after this prepended banner.
+ imports.unshift(FILENAME_BANNER_LINE.importLine);
}
if (declarations.length === 0) {