Skip to content

src: read env files only from Node.js options - #67642

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:cli-env-file-before-script
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:cli-env-file-before-script

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

--env-file and --env-file-if-exists were looked up in every argument up to a literal --, including the ones after the script name. Those arguments belong to the script, but the files they named were still opened and their NODE_OPTIONS applied.

Find where the options of Node.js end with a throwaway parse, the same way --config-file is handled, and only look for env file flags before that point.

Testing: added test/parallel/test-dotenv-args-after-script.js. The test-dotenv*, test-cli-* and test-config-file* suites pass, as do eslint and cpplint.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/config

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. config Issues and PRs related to Node.js configuration and feature settings. needs-ci PRs that need a full CI run. labels Nov 20, 2026
@ShogunPanda
ShogunPanda force-pushed the cli-env-file-before-script branch from 65a1557 to 20d5f8e Compare November 20, 2026 09:01
@ShogunPanda ShogunPanda added the notable-change PRs with changes that should be highlighted in changelogs. label Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

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.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.58%. Comparing base (50ba1ec) to head (25abed7).
⚠️ Report is 310 commits behind head on main.

Files with missing lines Patch % Lines
src/node_dotenv.cc 85.71% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67642      +/-   ##
==========================================
- Coverage   90.71%   90.58%   -0.14%     
==========================================
  Files         801      802       +1     
  Lines      280906   286942    +6036     
  Branches    54246    55403    +1157     
==========================================
+ Hits       254834   259922    +5088     
- Misses      16251    17115     +864     
- Partials     9821     9905      +84     
Files with missing lines Coverage Δ
src/node_config_file.cc 84.24% <100.00%> (-0.82%) ⬇️
src/node_options.cc 88.34% <100.00%> (+0.10%) ⬆️
src/node_options.h 95.86% <100.00%> (+0.10%) ⬆️
src/node_dotenv.cc 86.44% <85.71%> (+1.68%) ⬆️

... and 206 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread doc/api/cli.md
in the file, the value from the environment takes precedence.

The flag is only recognized before the script. Arguments after the script name
or after `--` are passed to the script and are not read as Node.js options.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this make this semver-major?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be semver-major. The documented CLI contract is node [options] … [<program-entry-point>] [--] [arguments]: flags after the script are script arguments, not Node options. That's also what we told users in #58338.

What's actually changing is an inconsistency in the current implementation. GetDataFromArgs still opens --env-file after the script — so a missing file exits 9, and NODE_OPTIONS from the file is applied — but has_env_file_string stays false, so those variables never show up on process.env and the flag never appears in process.execArgv. --config-file was already fixed the same way in #66431 without a semver-major label.

I'll treat it as a bug fix. If we'd rather be conservative I can add the label, but I don't think a deprecation cycle is warranted.

`--env-file` and `--env-file-if-exists` were looked up in every
argument up to a literal `--`, including the ones that follow the script
name. Those belong to the script, but the files they named were still
opened and their `NODE_OPTIONS` applied.

Find where the options of Node.js end with a throwaway parse and only
look for env file flags before that point. The parse is shared with the
config file reader and only runs when an env file flag is present.

Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@ShogunPanda
ShogunPanda force-pushed the cli-env-file-before-script branch from 20d5f8e to 25abed7 Compare December 3, 2026 11:51
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
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++. config Issues and PRs related to Node.js configuration and feature settings. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants