Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: implement --trace-env and --trace-env-[js|native]-stack #55604

Merged
merged 5 commits into from
Nov 27, 2024

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Oct 30, 2024

This implements --trace-env, --trace-env-js-stack and --trace-env-native-stack CLI options that print information about any access to environment variables done in the current Node.js instance to stderr, including:

  • The environment variable reads that Node.js does internally.
  • Writes in the form of process.env.KEY = "SOME VALUE".
  • Reads in the form of process.env.KEY.
  • Definitions in the form of Object.defineProperty(process.env, 'KEY', {...}).
  • 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).

Only the names of the environment variables being accessed are printed. The values are not printed. To print the stack trace of the access, use --trace-env-js-stack and/or --trace-env-native-stack.

$ cat env.js
const foo = process.env.FOO;
process.env.FOO = foo + "foo";

$ out/Release/node --trace-env env.js
[--trace-env] get environment variable "NODE_ICU_DATA"
[--trace-env] get environment variable "OPENSSL_CONF"
[--trace-env] get environment variable "NODE_EXTRA_CA_CERTS"
[--trace-env] get environment variable "NODE_DEBUG_NATIVE"
[--trace-env] get environment variable "NODE_COMPILE_CACHE"
[--trace-env] get environment variable "NODE_NO_WARNINGS"
[--trace-env] get environment variable "NODE_V8_COVERAGE"
[--trace-env] get environment variable "NODE_DEBUG"
[--trace-env] get environment variable "NODE_CHANNEL_FD"
[--trace-env] get environment variable "NODE_UNIQUE_ID"
[--trace-env] get environment variable "HOME"
[--trace-env] get environment variable "NODE_PATH"
[--trace-env] get environment variable "WATCH_REPORT_DEPENDENCIES"
[--trace-env] get environment variable "FOO"
[--trace-env] set environment variable "FOO"
See output of `--trace-env-js-stack`
out/Release/node --trace-env-js-stack env.js
[--trace-env] get environment variable "NODE_ICU_DATA"
[--trace-env] get environment variable "OPENSSL_CONF"
[--trace-env] get environment variable "NODE_EXTRA_CA_CERTS"
[--trace-env] get environment variable "NODE_DEBUG_NATIVE"
[--trace-env] get environment variable "NODE_COMPILE_CACHE"
[--trace-env] get environment variable "NODE_NO_WARNINGS"

----- JavaScript stack trace -----

1: setupWarningHandler (node:internal/process/pre_execution:263:17)
2: prepareExecution (node:internal/process/pre_execution:102:3)
3: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
4: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "NODE_V8_COVERAGE"

----- JavaScript stack trace -----

1: setupCodeCoverage (node:internal/process/pre_execution:333:19)
2: prepareExecution (node:internal/process/pre_execution:107:3)
3: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
4: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "NODE_DEBUG"

----- JavaScript stack trace -----

1: setupDebugEnv (node:internal/process/pre_execution:363:68)
2: prepareExecution (node:internal/process/pre_execution:108:3)
3: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
4: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "NODE_CHANNEL_FD"

----- JavaScript stack trace -----

1: setupChildProcessIpcChannel (node:internal/process/pre_execution:497:19)
2: prepareExecution (node:internal/process/pre_execution:133:5)
3: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
4: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "NODE_UNIQUE_ID"

----- JavaScript stack trace -----

1: initializeClusterIPC (node:internal/process/pre_execution:514:38)
2: prepareExecution (node:internal/process/pre_execution:137:5)
3: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
4: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "HOME"

----- JavaScript stack trace -----

1: node:internal/modules/cjs/loader:1782:57
2: initializeCJS (node:internal/modules/cjs/loader:427:12)
3: initializeCJSLoader (node:internal/process/pre_execution:590:3)
4: setupUserModules (node:internal/process/pre_execution:155:3)
5: prepareExecution (node:internal/process/pre_execution:148:5)
6: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
7: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "NODE_PATH"

----- JavaScript stack trace -----

1: node:internal/modules/cjs/loader:1783:56
2: initializeCJS (node:internal/modules/cjs/loader:427:12)
3: initializeCJSLoader (node:internal/process/pre_execution:590:3)
4: setupUserModules (node:internal/process/pre_execution:155:3)
5: prepareExecution (node:internal/process/pre_execution:148:5)
6: prepareMainThreadExecution (node:internal/process/pre_execution:47:10)
7: node:internal/main/run_main_module:15:19


[--trace-env] get environment variable "WATCH_REPORT_DEPENDENCIES"

----- JavaScript stack trace -----

1: node:internal/modules/cjs/loader:161:63
2: node:internal/util:809:15
3: reportModuleToWatchMode (node:internal/modules/cjs/loader:276:7)
4: node:internal/modules/cjs/loader:1106:5
5: traceSync (node:diagnostics_channel:322:14)
6: wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
7: executeUserEntryPoint (node:internal/modules/run_main:170:5)
8: node:internal/main/run_main_module:36:49


[--trace-env] get environment variable "FOO"

----- JavaScript stack trace -----

1: /Users/joyee/projects/node/env.js:1:25
2: node:internal/modules/cjs/loader:1546:14
3: node:internal/modules/cjs/loader:1698:10
4: node:internal/modules/cjs/loader:1303:32
5: node:internal/modules/cjs/loader:1117:12
6: traceSync (node:diagnostics_channel:322:14)
7: wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
8: executeUserEntryPoint (node:internal/modules/run_main:170:5)
9: node:internal/main/run_main_module:36:49


[--trace-env] set environment variable "FOO"

----- JavaScript stack trace -----

1: /Users/joyee/projects/node/env.js:2:17
2: node:internal/modules/cjs/loader:1546:14
3: node:internal/modules/cjs/loader:1698:10
4: node:internal/modules/cjs/loader:1303:32
5: node:internal/modules/cjs/loader:1117:12
6: traceSync (node:diagnostics_channel:322:14)
7: wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
8: executeUserEntryPoint (node:internal/modules/run_main:170:5)
9: node:internal/main/run_main_module:36:49

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Oct 30, 2024
@richardlau richardlau added the semver-minor PRs that contain new features and should be released in the next minor version. label Oct 30, 2024
Copy link

codecov bot commented Oct 30, 2024

Codecov Report

Attention: Patch coverage is 98.75000% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.95%. Comparing base (1d01ad6) to head (81713b5).
Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
src/env.cc 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #55604      +/-   ##
==========================================
- Coverage   88.01%   87.95%   -0.06%     
==========================================
  Files         653      656       +3     
  Lines      187735   188372     +637     
  Branches    35874    35980     +106     
==========================================
+ Hits       165229   165687     +458     
- Misses      15693    15845     +152     
- Partials     6813     6840      +27     
Files with missing lines Coverage Δ
src/debug_utils.cc 60.90% <100.00%> (+1.50%) ⬆️
src/debug_utils.h 80.00% <ø> (ø)
src/node.cc 73.60% <100.00%> (-0.15%) ⬇️
src/node_credentials.cc 69.28% <100.00%> (+1.13%) ⬆️
src/node_env_var.cc 87.02% <100.00%> (+2.21%) ⬆️
src/node_internals.h 81.03% <ø> (ø)
src/node_options.cc 87.55% <100.00%> (+0.08%) ⬆️
src/node_options.h 98.29% <100.00%> (+0.01%) ⬆️
src/path.cc 69.04% <ø> (ø)
src/env.cc 85.49% <66.66%> (-0.19%) ⬇️

... and 42 files with indirect coverage changes

src/node_credentials.cc Outdated Show resolved Hide resolved
@joyeecheung
Copy link
Member Author

Updated this a bit and added tracing for enumeration. I figured it would also be useful to see where e.g. the ...process.env accesses are coming from.

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 12, 2024
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Nov 12, 2024
Copy link
Contributor

Failed to start CI
   ⚠  No approving reviews found
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/11789675299

@targos
Copy link
Member

targos commented Nov 12, 2024

Shouldn't collaborators be allowed to start CI without approving reviews?

@legendecas
Copy link
Member

Shouldn't collaborators be allowed to start CI without approving reviews?

There was an update on the PR after the request-ci Add this label to start a Jenkins CI on a PR. was added, which failed the check here.

@joyeecheung joyeecheung added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Nov 19, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 19, 2024
@nodejs-github-bot
Copy link
Collaborator

This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.
@joyeecheung
Copy link
Member Author

Fixed test to accomondate different builds.

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 19, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 19, 2024
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 21, 2024
@StefanStojanovic
Copy link
Contributor

Not sure why coverage-windows is failing but that seems to predate this PR: https://github.com/nodejs/node/actions/workflows/coverage-windows.yml

This is the reason nodejs/build#3963. The mentioned should now be disabled as said in #55929.

@joyeecheung
Copy link
Member Author

Updated to not printing values. Can you take a look again? @legendecas @jasnell

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 26, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 26, 2024
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added request-ci Add this label to start a Jenkins CI on a PR. commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Nov 27, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 27, 2024
@nodejs-github-bot nodejs-github-bot merged commit 9029aec into nodejs:main Nov 27, 2024
67 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 9029aec

@joyeecheung joyeecheung added the notable-change PRs with changes that should be highlighted in changelogs. label Nov 27, 2024
Copy link
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @joyeecheung.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

targos pushed a commit that referenced this pull request Dec 2, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: #55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
github-actions bot pushed a commit that referenced this pull request Dec 3, 2024
Notable changes:

assert:
  * (SEMVER-MINOR) add partialDeepStrictEqual (Giovanni Bucci) #54630
cli:
  * (SEMVER-MINOR) implement --trace-env and --trace-env-[js|native]-stack (Joyee Cheung) #55604
doc,lib,src,test:
  * (SEMVER-MINOR) unflag sqlite module (Colin Ihrig) #55890
net:
  * (SEMVER-MINOR) support blocklist for net.Server (theanarkh) #56079
process:
  * (SEMVER-MINOR) deprecate `features.{ipv6,uv}` and `features.tls_*` (René) #55545
sqlite:
  * (SEMVER-MINOR) add `StatementSync.prototype.iterate` method (tpoisseau) #54213

PR-URL: TODO
github-actions bot pushed a commit that referenced this pull request Dec 3, 2024
Notable changes:

assert:
  * (SEMVER-MINOR) add partialDeepStrictEqual (Giovanni Bucci) #54630
cli:
  * (SEMVER-MINOR) implement --trace-env and --trace-env-[js|native]-stack (Joyee Cheung) #55604
doc,lib,src,test:
  * (SEMVER-MINOR) unflag sqlite module (Colin Ihrig) #55890
net:
  * (SEMVER-MINOR) support blocklist for net.Server (theanarkh) #56079
process:
  * (SEMVER-MINOR) deprecate `features.{ipv6,uv}` and `features.tls_*` (René) #55545
sqlite:
  * (SEMVER-MINOR) add `StatementSync.prototype.iterate` method (tpoisseau) #54213

PR-URL: #56119
targos pushed a commit that referenced this pull request Dec 3, 2024
Notable changes:

assert:
  * (SEMVER-MINOR) add partialDeepStrictEqual (Giovanni Bucci) #54630
cli:
  * (SEMVER-MINOR) implement --trace-env and --trace-env-[js|native]-stack (Joyee Cheung) #55604
doc,lib,src,test:
  * (SEMVER-MINOR) unflag sqlite module (Colin Ihrig) #55890
net:
  * (SEMVER-MINOR) support blocklist for net.Server (theanarkh) #56079
process:
  * (SEMVER-MINOR) deprecate `features.{ipv6,uv}` and `features.tls_*` (René) #55545
sqlite:
  * (SEMVER-MINOR) add `StatementSync.prototype.iterate` method (tpoisseau) #54213

PR-URL: #56119
github-actions bot pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this pull request Dec 4, 2024
This implements --trace-env, --trace-env-js-stack and
--trace-env-native-stack CLI options which can be used to find
out what environment variables are accessed and where they are
accessed.

PR-URL: nodejs#55604
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants