Skip to content

Standalone binary: npm scripts with &&, quotes or node -e run incorrectly #11627

Backlog

Description

@IzaakGough

[REQUIRED] Environment info

firebase-tools: 15.37.0 (standalone binary), and main at the time of writing

Platform: macOS (applies to the Linux/macOS shell wrapper; Windows not checked)

[REQUIRED] Test case

package.json:

{
  "scripts": {
    "two": "node a.js && node b.js",
    "inline": "node -e \"console.log(1)\"",
    "quoted": "node a.js \"x y\""
  }
}

a.js:

console.log("A", JSON.stringify(process.argv.slice(2)));

b.js:

console.log("B ran");

[REQUIRED] Steps to reproduce

  1. Use the standalone CLI binary, which sets npm's script-shell to its bundled shell.js wrapper.
  2. Run any of the scripts above through that wrapper, e.g. shell.js -c "node a.js && node b.js".

[REQUIRED] Expected behavior

The same result as sh -c:

$ sh -c "node a.js && node b.js"
A []
B ran

[REQUIRED] Actual behavior

Script Result
node a.js && node b.js a.js gets &&, node, b.js as args, b.js never runs, exit 0
node -e "console.log(1)" Cannot find module '<cwd>/-e', exit 1
node a.js "x y" a.js gets "x and y" as two args
$ node shell.js -c "node a.js && node b.js"
A ["&&","node","b.js"]
$ echo $?
0

$ node shell.js -c 'node -e "console.log(1)"'
Error: Cannot find module '<cwd>/-e'
  code: 'MODULE_NOT_FOUND',

$ node shell.js -c 'node a.js "x y"'
A ["\"x","y\""]

The -e case breaks packages whose install scripts use node -e, such as core-js's postinstall, so installs that include them fail.

These were reproduced by building shell.js the way createRuntimeBinaries in standalone/firepit.js does and running it with node, not with a full standalone binary build.

Cause: Script_ShellJS in standalone/runtime.js splits the script on spaces and runs node commands with fork, without a shell:

let [cmdRuntime, cmdScript, ...otherArgs] = args[0].split(" ");
// ...
if (cmdRuntime === "node") {
  cmd = child_process.fork(cmdScript, otherArgs, { ... });
}

So && and quotes are never interpreted, and the token after node is always treated as a script path.

Workaround: Use the CLI installed from npm (npm install -g firebase-tools), which does not use this wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions