Skip to content
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

lib: do not throw if global property is no longer configurable #45344

Merged
merged 2 commits into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/internal/modules/cjs/helpers.js
Expand Up @@ -176,16 +176,19 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
get: () => {
const lib = dummyModule.require(name);

// Disable the current getter/setter and set up a new
// non-enumerable property.
delete object[name];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this is line of code not needed anymore.

ObjectDefineProperty(object, name, {
__proto__: null,
get: () => lib,
set: setReal,
configurable: true,
enumerable: false
});
try {
// Override the current getter/setter and set up a new
// non-enumerable property.
ObjectDefineProperty(object, name, {
__proto__: null,
get: () => lib,
set: setReal,
configurable: true,
enumerable: false,
});
} catch {
// If the property is no longer configurable, ignore the error.
}

return lib;
},
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-cli-eval.js
Expand Up @@ -354,3 +354,12 @@ child.exec(
common.mustSucceed((stdout) => {
assert.match(stdout, /^number/);
}));

// Regression test for https://github.com/nodejs/node/issues/45336
child.exec(`${nodejs} -p '` +
'Object.defineProperty(global, "fs", { configurable: false });' +
'fs === require("node:fs")' +
'\'',
common.mustSucceed((stdout) => {
assert.match(stdout, /^true/);
}));