Skip to content
Closed
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 @@ -2,6 +2,8 @@

## Unreleased

- Keep ESM namespaces free of inherited `__esModule` markers and preserve the own marker and live exports for `require(esm)`, fixing Vite/tsx namespace interop. Adapts [oven-sh/bun#33894](https://github.com/oven-sh/bun/pull/33894) and [oven-sh/WebKit#279](https://github.com/oven-sh/WebKit/pull/279). Thanks @robobun!

- Align WebKit and VM source-position assertions with Node's constructor locations and select these regressions for native stack-formatting changes in both fork CI lanes.

- Preserve the native-context count in the fast `v8.getHeapStatistics()` adapter and avoid appending a second strict code-generation flag to inherited worker arguments.
Expand Down
2 changes: 2 additions & 0 deletions docs/runtime/module-resolution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ import Stuff from "./my-commonjs.cjs";
const myStuff = require("./my-commonjs.cjs");
```

When `require()` loads an ES module with a default export and no explicit `__esModule` export, it returns a namespace with an own, enumerable `__esModule: true` property. Its exports remain live. Importing the same module returns the original namespace without that added marker. Both namespace objects have a null prototype and reject property assignments.

### Top level await

The only exception to this rule is top-level await. You can't `require()` a file that uses top-level await, since the `require()` function is inherently synchronous.
Expand Down
2 changes: 2 additions & 0 deletions docs/runtime/nodejs-compat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Stdin and eval scripts report malformed ancestor package metadata only when an i

The OpenClaw fork reports missing runtime imports without downloading npm packages. This default covers scripts, workers, and child processes running the fork. You can opt in with `--install=auto|fallback|force` or an explicit bunfig `install.auto` setting. Package-manager commands (`bun install`, `bun add`, `bun x`, and `bunx`) are unchanged. See [Auto-install](/runtime/auto-install).

Module namespaces have a null prototype. `require(esm)` adds an own enumerable `__esModule` marker only for modules with a default export and no explicit marker, without modifying the imported namespace. CommonJS namespaces preserve explicit markers and expose the original exports as `module.exports`.

## Built-in Node.js modules

`Error.prepareStackTrace` supports deletion, descriptor restoration, and accessor replacement. Nested stack formatting and throwing formatters leave later captures usable.
Expand Down
36 changes: 3 additions & 33 deletions src/js/builtins/CommonJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,11 @@ export function overridableRequire(this: JSCommonJSModule, originalId: string, o

const namespace = out;
// In a require cycle the namespace is live while the module body is still
// running, so an export named `__esModule` / `module.exports` may be in TDZ.
let esModule, moduleExports;
// running, so an export named `module.exports` may be in TDZ.
let moduleExports;
try {
esModule = namespace.__esModule;
moduleExports = namespace["module.exports"];
} catch {}
// In Bun, when __esModule is not defined, it's a CustomAccessor on the prototype.
// Various libraries expect __esModule to be set when using ESM from require().
// We don't want to always inject the __esModule export into every module,
// And creating an Object wrapper causes the actual exports to not be own properties.
// So instead of either of those, we make it so that the __esModule property can be set at runtime.
// It only supports "true" and undefined. Anything non-truthy is treated as undefined.
// https://github.com/oven-sh/bun/issues/14411
if (esModule === undefined) {
try {
namespace.__esModule = true;
} catch {
// https://github.com/oven-sh/bun/issues/17816
}
}

return (mod.exports = moduleExports ?? namespace);
}
Expand Down Expand Up @@ -217,25 +202,10 @@ export function requireESMFromHijackedExtension(this: JSCommonJSModule, id: stri
const namespace = $requireESM(id, this);

// See `overridableRequire`: TDZ-safe reads for the require-cycle case.
let esModule, moduleExports;
let moduleExports;
try {
esModule = namespace.__esModule;
moduleExports = namespace["module.exports"];
} catch {}
// In Bun, when __esModule is not defined, it's a CustomAccessor on the prototype.
// Various libraries expect __esModule to be set when using ESM from require().
// We don't want to always inject the __esModule export into every module,
// And creating an Object wrapper causes the actual exports to not be own properties.
// So instead of either of those, we make it so that the __esModule property can be set at runtime.
// It only supports "true" and undefined. Anything non-truthy is treated as undefined.
// https://github.com/oven-sh/bun/issues/14411
if (esModule === undefined) {
try {
namespace.__esModule = true;
} catch {
// https://github.com/oven-sh/bun/issues/17816
}
}

this.exports = moduleExports ?? namespace;
}
Expand Down
27 changes: 23 additions & 4 deletions src/jsc/bindings/JSCommonJSModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,8 @@ void populateESMExports(

if (auto* exports = result.getObject()) {
bool hasESModuleMarker = false;
if (!ignoreESModuleAnnotation) {
// A native namespace forwarded by CJS keeps its default binding inside that namespace.
if (!ignoreESModuleAnnotation && !dynamicDowncast<JSModuleNamespaceObject>(exports)) {
PropertySlot slot(exports, PropertySlot::InternalMethodType::VMInquiry, &vm);
auto has = exports->getPropertySlot(globalObject, esModuleMarker, slot);
RETURN_IF_EXCEPTION(scope, );
Expand All @@ -1213,7 +1214,7 @@ void populateESMExports(
if (canPerformFastEnumeration(structure)) {
exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
auto key = entry.key();
if (key->isSymbol() || key == esModuleMarker)
if (key->isSymbol())
return true;

needsToAssignDefault = needsToAssignDefault && key != vm.propertyNames->defaultKeyword;
Expand All @@ -1230,7 +1231,7 @@ void populateESMExports(
RETURN_IF_EXCEPTION(scope, );

for (auto property : properties) {
if (property.isEmpty() || property.isNull() || property == esModuleMarker || property.isPrivateName() || property.isSymbol()) [[unlikely]]
if (property.isEmpty() || property.isNull() || property.isPrivateName() || property.isSymbol()) [[unlikely]]
continue;

// ignore constructor
Expand Down Expand Up @@ -1336,7 +1337,25 @@ void JSCommonJSModule::toSyntheticSource(JSC::JSGlobalObject* globalObject,
auto result = this->exportsObject();
RETURN_IF_EXCEPTION(scope, );

RELEASE_AND_RETURN(scope, populateESMExports(globalObject, result, exportNames, exportValues, this->ignoreESModuleAnnotation));
populateESMExports(globalObject, result, exportNames, exportValues, this->ignoreESModuleAnnotation);
RETURN_IF_EXCEPTION(scope, );

auto& vm = globalObject->vm();
const auto moduleExports = Identifier::fromString(vm, "module.exports"_s);
exportValues.append(result);
if (exportValues.hasOverflowed()) {
throwOutOfMemoryError(globalObject, scope);
return;
}
// Node's CJS namespace always exposes the original exports through this name.
for (size_t i = 0; i < exportNames.size(); ++i) {
if (exportNames[i] == moduleExports) {
exportValues[i] = exportValues.last();
exportValues.removeLast();
return;
}
}
exportNames.append(moduleExports);
}

void JSCommonJSModule::setExportsObject(JSC::JSValue exportsObject)
Expand Down
5 changes: 0 additions & 5 deletions src/jsc/bindings/NodeVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,8 @@ JSModuleNamespaceObject* NodeVMModule::namespaceObject(JSC::JSGlobalObject* glob
object = amr->getModuleNamespace(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (object) {
// The shared module namespace structure carries an __esModule accessor for
// CJS interop, but a Module Namespace Exotic Object is specified to have a
// null [[Prototype]], which is what vm modules must expose.
object->setPrototypeDirect(vm, jsNull());
namespaceObject(vm, object);
}
RETURN_IF_EXCEPTION(scope, {});

return object;
}
Expand Down
50 changes: 12 additions & 38 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,11 @@ JSC_DEFINE_HOST_FUNCTION(functionEsmNamespaceForCjs, (JSC::JSGlobalObject * glob
RETURN_IF_EXCEPTION(scope, {});
if (!record)
return JSValue::encode(jsUndefined());
auto* ns = record->getModuleNamespace(globalObject, false);
auto* ns = record->getModuleNamespace(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(ns);
auto* required = ns->createNamespaceForRequire(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(required);
}

JSC_DEFINE_HOST_FUNCTION(functionEsmRegistryHasEvaluated, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
Expand Down Expand Up @@ -883,9 +885,11 @@ JSC_DEFINE_HOST_FUNCTION(functionEsmLoadSync, (JSC::JSGlobalObject * lexicalGlob
if (auto* entry = loader->registryEntry(key)) {
entryExistedBefore = true;
if (isModuleEvaluated(entry->record()) || isModuleEvaluatingSync(entry->record())) {
auto* ns = entry->record()->getModuleNamespace(globalObject, false);
auto* ns = entry->record()->getModuleNamespace(globalObject);
RETURN_IF_EXCEPTION(scope, {});
auto* required = ns->createNamespaceForRequire(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(ns);
return JSValue::encode(required);
}
// Any other Evaluating record (one with top-level await) must not
// reach loadModuleSync: Link() and Evaluate() reject that status.
Expand Down Expand Up @@ -958,9 +962,11 @@ JSC_DEFINE_HOST_FUNCTION(functionEsmLoadSync, (JSC::JSGlobalObject * lexicalGlob
}
}

auto* ns = record->getModuleNamespace(globalObject, false);
auto* ns = record->getModuleNamespace(globalObject);
RETURN_IF_EXCEPTION(scope, {});
auto* required = ns->createNamespaceForRequire(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(ns);
return JSValue::encode(required);
}

#define WEBCORE_GENERATED_CONSTRUCTOR_GETTER(ConstructorName) \
Expand Down Expand Up @@ -1990,31 +1996,6 @@ extern "C" napi_env ZigGlobalObject__makeNapiEnvForFFI(Zig::GlobalObject* global
}

extern "C" JSC::EncodedJSValue CryptoObject__create(JSGlobalObject*);
JSC_DEFINE_CUSTOM_GETTER(moduleNamespacePrototypeGetESModuleMarker, (JSGlobalObject * globalObject, JSC::EncodedJSValue encodedThisValue, PropertyName))
{
JSValue thisValue = JSValue::decode(encodedThisValue);
JSModuleNamespaceObject* moduleNamespaceObject = dynamicDowncast<JSModuleNamespaceObject>(thisValue);
if (!moduleNamespaceObject || moduleNamespaceObject->m_hasESModuleMarker != WTF::TriState::True) {
return JSC::JSValue::encode(jsUndefined());
}

return JSC::JSValue::encode(jsBoolean(true));
}

JSC_DEFINE_CUSTOM_SETTER(moduleNamespacePrototypeSetESModuleMarker, (JSGlobalObject * globalObject, JSC::EncodedJSValue encodedThisValue, JSC::EncodedJSValue encodedValue, PropertyName))
{
auto& vm = JSC::getVM(globalObject);
JSValue thisValue = JSValue::decode(encodedThisValue);
JSModuleNamespaceObject* moduleNamespaceObject = dynamicDowncast<JSModuleNamespaceObject>(thisValue);
if (!moduleNamespaceObject) {
return false;
}
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = JSValue::decode(encodedValue);
WTF::TriState triState = value.toBoolean(globalObject) ? WTF::TriState::True : WTF::TriState::False;
moduleNamespaceObject->m_hasESModuleMarker = triState;
return true;
}

namespace {

Expand Down Expand Up @@ -2347,11 +2328,6 @@ void GlobalObject::finishCreation(VM& vm)
createMemoryFootprintStructure(
init.vm, static_cast<Zig::GlobalObject*>(init.owner)));
} },
{ OBJECT_OFFSETOF(GlobalObject, m_moduleNamespaceObjectStructure), [](const LazyProperty<JSGlobalObject, Structure>::Initializer& init) {
JSObject* moduleNamespacePrototype = JSC::constructEmptyObject(init.vm, init.owner->nullPrototypeObjectStructure());
moduleNamespacePrototype->putDirectCustomAccessor(init.vm, init.vm.propertyNames->__esModule, CustomGetterSetter::create(init.vm, moduleNamespacePrototypeGetESModuleMarker, moduleNamespacePrototypeSetESModuleMarker), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::CustomAccessor | 0);
init.set(JSModuleNamespaceObject::createStructure(init.vm, init.owner, moduleNamespacePrototype));
} },
{ OBJECT_OFFSETOF(GlobalObject, m_JSBufferSubclassStructure), [](const LazyProperty<JSGlobalObject, Structure>::Initializer& init) {
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(init.vm);
auto* globalObject = static_cast<Zig::GlobalObject*>(init.owner);
Expand Down Expand Up @@ -2684,8 +2660,6 @@ void GlobalObject::finishCreation(VM& vm)
dynamicDowncast<Zig::GlobalObject>(init.owner)));
});

// Change prototype from null to object for synthetic modules.

m_vmModuleContextMap.initLater(
[](const Initializer<JSWeakMap>& init) {
init.set(JSWeakMap::create(init.vm, init.owner->weakMapStructure()));
Expand Down
10 changes: 5 additions & 5 deletions test/js/bun/import-attributes/import-attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ describe("other loaders do not crash", () => {
});

describe("?raw", () => {
for (const [name, fn] of [
["bun run", testBunRun],
for (const [name, fn, expected] of [
["bun run", testBunRun, {}],
// ["bun build", testBunBuild], // TODO: bun.build doesn't support query params at all yet
["bun run await import", testBunRunAwaitImport],
["require", testBunRunRequire],
["bun run await import", testBunRunAwaitImport, {}],
["require", testBunRunRequire, { __esModule: true }],
// ["bun build require", testBunBuildRequire], // TODO: bun.build doesn't support query params at all yet
] as const) {
test(name, async () => {
Expand All @@ -357,7 +357,7 @@ describe("?raw", () => {
await using question_raw = tempDir("import-attributes", {
[filename]: code,
});
expect(await fn(question_raw, null, filename + "?raw")).toEqual({ default: code });
expect(await fn(question_raw, null, filename + "?raw")).toEqual({ ...expected, default: code });
});
}
});
9 changes: 6 additions & 3 deletions test/js/bun/namespace-prototype-pollution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ test("namespace imports should not inherit from Object.prototype", async () => {
console.log("PASS: prototype pollution prevented");
}

// Verify __esModule still works
console.log("__esModule settable:", (mod.__esModule = true, mod.__esModule === true));
console.log("Null prototype:", Object.getPrototypeOf(mod) === null);
console.log("__esModule absent:", !("__esModule" in mod));
console.log("__esModule immutable:", !Reflect.set(mod, "__esModule", true));

// Original exports should work
console.log("Original export:", mod.value);
Expand All @@ -38,6 +39,8 @@ test("namespace imports should not inherit from Object.prototype", async () => {

expect(exitCode).toBe(0);
expect(stdout).toContain("PASS: prototype pollution prevented");
expect(stdout).toContain("__esModule settable: true");
expect(stdout).toContain("Null prototype: true");
expect(stdout).toContain("__esModule absent: true");
expect(stdout).toContain("__esModule immutable: true");
expect(stdout).toContain("Original export: original");
});
10 changes: 5 additions & 5 deletions test/js/bun/resolve/esModule-annotation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ describe('without type: "module"', () => {
__esModule: true,
});

// The module namespace object will not have the __esModule property.
expect(WithoutTypeModuleExportEsModuleAnnotationNoDefault.__esModule).toBeUndefined();
expect(Object.hasOwn(WithoutTypeModuleExportEsModuleAnnotationNoDefault, "__esModule")).toBe(true);
expect(WithoutTypeModuleExportEsModuleAnnotationNoDefault.__esModule).toBeTrue();
});

test("exports.default = true; exports.__esModule = true;", () => {
expect(WithoutTypeModuleExportEsModuleAnnotation.default).toBeTrue();
expect(WithoutTypeModuleExportEsModuleAnnotation.__esModule).toBeUndefined();
expect(WithoutTypeModuleExportEsModuleAnnotation.__esModule).toBeTrue();
});

test("exports.default = true;", () => {
expect(WithoutTypeModuleExportEsModuleNoAnnotation.default).toEqual({
default: true,
});
expect(WithoutTypeModuleExportEsModuleAnnotation.__esModule).toBeUndefined();
expect(WithoutTypeModuleExportEsModuleNoAnnotation.__esModule).toBeUndefined();
});
});

Expand Down Expand Up @@ -65,7 +65,7 @@ describe('with type: "module"', () => {
expect(WithTypeModuleExportEsModuleNoAnnotation.default).toEqual({
default: true,
});
expect(WithTypeModuleExportEsModuleAnnotation.__esModule).toBeTrue();
expect(WithTypeModuleExportEsModuleNoAnnotation.__esModule).toBeUndefined();
});
});

Expand Down
Loading
Loading