Skip to content

Commit 79dd87b

Browse files
committed
src: override inherited task runner environment
Filter inherited NODE_RUN_SCRIPT_NAME and NODE_RUN_PACKAGE_JSON_PATH before adding the current task values. This prevents duplicate environment entries during nested node --run invocations. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent a4aa3c0 commit 79dd87b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/node_task_runner.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ static constexpr const char* env_var_separator = ";";
1111
static constexpr const char* env_var_separator = ":";
1212
#endif // _WIN32
1313

14+
static bool IsNodeRunEnvironmentVariable(const std::string& name) {
15+
#ifdef _WIN32
16+
return StringEqualNoCase(name.c_str(), "NODE_RUN_SCRIPT_NAME") ||
17+
StringEqualNoCase(name.c_str(), "NODE_RUN_PACKAGE_JSON_PATH");
18+
#else
19+
return name == "NODE_RUN_SCRIPT_NAME" ||
20+
name == "NODE_RUN_PACKAGE_JSON_PATH";
21+
#endif // _WIN32
22+
}
23+
1424
ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
1525
const std::filesystem::path& package_json_path,
1626
std::string_view script_name,
@@ -108,6 +118,12 @@ void ProcessRunner::SetEnvironmentVariables() {
108118
}
109119
#endif // _WIN32
110120

121+
// These variables describe the current task and are added below. Do not
122+
// retain inherited values, as that would create duplicate entries.
123+
if (IsNodeRunEnvironmentVariable(name)) {
124+
continue;
125+
}
126+
111127
if (StringEqualNoCase(name.c_str(), "path")) {
112128
// Add path env variable to the beginning of the PATH
113129
value = path_env_var_ + value;

test/parallel/test-node-run.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ describe('node --run [command]', () => {
189189
assert.strictEqual(child.code, 0);
190190
});
191191

192+
it('should override inherited special environment variables', async () => {
193+
const scriptName = `special-env-variables${envSuffix}`;
194+
const packageJsonPath = fixtures.path('run-script/package.json');
195+
const child = await common.spawnPromisified(
196+
process.execPath,
197+
[ '--run', scriptName],
198+
{
199+
cwd: fixtures.path('run-script'),
200+
env: {
201+
...process.env,
202+
NODE_RUN_SCRIPT_NAME: 'inherited-script-name',
203+
NODE_RUN_PACKAGE_JSON_PATH: 'inherited-package-json-path',
204+
},
205+
},
206+
);
207+
assert.deepStrictEqual(child.stdout.trim().split(/\r?\n/), [
208+
scriptName,
209+
packageJsonPath,
210+
]);
211+
assert.strictEqual(child.stderr, '');
212+
assert.strictEqual(child.code, 0);
213+
});
214+
192215
it('will search parent directories for a package.json file', async () => {
193216
const packageJsonPath = fixtures.path('run-script/package.json');
194217
const child = await common.spawnPromisified(

0 commit comments

Comments
 (0)