Skip to content

[BUG] inconsistent interpretation of backslash passed to npm scripts in windows #2638

@highmtworks

Description

@highmtworks

I'm sorry to report a minor issue.

Tools built on node tend to use npm scripts in package.json as shortcuts of various tasks.
Passing paths of files or directories to such tools is a common use case,
but it sometimes meets a difficulty when the path contains backslashes in windows.

Current Behavior:

package.json

{
  "scripts": {
    "start": "node index.js \\"
  }
}

index.js

console.log(process.argv.slice(2));

npm start commands:

(1)> npm start
(2)> npm start -- \
(3)> npm start -- "\"
(4)> npm start -- "\""
(5)> npm start -- \\
(6)> npm start -- "\\"
(7)> npm start -- \a
(8)> npm start -- "\a"
(9)> npm start -- \\a
(10)> npm start -- "\\a"

in npm 7.5.2

(1)#-> [ '\\' ]
(2)#-> [ '\\', '\\\\' ]
(3)#-> npm ERR! String is missing a " character
(4)#-> npm ERR! String is missing a " character
(5)#-> [ '\\', '\\\\\\\\' ]
(6)#-> [ '\\', '\\\\' ]
(7)#-> [ '\\', '\\\\a' ]
(8)#-> [ '\\', '\\\\a' ]
(9)#-> [ '\\', '\\\\\\\\a' ]
(10)#-> [ '\\', '\\\\\\\\a' ]

in npm 6.4.11

(1)#-> [ '\\' ]
(2)#-> [ '\\', '"' ]
(3)#-> [ '\\', '"' ]
(4)#-> [ '\\', '"' ]
(5)#-> [ '\\', '\\' ]
(6)#-> [ '\\', '"' ]
(7)#-> [ '\\', '\\a' ]
(8)#-> [ '\\', '\\a' ]
(9)#-> [ '\\', '\\\\a' ]
(10)#-> [ '\\', '\\\\a' ]

Expected Behavior:

I hope the result is the same as that of corresponding node commands:

(1)> node index.js \
(2)> node index.js \ \
(3)> node index.js \ "\"
(4)> node index.js \ "\""
(5)> node index.js \ \\
(6)> node index.js \ "\\"
(7)> node index.js \ \a
(8)> node index.js \ "\a"
(9)> node index.js \ \\a
(10)> node index.js \ "\\a"

in both npm 7.5.2 and npm 6.4.11

(1)#-> [ '\\' ]
(2)#-> [ '\\', '\\' ]
(3)#-> [ '\\', '"' ]
(4)#-> [ '\\', '"' ]
(5)#-> [ '\\', '\\\\' ]
(6)#-> [ '\\', '\\' ]
(7)#-> [ '\\', '\\a' ]
(8)#-> [ '\\', '\\a' ]
(9)#-> [ '\\', '\\\\a' ]
(10)#-> [ '\\', '\\\\a' ]

but it looks quite hard to resolve.

Steps To Reproduce:

see Current Behavior section.

Environment:

  • OS: Windows 10 Pro 2004 (19041.746)
  • Node: 15.8.0
  • npm: 7.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 7.xwork is associated with a specific npm 7 releaseplatform:windowsis Windows-specific

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions