Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ static constexpr const char* env_var_separator = ";";
static constexpr const char* env_var_separator = ":";
#endif // _WIN32

static bool IsNodeRunEnvironmentVariable(const std::string& name) {
#ifdef _WIN32
return StringEqualNoCase(name.c_str(), "NODE_RUN_SCRIPT_NAME") ||
StringEqualNoCase(name.c_str(), "NODE_RUN_PACKAGE_JSON_PATH");
#else
return name == "NODE_RUN_SCRIPT_NAME" || name == "NODE_RUN_PACKAGE_JSON_PATH";
#endif // _WIN32
}

ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
const std::filesystem::path& package_json_path,
std::string_view script_name,
Expand Down Expand Up @@ -108,6 +117,12 @@ void ProcessRunner::SetEnvironmentVariables() {
}
#endif // _WIN32

// These variables describe the current task and are added below. Do not
// retain inherited values, as that would create duplicate entries.
if (IsNodeRunEnvironmentVariable(name)) {
continue;
}

if (StringEqualNoCase(name.c_str(), "path")) {
// Add path env variable to the beginning of the PATH
value = path_env_var_ + value;
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-node-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ describe('node --run [command]', () => {
assert.strictEqual(child.code, 0);
});

it('should override inherited special environment variables', async () => {
const scriptName = `special-env-variables${envSuffix}`;
const packageJsonPath = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--run', scriptName],
{
cwd: fixtures.path('run-script'),
env: {
...process.env,
NODE_RUN_SCRIPT_NAME: 'inherited-script-name',
NODE_RUN_PACKAGE_JSON_PATH: 'inherited-package-json-path',
},
},
);
assert.deepStrictEqual(child.stdout.trim().split(/\r?\n/), [
scriptName,
packageJsonPath,
]);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('will search parent directories for a package.json file', async () => {
const packageJsonPath = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
Expand Down
Loading