Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@
- Make newly assigned Windows native environment variables enumerable so environment copies and children retain them. Adapts [oven-sh/bun#35254](https://github.com/oven-sh/bun/pull/35254). Thanks @robobun!

- Allow later Windows pipe stdout/stderr writes after `end()` and pipeline completion, matching Node while preserving POSIX socket shutdown.

- Preserve already-fetched CommonJS-to-ESM module keys through linking so Windows forward-slash aliases and paths containing dot segments do not crash when reading exports.
2 changes: 2 additions & 0 deletions docs/runtime/nodejs-compat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Module loading has four additional limits:
- Resolve hooks can run more than once for the same edge in a static cycle. For example, `entry → middle → leaf → middle` returns the correct exports but can resolve `middle → leaf` twice; Node resolves it once. This affects both dynamic import of the entry and `require(entry)`. JavaScriptCore's `loadRequestedModules` starts separate graph traversals before `finishLoadingImportedModule` fills the referring record's completed-edge cache. Resolve hooks should tolerate these repeated calls.
- Bun uses one module registry for `import()` and `require()` of the same URL. After `import()` finishes loading a URL, a later `require()` reuses the completed record without running load hooks again. A hook cannot return different source for `require()` of a URL that `import()` already loaded. Node can run a second load hook and return different source for the CommonJS request. This completed-record reuse does not trigger `ERR_MODULE_HOOK_REENTRANCY`; that error protects in-flight requests.

CommonJS-to-ESM loads preserve already-fetched absolute module keys, including Windows forward-slash aliases and paths containing dot segments, through linking and export access.

CommonJS named exports include nonenumerable data properties even when an `__esModule` marker and accessors are present. Nonenumerable accessors remain excluded.

Package imports and exports match the `module-sync` condition for both `import` and `require`. Conditions retain package.json key order. Bun and Node build targets include this condition; the browser target does not.
Expand Down
20 changes: 19 additions & 1 deletion src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3536,6 +3536,17 @@ JSC::Identifier GlobalObject::moduleLoaderResolve(JSGlobalObject* jsGlobalObject
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);

String preparedKey;
if (key.isString() && (!referrer || referrer.isUndefinedOrNull() || (referrer.isString() && !asString(referrer)->length()))) {
auto name = asString(key)->value(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (isAbsolutePath(name)) {
auto moduleKey = Identifier::fromString(vm, name);
if (auto* entry = loader->registryEntry(moduleKey); entry && entry->record())
preparedKey = name;
}
}

if (Bun__hasModuleHooks(globalObject->bunVM())) {
auto name = key.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, {});
Expand Down Expand Up @@ -3656,7 +3667,14 @@ JSC::Identifier GlobalObject::moduleLoaderResolve(JSGlobalObject* jsGlobalObject
}
auto resolved = res.result.value.transferToWTFString();
auto suffix = requestedSuffix.isEmpty() ? queryZ.transferToWTFString() : requestedSuffix;
return Identifier::fromString(vm, resolvedModuleKey(resolved, suffix));
auto resolvedKey = resolvedModuleKey(resolved, suffix);
// The loader revisits require(esm)'s prepared key as a top-level request.
// Keep that record when only path spelling changed, or its environment is
// never linked. Actual plugin redirects must still select their new target.
if (!preparedKey.isEmpty() && isAbsolutePath(resolvedKey)
&& URL::fileURLWithFileSystemPath(preparedKey) == URL::fileURLWithFileSystemPath(resolvedKey))
return Identifier::fromString(vm, preparedKey);
return Identifier::fromString(vm, resolvedKey);
}

JSC::Identifier StandaloneGlobalObject::moduleLoaderResolve(JSGlobalObject* globalObject, JSModuleLoader* loader, JSValue key, JSValue referrer, RefPtr<JSC::ScriptFetcher> fetcher, bool b)
Expand Down
43 changes: 43 additions & 0 deletions test/js/bun/plugin/plugin-resolved-key-fixture.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions test/js/bun/plugin/plugin-resolved-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, isWindows, tempDir } from "harness";
import path from "node:path";

for (const mode of ["plugin", "override"]) {
for (const spelling of isWindows ? ["dot", "forward"] : ["dot"]) {
test(`${mode} alias preserves the prepared ESM key with ${spelling} separators`, async () => {
using dir = tempDir("resolved-module-key", {});
await using child = Bun.spawn({
cmd: [bunExe(), path.join(import.meta.dir, "plugin-resolved-key-fixture.cjs"), mode, spelling, String(dir)],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([child.stdout.text(), child.stderr.text(), child.exited]);
expect({ stdout, stderr, exitCode }).toEqual({ stdout: "ok\n", stderr: "", exitCode: 0 });
});
}
}
Loading