Skip to content

fix(runtime): require cache should not include unevaluated ESM modules. #5233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023
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
8 changes: 3 additions & 5 deletions src/js/builtins/ImportMetaObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,11 @@ export function createRequireCache() {

ownKeys(target) {
var array = [...$requireMap.$keys()];
const registryKeys = [...Loader.registry.$keys()];
for (const key of registryKeys) {
if (!array.includes(key)) {
for (const key of Loader.registry.$keys()) {
if (!array.includes(key) && Loader.registry.$get(key)?.evaluated) {
$arrayPush(array, key);
}
}

return array;
},

Expand All @@ -193,7 +191,7 @@ export function createRequireCache() {
},

getOwnPropertyDescriptor(target, key: string) {
if ($requireMap.$has(key) || Loader.registry.$has(key)) {
if ($requireMap.$has(key) || Loader.registry.$get(key)?.evaluated) {
return {
configurable: true,
enumerable: true,
Expand Down
4 changes: 2 additions & 2 deletions src/js/out/WebCoreJSBuiltins.cpp

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

Binary file modified test/bun.lockb
Binary file not shown.
21 changes: 21 additions & 0 deletions test/cli/run/require-cache-bug-5188.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { strictEqual, ok } = require("assert");

Loader.registry.set("bad", { evaluated: false });
strictEqual(require.cache.bad, undefined);
ok(!Object.hasOwn(require.cache, "bad"));
ok(!Object.getOwnPropertyNames(require.cache).includes("bad"));

// hard to simplify this test case, but importing this would cause require.cache.extract to be set
require("msgpackr-extract");

strictEqual(require.cache["extract"], undefined);
ok(!Object.hasOwnProperty.call(require.cache, "extract"));
ok(!Object.getOwnPropertyNames(require.cache).includes("extract"));

for (const key of Object.keys(require.cache)) {
if (!require.cache[key]) {
throw new Error("require.cache has an undefined value that was in it's keys");
}
}

console.log("--pass--");
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ test("require.cache", () => {
expect(stdout.toString().trim().endsWith("--pass--")).toBe(true);
expect(exitCode).toBe(0);
});

// https://github.com/oven-sh/bun/issues/5188
test("require.cache does not include unevaluated modules", () => {
const { stdout, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "run", join(import.meta.dir, "require-cache-bug-5188.js")],
env: bunEnv,
stderr: "inherit",
});

expect(stdout.toString().trim().endsWith("--pass--")).toBe(true);
expect(exitCode).toBe(0);
});
3 changes: 2 additions & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@prisma/client": "5.1.1",
"@resvg/resvg-js": "2.4.1",
"@swc/core": "1.3.38",
"body-parser": "1.20.2",
"comlink": "4.4.1",
Expand All @@ -20,12 +21,12 @@
"iconv-lite": "0.6.3",
"jest-extended": "4.0.0",
"lodash": "4.17.21",
"msgpackr-extract": "3.0.2",
"nodemailer": "6.9.3",
"pg": "8.11.1",
"pg-connection-string": "2.6.1",
"postgres": "3.3.5",
"prisma": "5.1.1",
"@resvg/resvg-js": "2.4.1",
"socket.io": "4.7.1",
"socket.io-client": "4.7.1",
"supertest": "6.3.3",
Expand Down