nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.23k stars 29.4k forks source link

--use-strict flag doesn't apply strict mode to script #30039

Open Pravv opened 4 years ago

Pravv commented 4 years ago

The --use-strict flag doesn't apply strict mode to script. reproduction:

echo a = 1 > test.js && node --use-strict test

expected behavior: ReferenceError: a is not defined is thrown. behavior: script executes without any issue.

however it works as expected when node is started first and script is called from repl. <12 versions aren't affected.

node --use-strict
Welcome to Node.js v12.12.0.
Type ".help" for more information.
> a = 1
Thrown:
ReferenceError: a is not defined
>
SaltyMonkey commented 4 years ago

Tested some versions of runtime:

test.js file:

module.exports = function()  {
    a = 1;
    console.log(a);
};

Call:

PS E:\test> node --use-strict 
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> let z = require(".\\test");
undefined
> z()
1

Reproduced at: 12.4, 12.6, 12.8, 12.8.1, 12.12 OS: Win 10 x64

addaleax commented 4 years ago

This was caused by b338edbb0ab1ae9823d1128ef9baa4295885b9d7 (the switch to CompileFunctionInContext()). /cc @nodejs/v8 @hashseed @ryzokuken

hashseed commented 4 years ago

Fix should be easy. Just change the language mode in ParseInfo depending on FLAG_use_strict.

targos commented 4 years ago

@hashseed it looks like it's already done?

Refs: https://github.com/v8/v8/blob/16b83b1b4a30f4c71b3114a19926169190785f2c/src/codegen/compiler.cc#L2079-L2164

ryzokuken commented 4 years ago

@targos hmm, weird. By that logic, the default should be strict mode. Let me try building and debugging to see what's happening exactly.

alexfernandez commented 4 years ago

Any news on this? In Node.js v14.7.0 --use-strict is not working, and execution is still not strict by default.

DVLP commented 2 years ago

node 18.1 the problem is still there

loynoir commented 1 year ago

Should have some warnings like

if(has_cli_flag_use_strict) {
  switch(node_running_mode) {
    case 'repl':
    case 'script':
      throw new InValidFlagError("repl and script does not support --use_strict")
    case 'stdin':
    case 'eval':
      // OK with `--use_strict`
      break
    default:
      throw new UnreachableCaseError()
  }
}