Skip to content

Commit ead7e84

Browse files
Explicitly clear NODE environment variables to prevent affecting the spawned app
1 parent 83194e7 commit ead7e84

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/apply-defaults.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ module.exports = applyDefaults;
66
* Applies default values (if any) to the arguments
77
*/
88
function applyDefaults (defaults, originalArgs) {
9-
if (!defaults || (!defaults.command && !defaults.args && !defaults.options)) {
10-
// No defaults are set, so just return the args as-is
11-
return originalArgs;
12-
}
9+
defaults = defaults || {};
1310

1411
let command = defaults.command;
1512
let args = Array.prototype.slice.call(originalArgs);
@@ -71,6 +68,16 @@ function applyDefaults (defaults, originalArgs) {
7168
}
7269
}
7370

71+
// By default, use the current process's environment variables.
72+
// except for NODE environment variables, which could impact how the child process runs.
73+
options = options || {};
74+
if (!options.env) {
75+
options.env = Object.assign({}, process.env, {
76+
NODE_ENV: "",
77+
NODE_OPTIONS: "",
78+
});
79+
}
80+
7481
if (typeof args === "string") {
7582
return [command + " " + args, options];
7683
}

test/specs/defaults.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe("defaults", () => {
144144

145145
describe("defaults.options", () => {
146146
it("should use the default options", () => {
147-
chaiExec.defaults.options = { env: { ...process.env, foo: "Hello", bar: "world" }};
147+
chaiExec.defaults.options = { env: { ...process.env, NODE_OPTIONS: "", foo: "Hello", bar: "world" }};
148148
let cli = chaiExec("test/fixtures/bin/echo-env foo bar");
149149

150150
cli.command.should.equal("test/fixtures/bin/echo-env");
@@ -155,7 +155,7 @@ describe("defaults", () => {
155155

156156
it("should use the default command and options", () => {
157157
chaiExec.defaults.command = "test/fixtures/bin/echo-env";
158-
chaiExec.defaults.options = { env: { ...process.env, foo: "Hello", bar: "world" }};
158+
chaiExec.defaults.options = { env: { ...process.env, NODE_OPTIONS: "", foo: "Hello", bar: "world" }};
159159
let cli = chaiExec("foo bar");
160160

161161
cli.command.should.equal("test/fixtures/bin/echo-env");
@@ -166,7 +166,7 @@ describe("defaults", () => {
166166
it("should use the default command, args, and options", () => {
167167
chaiExec.defaults.command = "test/fixtures/bin/echo-env";
168168
chaiExec.defaults.args = "foo bar";
169-
chaiExec.defaults.options = { env: { ...process.env, foo: "Hello", bar: "world" }};
169+
chaiExec.defaults.options = { env: { ...process.env, NODE_OPTIONS: "", foo: "Hello", bar: "world" }};
170170
let cli = chaiExec();
171171

172172
cli.command.should.equal("test/fixtures/bin/echo-env");
@@ -176,7 +176,7 @@ describe("defaults", () => {
176176

177177
it("should append args to the default args, and options", () => {
178178
chaiExec.defaults.args = ["foo", "bar"];
179-
chaiExec.defaults.options = { env: { ...process.env, foo: "Hello", bar: "world", biz: "AAA", baz: "BBB" }};
179+
chaiExec.defaults.options = { env: { ...process.env, NODE_OPTIONS: "", foo: "Hello", bar: "world", biz: "AAA", baz: "BBB" }};
180180
let cli = chaiExec(["test/fixtures/bin/echo-env", "biz", "baz"]);
181181

182182
cli.command.should.equal("test/fixtures/bin/echo-env");
@@ -187,7 +187,7 @@ describe("defaults", () => {
187187
it("should append args to the default command, args, and options", () => {
188188
chaiExec.defaults.command = "test/fixtures/bin/echo-env";
189189
chaiExec.defaults.args = "foo bar";
190-
chaiExec.defaults.options = { env: { ...process.env, foo: "Hello", bar: "world", biz: "AAA", baz: "BBB" }};
190+
chaiExec.defaults.options = { env: { ...process.env, NODE_OPTIONS: "", foo: "Hello", bar: "world", biz: "AAA", baz: "BBB" }};
191191
let cli = chaiExec("biz baz");
192192

193193
cli.command.should.equal("test/fixtures/bin/echo-env");

0 commit comments

Comments
 (0)