Skip to content

Commit

Permalink
fixup! cli: implement --trace-env and --trace-env-[js|native]-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Nov 12, 2024
1 parent 1dc934d commit 8d5d797
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,7 @@ The following accesses will be printed:
* Queries in the form of `Object.hasOwn(process.env, 'KEY')`,
`process.env.hasOwnProperty('KEY')` or `'KEY' in process.env`.
* Deletions in the form of `delete process.env.KEY`.
* Enumerations inf the form of `...process.env` or `Object.keys(process.env)`.

To print the stack trace of the access, use `--trace-env-js-stack` and/or
`--trace-env-native-stack`.
Expand Down
7 changes: 1 addition & 6 deletions src/node_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ bool SafeGetenv(const char* key, std::string* text, Environment* env) {
fprintf(stderr, "undefined\n");
}

if (options->trace_env_native_stack) {
DumpNativeBacktrace(stderr);
}
if (options->trace_env_js_stack) {
DumpJavaScriptBacktrace(stderr);
}
PrintTraceEnvStack(options);
}

return has_env;
Expand Down
14 changes: 12 additions & 2 deletions src/node_env_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,14 @@ Maybe<void> KVStore::AssignToObject(v8::Isolate* isolate,
}

void PrintTraceEnvStack(Environment* env) {
if (env->options()->trace_env_native_stack) {
PrintTraceEnvStack(env->options());
}

void PrintTraceEnvStack(std::shared_ptr<EnvironmentOptions> options) {
if (options->trace_env_native_stack) {
DumpNativeBacktrace(stderr);
}
if (env->options()->trace_env_js_stack) {
if (options->trace_env_js_stack) {
DumpJavaScriptBacktrace(stderr);
}
}
Expand Down Expand Up @@ -483,6 +487,12 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Environment* env = Environment::GetCurrent(info);
CHECK(env->has_run_bootstrapping_code());

if (env->options()->trace_env) {
fprintf(stderr, "[--trace-env] enumerate environment variables\n");

PrintTraceEnvStack(env);
}

info.GetReturnValue().Set(
env->env_vars()->Enumerate(env->isolate()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ namespace credentials {
bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
} // namespace credentials

void PrintTraceEnvStack(Environment* env);
void PrintTraceEnvStack(std::shared_ptr<EnvironmentOptions> options);

void DefineZlibConstants(v8::Local<v8::Object> target);
v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
uv_loop_t* event_loop,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/process-env/enumerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Object.keys(process.env);

const env = { ...process.env };
8 changes: 8 additions & 0 deletions test/parallel/test-trace-env-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ spawnSyncAndAssert(process.execPath, ['--trace-env-js-stack', fixtures.path('pro
assert.match(output, /delete\.js:1:16/);
}
});

// Check enumeration from user land.
spawnSyncAndAssert(process.execPath, ['--trace-env-js-stack', fixtures.path('process-env', 'enumerate.js') ], {
stderr(output) {
assert.match(output, /enumerate\.js:1:8/);
assert.match(output, /enumerate\.js:3:26/);
}
});
8 changes: 8 additions & 0 deletions test/parallel/test-trace-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ spawnSyncAndAssert(process.execPath, ['--trace-env', fixtures.path('process-env'
assert.match(output, /delete environment variable "FOO"/);
}
});

// Check enumeration from user land.
spawnSyncAndAssert(process.execPath, ['--trace-env', fixtures.path('process-env', 'enumerate.js') ], {
stderr(output) {
const matches = output.match(/enumerate environment variables/g);
assert.strictEqual(matches.length, 2);
}
});

0 comments on commit 8d5d797

Please sign in to comment.