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

module: do not set CJS variables for Worker eval #53050

Merged
merged 5 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 1 addition & 1 deletion lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {

if (getOptionValue('--experimental-detect-module') &&
getOptionValue('--input-type') === '' && getOptionValue('--experimental-default-type') === '' &&
containsModuleSyntax(body, name)) {
containsModuleSyntax(body, name, null, 'no CJS variables')) {
return evalModuleEntryPoint(body, print);
}

Expand Down
18 changes: 13 additions & 5 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
Local<Context> context,
Local<String> code,
Local<String> filename,
bool* cache_rejected) {
bool* cache_rejected,
bool is_cjs_scope) {
Isolate* isolate = context->GetIsolate();
EscapableHandleScope scope(isolate);

Expand Down Expand Up @@ -1498,7 +1499,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
options = ScriptCompiler::kConsumeCodeCache;
}

std::vector<Local<String>> params = GetCJSParameters(env->isolate_data());
std::vector<Local<String>> params;
if (is_cjs_scope) {
params = GetCJSParameters(env->isolate_data());
}
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
context,
&source,
Expand Down Expand Up @@ -1560,7 +1564,7 @@ static void CompileFunctionForCJSLoader(
ShouldNotAbortOnUncaughtScope no_abort_scope(realm->env());
TryCatchScope try_catch(env);
if (!CompileFunctionForCJSLoader(
env, context, code, filename, &cache_rejected)
env, context, code, filename, &cache_rejected, true)
.ToLocal(&fn)) {
CHECK(try_catch.HasCaught());
CHECK(!try_catch.HasTerminated());
Expand Down Expand Up @@ -1698,11 +1702,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsString());
Local<String> filename = args[1].As<String>();

// Argument 2: resource name (URL for ES module).
// Argument 3: resource name (URL for ES module).
Local<String> resource_name = filename;
if (args[2]->IsString()) {
resource_name = args[2].As<String>();
}
// Argument 4: flag to indicate if CJS variables should not be in scope
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
// (they should be for normal CommonJS modules, but not for the
// CommonJS eval scope).
bool cjs_var = !args[3]->IsString();

bool cache_rejected = false;
Local<String> message;
Expand All @@ -1711,7 +1719,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
TryCatchScope try_catch(env);
ShouldNotAbortOnUncaughtScope no_abort_scope(env);
if (CompileFunctionForCJSLoader(
env, context, code, filename, &cache_rejected)
env, context, code, filename, &cache_rejected, cjs_var)
.ToLocal(&fn)) {
args.GetReturnValue().Set(false);
return;
Expand Down
28 changes: 28 additions & 0 deletions test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@
strictEqual(signal, null);
});

it('should not switch to module if code is parsable as script', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
'--print',
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
'let __filename,__dirname,require,module,exports;this.a',
]);

strictEqual(stderr, '');
strictEqual(stdout, '');

Check failure on line 55 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-asan

--- stdout --- ▶ --experimental-detect-module ▶ string input ✔ permits ESM syntax in --eval input without requiring --input-type=module (171.877837ms) ::debug::starting to run --experimental-detect-module ::debug::starting to run string input ::debug::starting to run permits ESM syntax in --eval input without requiring --input-type=module ::debug::completed running permits ESM syntax in --eval input without requiring --input-type=module ✔ permits ESM syntax in STDIN input without requiring --input-type=module (204.083217ms) ::debug::starting to run permits ESM syntax in STDIN input without requiring --input-type=module ::debug::completed running permits ESM syntax in STDIN input without requiring --input-type=module ✔ should be overridden by --input-type (166.592274ms) ::debug::starting to run should be overridden by --input-type ::debug::completed running should be overridden by --input-type ✖ should not switch to module if code is parsable as script (124.824144ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///home/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } ::debug::starting to run should not switch to module if code is parsable as script ::error title=should not switch to module if code is parsable as script,file=test/es-module/test-esm-detect-ambiguous.mjs,line=55,col=7::[Error [ERR_TEST_FAILURE]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - ''] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///home/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } } ✔ should be overridden by --experimental-default-type (104.472328ms) ::debug::starting to run should be overridden by --experimental-default-type ::debug::completed running should be overridden by --experimental-default-type ✔ does not trigger detection via source code `eval()` (160.345671ms) ▶ string input (943.565535ms) ::debug::starting to run does not trigger detection via source code `eval()` ::debug::completed running does not trigger detection via source code `eval()` ▶ .js file input in a typeless package ✔ permits CommonJS syntax in a .js entry point (159.954966ms) ::debug::starting to run .js file input in a typeless package ::debug::starting to run permits CommonJS syntax in a .js entry point ::debug::completed running permits CommonJS syntax in a .js entry point ✔ permits ESM syntax in a .js entry point (161.676619ms) ::debug::starting to run permits ESM syntax in a .js entry point ::debug::completed running permits ESM syntax in a .js entry point ✔ permits CommonJS syntax in a .js file imported by a CommonJS entry point (260.828094ms) ::debug::starting to run permits CommonJS syntax in a .js file imported by a CommonJS entry point ::debug::completed running permits CommonJS syntax in a .js file imported by a CommonJS entry point ✔ permits ESM syntax in a .js file imported by a CommonJS entry point (167.875543ms) ::debug::starting to run permits ESM syntax in a

Check failure on line 55 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-linux

--- stdout --- ▶ --experimental-detect-module ▶ string input ✔ permits ESM syntax in --eval input without requiring --input-type=module (40.977372ms) ::debug::starting to run --experimental-detect-module ::debug::starting to run string input ::debug::starting to run permits ESM syntax in --eval input without requiring --input-type=module ::debug::completed running permits ESM syntax in --eval input without requiring --input-type=module ✔ permits ESM syntax in STDIN input without requiring --input-type=module (38.316382ms) ::debug::starting to run permits ESM syntax in STDIN input without requiring --input-type=module ::debug::completed running permits ESM syntax in STDIN input without requiring --input-type=module ✔ should be overridden by --input-type (45.986776ms) ::debug::starting to run should be overridden by --input-type ::debug::completed running should be overridden by --input-type ✖ should not switch to module if code is parsable as script (34.199277ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///home/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } ::debug::starting to run should not switch to module if code is parsable as script ::error title=should not switch to module if code is parsable as script,file=test/es-module/test-esm-detect-ambiguous.mjs,line=55,col=7::[Error [ERR_TEST_FAILURE]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - ''] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///home/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } } ✔ should be overridden by --experimental-default-type (41.825451ms) ::debug::starting to run should be overridden by --experimental-default-type ::debug::completed running should be overridden by --experimental-default-type ✔ does not trigger detection via source code `eval()` (40.288613ms) ▶ string input (246.494841ms) ::debug::starting to run does not trigger detection via source code `eval()` ::debug::completed running does not trigger detection via source code `eval()` ▶ .js file input in a typeless package ✔ permits CommonJS syntax in a .js entry point (43.511166ms) ::debug::starting to run .js file input in a typeless package ::debug::starting to run permits CommonJS syntax in a .js entry point ::debug::completed running permits CommonJS syntax in a .js entry point ✔ permits ESM syntax in a .js entry point (51.41038ms) ::debug::starting to run permits ESM syntax in a .js entry point ::debug::completed running permits ESM syntax in a .js entry point ✔ permits CommonJS syntax in a .js file imported by a CommonJS entry point (55.827128ms) ::debug::starting to run permits CommonJS syntax in a .js file imported by a CommonJS entry point ::debug::completed running permits CommonJS syntax in a .js file imported by a CommonJS entry point ✔ permits ESM syntax in a .js file imported by a CommonJS entry point (54.005702ms) ::debug::starting to run permits ESM syntax in a .js file i

Check failure on line 55 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-macOS

--- stdout --- ▶ --experimental-detect-module ▶ string input ✔ permits ESM syntax in --eval input without requiring --input-type=module (34.276833ms) ::debug::starting to run --experimental-detect-module ::debug::starting to run string input ::debug::starting to run permits ESM syntax in --eval input without requiring --input-type=module ::debug::completed running permits ESM syntax in --eval input without requiring --input-type=module ✔ permits ESM syntax in STDIN input without requiring --input-type=module (33.141042ms) ::debug::starting to run permits ESM syntax in STDIN input without requiring --input-type=module ::debug::completed running permits ESM syntax in STDIN input without requiring --input-type=module ✔ should be overridden by --input-type (31.204209ms) ::debug::starting to run should be overridden by --input-type ::debug::completed running should be overridden by --input-type ✖ should not switch to module if code is parsable as script (33.059541ms) AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///Users/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } ::debug::starting to run should not switch to module if code is parsable as script ::error title=should not switch to module if code is parsable as script,file=test/es-module/test-esm-detect-ambiguous.mjs,line=55,col=7::[Error [ERR_TEST_FAILURE]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - ''] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'undefined\n' - '' at TestContext.<anonymous> (file:///Users/runner/work/node/node/test/es-module/test-esm-detect-ambiguous.mjs:55:7) at async Test.run (node:internal/test_runner/test:825:9) at async Suite.processPendingSubtests (node:internal/test_runner/test:533:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'undefined\n', expected: '', operator: 'strictEqual' } } ✔ should be overridden by --experimental-default-type (35.361125ms) ::debug::starting to run should be overridden by --experimental-default-type ::debug::completed running should be overridden by --experimental-default-type ✔ does not trigger detection via source code `eval()` (31.439583ms) ▶ string input (200.426125ms) ::debug::starting to run does not trigger detection via source code `eval()` ::debug::completed running does not trigger detection via source code `eval()` ▶ .js file input in a typeless package ✔ permits CommonJS syntax in a .js entry point (32.397083ms) ::debug::starting to run .js file input in a typeless package ::debug::starting to run permits CommonJS syntax in a .js entry point ::debug::completed running permits CommonJS syntax in a .js entry point ✔ permits ESM syntax in a .js entry point (35.232333ms) ::debug::starting to run permits ESM syntax in a .js entry point ::debug::completed running permits ESM syntax in a .js entry point ✔ permits CommonJS syntax in a .js file imported by a CommonJS entry point (37.020334ms) ::debug::starting to run permits CommonJS syntax in a .js file imported by a CommonJS entry point ::debug::completed running permits CommonJS syntax in a .js file imported by a CommonJS entry point ✔ permits ESM syntax in a .js file imported by a CommonJS entry point (34.671333ms) ::debug::starting to run permits ESM syntax in a .js file imported by a CommonJS entry point ::debug::completed running permits ESM syntax in a .js file imported by a CommonJS entry point ✔ permits CommonJS syntax in a .js f
strictEqual(code, 0);
strictEqual(signal, null);
});

it('should be overridden by --experimental-default-type', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
Expand Down Expand Up @@ -393,3 +406,18 @@
strictEqual(signal, null);
});
});

describe('when working with Worker threads', () => {
it('should support sloppy scripts that declare CJS "global-like" variables', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
'--eval',
'new worker_threads.Worker("let __filename,__dirname,require,module,exports;this.a",{eval:true})',
]);

strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
strictEqual(signal, null);
});
});
Loading