Skip to content
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
7 changes: 7 additions & 0 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ void ProcessRunner::SetEnvironmentVariables() {
int env_count;
CHECK_EQ(0, uv_os_environ(&env_items, &env_count));

bool has_path_env_var = false;

// Iterate over environment variables once to store them in the current
// ProcessRunner instance.
for (int i = 0; i < env_count; i++) {
Expand All @@ -110,12 +112,17 @@ void ProcessRunner::SetEnvironmentVariables() {

if (StringEqualNoCase(name.c_str(), "path")) {
// Add path env variable to the beginning of the PATH
has_path_env_var = true;
value = path_env_var_ + value;
}
env_vars_.push_back(name + "=" + value);
}
uv_os_free_environ(env_items, env_count);

if (!has_path_env_var) {
env_vars_.push_back("PATH=" + path_env_var_);
}

// Add NODE_RUN_SCRIPT_NAME environment variable to the environment
// to indicate which script is being run.
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + script_name_);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-node-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ describe('node --run [command]', () => {
assert.strictEqual(child.code, 0);
});

it('creates PATH when it is missing from the environment', async () => {
const env = Object.fromEntries(
Object.entries(process.env)
.filter(([key]) => key.toLowerCase() !== 'path'),
);
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `ada${envSuffix}`],
{ cwd: fixtures.path('run-script'), env },
);
assert.match(child.stdout, /06062023/);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('chdirs into package directory', async () => {
const child = await common.spawnPromisified(
process.execPath,
Expand Down
Loading