Skip to content

[BUG] Using -- to pass additional arguments incorrectly wraps those arguments in quotes #2720

Closed
@Pomax

Description

@Pomax

Current Behavior:

Using -- to pass additional arguments incorrectly wraps those arguments in quotes

Expected Behavior:

Using -- to pass additional arguments does what https://docs.npmjs.com/cli/v6/commands/npm-run-script claims it will do:

npm will pass all the arguments after the -- directly to your script:

I take "directly" here to mean "without any kind of rewriting"

Steps To Reproduce:

a package.json with:

"scripts": {
  "x": "echo test",
  "y": "npm run x -- --flag"
}

shows:

npm run y               

> y
> npm run x -- --flag

> x
> echo test "--flag"

test --flag

instead of

npm run y               

> y
> npm run x -- --flag

> x
> echo test --flag

test --flag

Reason this is a bug

this makes it impossible to forward flags with escaped quotes. For example, passing a quoted string (in which the quotes must be preserved) to esbuild becomes impossible:

"scripts": {
    "esbuild:base": "esbuild --bundle --loader:.js=jsx --loader:.jsx=jsx",
    "esbuild:base:main": "npm run esbuild:base -- ./source/js/main.js --outfile=./network-api/networkapi/frontend/_js/main.js",
    "esbuild:dev:main": "npm run esbuild:base:main -- --define:process.env.NODE_ENV=\\\"development\\\" --watch"
}

Note the escaped development string, which is escaped once so " doesn't break the script string, and escaped a second time so that it's passed correctly as CLI argument.

This should run, but instead because things get wrapped in quotes, this errors out:

npm run esbuild:dev:main

> esbuild:dev:main
> npm run esbuild:base:main -- --define:process.env.NODE_ENV=\"development\" --watch


> esbuild:base:main
> npm run esbuild:base -- ./source/js/main.js --outfile=./network-api/networkapi/frontend/_js/main.js "--define:process.env.NODE_ENV=\\development\\" "--watch"


> esbuild:base
> esbuild --bundle --loader:.js=jsx --loader:.jsx=jsx "./source/js/main.js" "--outfile=./network-api/networkapi/frontend/_js/main.js" "--define:process.env.NODE_ENV=\\\\development\\\\" "--watch"

> error: Invalid define value (must be valid JSON syntax or a single identifier): \\\\development\\\\

Note the line above the error: we suddenly have quoted arguments in which --define:process.env.NODE_ENV=\\\"development\\\" has been rewritten such that it actually loses quotes: "--define:process.env.NODE_ENV=\\\\development\\\\" no longer has a quote preceding the d in development.

Nothing should be rewriting args passed further along with --, those should stay exactly as they were.

Environment:

Windows 10 Pro x64
Node 15..8.0
npm 7.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 7.xwork is associated with a specific npm 7 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions