Skip to content

Commit 0bf4a6f

Browse files
committed
src: create task runner PATH when missing
Track whether the inherited environment contains PATH and create it from the discovered node_modules/.bin paths when absent. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent a4aa3c0 commit 0bf4a6f

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/node_task_runner.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ void ProcessRunner::SetEnvironmentVariables() {
9393
int env_count;
9494
CHECK_EQ(0, uv_os_environ(&env_items, &env_count));
9595

96+
bool has_path_env_var = false;
97+
9698
// Iterate over environment variables once to store them in the current
9799
// ProcessRunner instance.
98100
for (int i = 0; i < env_count; i++) {
@@ -110,12 +112,17 @@ void ProcessRunner::SetEnvironmentVariables() {
110112

111113
if (StringEqualNoCase(name.c_str(), "path")) {
112114
// Add path env variable to the beginning of the PATH
115+
has_path_env_var = true;
113116
value = path_env_var_ + value;
114117
}
115118
env_vars_.push_back(name + "=" + value);
116119
}
117120
uv_os_free_environ(env_items, env_count);
118121

122+
if (!has_path_env_var) {
123+
env_vars_.push_back("PATH=" + path_env_var_);
124+
}
125+
119126
// Add NODE_RUN_SCRIPT_NAME environment variable to the environment
120127
// to indicate which script is being run.
121128
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + script_name_);

test/parallel/test-node-run.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ describe('node --run [command]', () => {
4545
assert.strictEqual(child.code, 0);
4646
});
4747

48+
it('creates PATH when it is missing from the environment', async () => {
49+
const env = Object.fromEntries(
50+
Object.entries(process.env)
51+
.filter(([key]) => key.toLowerCase() !== 'path'),
52+
);
53+
const child = await common.spawnPromisified(
54+
process.execPath,
55+
[ '--run', `ada${envSuffix}`],
56+
{ cwd: fixtures.path('run-script'), env },
57+
);
58+
assert.match(child.stdout, /06062023/);
59+
assert.strictEqual(child.stderr, '');
60+
assert.strictEqual(child.code, 0);
61+
});
62+
4863
it('chdirs into package directory', async () => {
4964
const child = await common.spawnPromisified(
5065
process.execPath,

0 commit comments

Comments
 (0)