From d020daea0f369f439a32ac91ce67b5bf0c64a280 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Fri, 13 Sep 2019 07:03:38 -0400 Subject: [PATCH 1/2] fix: Remove '/.node-spawn-wrap-' from lib/homedir.js export This would cause lib/mungers/env.js to set SPAWN_WRAP_SHIM_ROOT including '/.node-spawn-wrap-' when that variable should point to the directory which should contain `.node-spawn-wrap-*`. --- index.js | 2 +- lib/homedir.js | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 70ea44f..edadc58 100644 --- a/index.js +++ b/index.js @@ -106,7 +106,7 @@ function setup(argv, env) { } const key = process.pid + '-' + crypto.randomBytes(6).toString('hex') - let workingDir = homedir + key + let workingDir = path.resolve(homedir, `.node-spawn-wrap-${key}`) const settings = JSON.stringify({ module: __filename, diff --git a/lib/homedir.js b/lib/homedir.js index c0c0f5f..ee49f28 100644 --- a/lib/homedir.js +++ b/lib/homedir.js @@ -2,7 +2,4 @@ const os = require('os') -const home = process.env.SPAWN_WRAP_SHIM_ROOT || os.homedir() -const homedir = home + '/.node-spawn-wrap-' - -module.exports = homedir +module.exports = process.env.SPAWN_WRAP_SHIM_ROOT || os.homedir() From 26abc8289926c108b6bfda2e2ca90c892eacbad1 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Fri, 13 Sep 2019 07:24:09 -0400 Subject: [PATCH 2/2] fix: Avoid path concatenation --- lib/mungers/cmd.js | 8 ++++---- lib/mungers/env.js | 3 +-- lib/mungers/node.js | 2 +- lib/mungers/sh.js | 5 +++-- lib/mungers/shebang.js | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/mungers/cmd.js b/lib/mungers/cmd.js index 4488e78..d2c13bd 100644 --- a/lib/mungers/cmd.js +++ b/lib/mungers/cmd.js @@ -33,9 +33,9 @@ function mungeCmd(workingDir, options) { if (m) { originalNode = m[2] // TODO: Remove `replace`: seems unused - replace = m[1] + workingDir + '/node.cmd' + m[3] + m[4] + replace = m[1] + path.join(workingDir, 'node.cmd') + m[3] + m[4] newArgs[cmdi + 1] = m[1] + m[2] + m[3] + - ' "' + workingDir + '\\node"' + m[4] + ' "' + path.join(workingDir, 'node') + '"' + m[4] } else { // XXX probably not a good idea to rewrite to the first npm in the // path if it's a full path to npm. And if it's not a full path to @@ -46,8 +46,8 @@ function mungeCmd(workingDir, options) { } let npmPath = whichOrUndefined('npm') || 'npm' - npmPath = path.dirname(npmPath) + '\\node_modules\\npm\\bin\\npm-cli.js' - replace = m[1] + '"' + workingDir + '/node.cmd"' + + npmPath = path.join(path.dirname(npmPath), 'node_modules', 'npm', 'bin', 'npm-cli.js') + replace = m[1] + '"' + path.join(workingDir, 'node.cmd') + '"' + ' "' + npmPath + '"' + m[3] + m[4] newArgs[cmdi + 1] = command.replace(npmre, replace) diff --git a/lib/mungers/env.js b/lib/mungers/env.js index 5496aab..c4a558a 100644 --- a/lib/mungers/env.js +++ b/lib/mungers/env.js @@ -5,7 +5,6 @@ const path = require("path") const homedir = require("../homedir") const pathRe = isWindows() ? /^PATH=/i : /^PATH=/; -const colon = isWindows() ? ';' : ':' /** * Updates the environment variables to intercept `node` commands and pass down options. @@ -23,7 +22,7 @@ function mungeEnv(workingDir, options) { // `5` corresponds to the length of `PATH=` pathEnv = ep.substr(5) const k = ep.substr(0, 5) - return k + workingDir + colon + pathEnv + return k + workingDir + path.delimiter + pathEnv } else { // Return as-is return ep; diff --git a/lib/mungers/node.js b/lib/mungers/node.js index bcd75af..76b3e05 100644 --- a/lib/mungers/node.js +++ b/lib/mungers/node.js @@ -56,7 +56,7 @@ function mungeNode(workingDir, options) { let newFile = options.file; if (hasMain) { - const replace = workingDir + '/' + command + const replace = path.join(workingDir, command) newArgs.splice(mainIndex, 0, replace) } diff --git a/lib/mungers/sh.js b/lib/mungers/sh.js index b96c08a..4107cb4 100644 --- a/lib/mungers/sh.js +++ b/lib/mungers/sh.js @@ -37,10 +37,11 @@ function mungeSh(workingDir, options) { let newArgs = [...options.args]; // Remember the original Node command to use it in the shim let originalNode; + const workingNode = path.join(workingDir, 'node') if (isNode(exe)) { originalNode = command - c = match[1] + match[2] + ' "' + workingDir + '/node" ' + match[3] + c = `${match[1]}${match[2]} "${workingNode}" ${match[3]}` newArgs[cmdi + 1] = c } else if (exe === 'npm' && !isWindows()) { // XXX this will exhibit weird behavior when using /path/to/npm, @@ -48,7 +49,7 @@ function mungeSh(workingDir, options) { const npmPath = whichOrUndefined('npm') if (npmPath) { - c = c.replace(re, '$1 "' + workingDir + '/node" "' + npmPath + '" $3') + c = c.replace(re, `$1 "${workingNode}" "${npmPath}" $3`) newArgs[cmdi + 1] = c debug('npm munge!', c) } diff --git a/lib/mungers/shebang.js b/lib/mungers/shebang.js index 44105ab..6357347 100644 --- a/lib/mungers/shebang.js +++ b/lib/mungers/shebang.js @@ -32,7 +32,7 @@ function mungeShebang(workingDir, options) { const originalNode = shebangbin const file = shebangbin - const args = [shebangbin, workingDir + '/' + maybeNode] + const args = [shebangbin, path.join(workingDir, maybeNode)] .concat(resolved) .concat(match[1].split(' ').slice(1)) .concat(options.args.slice(1))