Skip to content

node -c rejects BOM before #!, but node accepts it #27767

Closed
@bakkot

Description

@bakkot

Version: v12.1.0

$ node -e 'require("fs").writeFileSync("test.js", `\u{FEFF}#!`, "utf8")'
$ node -c test.js
(function (exports, require, module, __filename, __dirname) { #!
                                                              ^
SyntaxError: Invalid or unexpected token
$ echo $?
1
$ node test.js
$ echo $?
0

The bug is that check_syntax.js call stripShebang before stripBOM, but the actual loader calls stripBOM before stripShebag.

It's not totally clear which behavior is desired. The original bug which lead to the introduction of stripBOM actually had a script with a BOM preceding the #!, though the test included in that commit doesn't. But unix systems generally require the #! to be the first two bytes for it to be parsed as an interpreter directive, which means it cannot be preceded by a BOM.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.discussIssues opened for discussions and feedbacks.esmIssues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions