Skip to content

Fixes bug: unable to spawn .bat with %<environment variable>% in name #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var assign = require('lodash.assign');

var resolveCommand = require('./util/resolveCommand');
var hasEmptyArgumentBug = require('./util/hasEmptyArgumentBug');
var escapeArgument = require('./util/escapeArgument');
Expand All @@ -17,6 +19,10 @@ function parseNonShell(parsed) {
var shebang;
var needsShell;
var applyQuotes;
var command;
var args;
var envVariableName;
var envVariableSuffix = 1;

if (!isWin) {
return parsed;
Expand All @@ -39,13 +45,28 @@ function parseNonShell(parsed) {
if (needsShell) {
// Escape command & arguments
applyQuotes = (parsed.command !== 'echo'); // Do not quote arguments for the special "echo" command
parsed.command = escapeCommand(parsed.command);
parsed.args = parsed.args.map(function (arg) {
command = escapeCommand(parsed.command);
args = parsed.args.map(function (arg) {
return escapeArgument(arg, applyQuotes);
});

// Make use of cmd.exe
parsed.args = ['/d', '/s', '/c', '"' + parsed.command + (parsed.args.length ? ' ' + parsed.args.join(' ') : '') + '"'];
// Anything passed to cmd.exe after the `/c` argument is subject to environment variable expansion.
// This means, for example, that if we intend to execute a file named "%cd%.bat" we need to ensure cmd.exe
// doesn't replace "%cd%" with the value of the "cd" environment variable. (See tests for an example)
// The only way to do this is to pass our entire command string as an environment variable and tell
// cmd to execute it as a command. (environment variable expansion is not recursive)
// We must also delete our custom environment variable first to prevent it from leaking into the environment
// of the command we're executing.

// Must copy `env` to avoid mutating the original
parsed.options.env = assign({}, parsed.options.env || process.env);
do {
envVariableName = 'cross_spawn_command_' + envVariableSuffix;
envVariableSuffix += 1;
} while (Object.prototype.hasOwnProperty.call(parsed.options.env, envVariableName));
parsed.options.env[envVariableName] = command + (args.length ? ' ' + args.join(' ') : '');
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining this technique and why it's necessary for future reading.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I added a description.

parsed.args = ['/d', '/s', '/c', '"set ' + envVariableName + '=& %' + envVariableName + '%"'];
parsed.command = process.env.comspec || 'cmd.exe';
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
}
Expand Down Expand Up @@ -95,7 +116,7 @@ function parse(command, args, options) {
}

args = args ? args.slice(0) : []; // Clone array to avoid changing the original
options = options || {};
options = options ? assign({}, options) : {}; // Clone object to avoid changing the original

// Build our parsed object
parsed = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"author": "IndigoUnited <hello@indigounited.com> (http://indigounited.com)",
"license": "MIT",
"dependencies": {
"lodash.assign": "^4.2.0",
"lru-cache": "^4.0.1",
"shebang-command": "^1.2.0",
"which": "^1.2.9"
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/%CD%
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo special
2 changes: 2 additions & 0 deletions test/fixtures/%CD%.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo special
1 change: 1 addition & 0 deletions test/fixtures/echoEnv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "$FOO""$BAR"
2 changes: 2 additions & 0 deletions test/fixtures/echoEnv.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo %FOO%%BAR%
64 changes: 64 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path');
var expect = require('expect.js');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var assign = require('lodash.assign');
var hasEmptyArgumentBug = require('../lib/util/hasEmptyArgumentBug');
var spawn = require('../');
var buffered = require('./util/buffered');
Expand Down Expand Up @@ -140,6 +141,50 @@ extension\');', { mode: parseInt('0777', 8) });
});
});

it('should handle commands with names of environment variables', function (next) {
buffered(method, __dirname + '/fixtures/%CD%', function (err, data, code) {
expect(err).to.not.be.ok();
expect(code).to.be(0);
expect(data.trim()).to.equal('special');

next();
});
});

describe('should preserve environment variables', function () {
before(function () {
process.env.FOO = 'foovalue';
});

it('when env dictionary is omitted', function (next) {
buffered(method, __dirname + '/fixtures/echoEnv', function (err, data, code) {
expect(err).to.not.be.ok();
expect(code).to.be(0);
expect(data.trim()).to.equal('foovalue');

next();
});
});

it('when env dictionary is passed', function (next) {
buffered(method, __dirname + '/fixtures/echoEnv', {
env: {
BAR: 'barvalue',
},
}, function (err, data, code) {
expect(err).to.not.be.ok();
expect(code).to.be(0);
expect(data.trim()).to.equal('barvalue');

next();
});
});

after(function () {
delete process.env.FOO;
});
});

it('should handle arguments with quotes', function (next) {
buffered(method, 'node', [
__dirname + '/fixtures/echo',
Expand Down Expand Up @@ -524,6 +569,25 @@ extension\');', { mode: parseInt('0777', 8) });
});
}
}

it('should not mutate passed `options` or `env` objects', function (next) {
var options = {
env: {
a: 'a',
b: 'b',
},
};
var env = options.env;
var optionsClone = assign({}, options);
var envClone = assign({}, env);

buffered(method, __dirname + '/fixtures/foo', options, function (err) {
expect(err).to.not.be.ok();
expect(options).to.eql(optionsClone);
expect(env).to.eql(envClone);
next();
});
});
});
});
});