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

--use_strict does not work with script and REPL, only work with stdin and eval #47918

Closed
loynoir opened this issue May 8, 2023 · 3 comments
Closed
Labels
confirmed-bug Issues with confirmed bugs. duplicate Issues and PRs that are duplicates of other issues or PRs. v8 engine Issues and PRs related to the V8 dependency.

Comments

@loynoir
Copy link

loynoir commented May 8, 2023

Background

--use_strict is not my use case.

I'm just investigating --use_strict.

Description

--use_strict works with stdin

$ cat actual.cjs | node --use_strict
{ isStrict: [ true, true ] }

--use_strict works with eval

$ node --use_strict -e "$(cat actual.cjs)"
{ isStrict: [ true, true ] }

--use_strict not works file

$ node --use_strict actual.cjs
{ isStrict: [ false, false ] }

--use_strict not works REPL

$ node --use_strict
Welcome to Node.js v18.15.0.
Type ".help" for more information.
> require('./actual.cjs')
{ isStrict: [ false, false ] }
{}

Version

v18.15.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

==> actual.cjs <==

#!/usr/bin/env -S node --use_strict


var isStrict = (function () {
  var a = !this;

  var b;
  try {
    eval(`with({}){}`);
    b = false;
  } catch {
    b = true;
  }

  return [a, b];
})();

console.log({ isStrict });

==> expected.cjs <==

#!/usr/bin/env -S node
"use strict";

var isStrict = (function () {
  var a = !this;

  var b;
  try {
    eval(`with({}){}`);
    b = false;
  } catch {
    b = true;
  }

  return [a, b];
})();

console.log({ isStrict });

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

$ node expected.cjs             
{ isStrict: [ true, true ] }
$ cat actual.cjs | node --use_strict
{ isStrict: [ true, true ] }
$ node --use_strict -e "$(cat actual.cjs)"
{ isStrict: [ true, true ] }

should be

$ node --use_strict actual.cjs
node:internal/modules/cjs/loader:xxx
  throw err;
  ^

Error:  The --use_strict flag does not work with script and REPL, only work with stdin and eval
    at xxx (node:internal/modules/cjs/loader:xxx:xxx) {
  code: 'INVALID_FLAG',
  requireStack: []
}

Node.js v18.15.0

What do you see instead?

--use_strict not works file

$ node --use_strict actual.cjs
{ isStrict: [ false, false ] }

--use_strict not works REPL

$ node --use_strict
Welcome to Node.js v18.15.0.
Type ".help" for more information.
> require('./actual.cjs')
{ isStrict: [ false, false ] }
{}

Additional information

Seems related

#4196

#6429

@loynoir loynoir changed the title --use_strict does not work with script, only work in REPL and eval --use_strict does not work with script and REPL, only work in stdin and eval May 8, 2023
@loynoir loynoir changed the title --use_strict does not work with script and REPL, only work in stdin and eval --use_strict does not work with script and REPL, only work with stdin and eval May 8, 2023
@bnoordhuis
Copy link
Member

So, I'm reasonably sure this is caused by the use of v8::ScriptCompiler::CompileFunction() vs. v8::Script::Run(). The former is used to evaluate require'd code (that includes the main script), the latter for -e / --eval and also vm.runInContext() and friends.

You can see it for yourself with these two test scripts:

// Flags: --use_strict
with ({}) {} // no exception

vs.

// Flags: --use_strict
require("vm").runInThisContext(`with ({}) {}`) // throws

v8::ScriptCompiler::CompileFunction() does look at the --use_strict flag in v8::internal::Compiler::GetWrappedFunction() but I still have every reason to believe this is a V8 bug, not a Node.js bug.

LanguageMode language_mode = construct_language_mode(v8_flags.use_strict);

@bnoordhuis bnoordhuis added confirmed-bug Issues with confirmed bugs. v8 engine Issues and PRs related to the V8 dependency. labels May 12, 2023
@targos
Copy link
Member

targos commented May 12, 2023

Duplicate of #30039 ?

@bnoordhuis
Copy link
Member

Oh, indeed! I'll go ahead and close this one then.

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2023
@bnoordhuis bnoordhuis added the duplicate Issues and PRs that are duplicates of other issues or PRs. label May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. duplicate Issues and PRs that are duplicates of other issues or PRs. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

No branches or pull requests

3 participants