From a59c0490cd9139c791887d2054b808b5c5794aed Mon Sep 17 00:00:00 2001 From: Som Samantray <92726151+SomSamantray@users.noreply.github.com> Date: Wed, 12 Aug 2026 15:02:01 +0000 Subject: [PATCH] deps: honor --use-strict for wrapped CommonJS scripts `node --use-strict script.js` did not apply strict mode to CommonJS scripts. Parser::ParseWrapped(), used to parse the function V8 synthesizes for CommonJS module wrapping, hardcoded LanguageMode::kSloppy instead of consulting the language mode already configured on the ParseInfo. As a result the `--use-strict` CLI flag had no effect on ordinary script execution, even though it worked correctly from the REPL. Pass `info->language_mode()` through to ParseFunctionLiteral() so the wrapped function honors the language mode Node.js configured for the parse, matching V8's behavior for other entry points. Add regression coverage in test/parallel/test-cli-eval.js that runs a script assigning to an undeclared variable with and without `--use-strict`, asserting the ReferenceError is only thrown in the latter case. Fixes: https://github.com/nodejs/node/issues/30039 Signed-off-by: Som Samantray <92726151+SomSamantray@users.noreply.github.com> --- deps/v8/src/parsing/parser.cc | 2 +- test/parallel/test-cli-eval.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/deps/v8/src/parsing/parser.cc b/deps/v8/src/parsing/parser.cc index 509ca9322666..112bbc623d69 100644 --- a/deps/v8/src/parsing/parser.cc +++ b/deps/v8/src/parsing/parser.cc @@ -947,7 +947,7 @@ void Parser::ParseWrapped(Isolate* isolate, ParseInfo* info, FunctionLiteral* function_literal = ParseFunctionLiteral(function_name, location, kSkipFunctionNameCheck, FunctionKind::kNormalFunction, kNoSourcePosition, - FunctionSyntaxKind::kWrapped, LanguageMode::kSloppy, + FunctionSyntaxKind::kWrapped, info->language_mode(), arguments_for_wrapped_function); Statement* return_statement = diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index 5b090279621f..d0c0c9c16f62 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -30,8 +30,10 @@ if (module !== require.main) { const common = require('../common'); const assert = require('assert'); const child = require('child_process'); +const fs = require('fs'); const path = require('path'); const fixtures = require('../common/fixtures'); +const tmpdir = require('../common/tmpdir'); if (process.argv.length > 2) { console.log(process.argv.slice(2).join(' ')); @@ -149,6 +151,29 @@ child.exec(...common.escapePOSIXShell`"${process.execPath}" --use-strict -p proc assert.strictEqual(stderr, ''); })); +// Regression test for https://github.com/nodejs/node/issues/30039. +{ + tmpdir.refresh(); + const scriptPath = tmpdir.resolve('use-strict.js'); + fs.writeFileSync(scriptPath, 'undeclared = 1;'); + + common.spawnPromisified(process.execPath, [scriptPath]) + .then(common.mustCall(({ stdout, stderr, code, signal }) => { + assert.strictEqual(stdout, ''); + assert.strictEqual(stderr, ''); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + })); + + common.spawnPromisified(process.execPath, ['--use-strict', scriptPath]) + .then(common.mustCall(({ stdout, stderr, code, signal }) => { + assert.strictEqual(stdout, ''); + assert.match(stderr, /ReferenceError: undeclared is not defined/); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + })); +} + // Regression test for https://github.com/nodejs/node/issues/3574. { const emptyFile = fixtures.path('empty.js');