Skip to content

Commit

Permalink
explore: escape args properly on Windows Bash
Browse files Browse the repository at this point in the history
Despite being bash, Node.js running on windows git mingw bash still
executes child processes using cmd.exe.  As a result, arguments in this
environment need to be escaped in the style of cmd.exe, not bash.
  • Loading branch information
isaacs committed Aug 22, 2019
1 parent bf93e91 commit 6cc4cc6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/explore.js
Expand Up @@ -9,10 +9,11 @@ var npm = require('./npm.js')
var spawn = require('./utils/spawn')
var path = require('path')
var fs = require('graceful-fs')
var isWindowsShell = require('./utils/is-windows-shell.js')
var isWindows = require('./utils/is-windows.js')
var escapeExecPath = require('./utils/escape-exec-path.js')
var escapeArg = require('./utils/escape-arg.js')
var output = require('./utils/output.js')
var log = require('npmlog')

function explore (args, cb) {
if (args.length < 1 || !args[0]) return cb(explore.usage)
Expand All @@ -23,7 +24,7 @@ function explore (args, cb) {

var shellArgs = []
if (args) {
if (isWindowsShell) {
if (isWindows) {
var execCmd = escapeExecPath(args.shift())
var execArgs = [execCmd].concat(args.map(escapeArg))
opts.windowsVerbatimArguments = true
Expand All @@ -49,6 +50,7 @@ function explore (args, cb) {
)
}

log.silly('explore', {sh, shellArgs, opts})
var shell = spawn(sh, shellArgs, opts)
shell.on('close', function (er) {
// only fail if non-interactive.
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/escape-arg.js
@@ -1,6 +1,6 @@
'use strict'
var path = require('path')
var isWindowsShell = require('./is-windows-shell.js')
var isWindows = require('./is-windows.js')

/*
Escape the name of an executable suitable for passing to the system shell.
Expand All @@ -15,7 +15,7 @@ any single quotes in the filename.
module.exports = escapify

function escapify (str) {
if (isWindowsShell) {
if (isWindows) {
return '"' + path.normalize(str) + '"'
} else {
if (/[^-_.~/\w]/.test(str)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/escape-exec-path.js
@@ -1,6 +1,6 @@
'use strict'
var path = require('path')
var isWindowsShell = require('./is-windows-shell.js')
var isWindows = require('./is-windows.js')

/*
Escape the name of an executable suitable for passing to the system shell.
Expand All @@ -20,7 +20,7 @@ function windowsQuotes (str) {
}

function escapify (str) {
if (isWindowsShell) {
if (isWindows) {
return path.normalize(str).split(/\\/).map(windowsQuotes).join('\\')
} else if (/[^-_.~/\w]/.test(str)) {
return "'" + str.replace(/'/g, "'\"'\"'") + "'"
Expand Down

1 comment on commit 6cc4cc6

@flash-me
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work for all cases. See #554

Please sign in to comment.