Skip to content

Conversation

dario-piotrowicz
Copy link
Member

We already have this logic in place for process.loadEnvFile so I figured that it could make sense to just repurpose it also provide a user facing function.

This function is the analogous of util.parseEnv but for files.

@nodejs-github-bot
Copy link
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++. 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 Jul 19, 2025
@dario-piotrowicz dario-piotrowicz force-pushed the dario/util/load-env-file branch from fcc6903 to 08e836c Compare July 19, 2025 17:55
});

it('loadEnvFile does not mutate --env-file output', async () => {
it('loadEnvFile overrides env vars loaded with --env-file', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

this is a breaking change, but I hope it's fine given that dotenv is experimental (and I don't imagine many developers relying on this behavior? 🤔)

I am also not sure about the rational, personally if a new env file is loaded onto process.env I would expect that it would override whatever else is there 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

If this is a problem I can keep the existing behavior.

Since I am thinking of making more changes in this space, to avoid churn and I would prefer this PR not to be a semver-major one if possible.

Comment on lines +359 to +360
const loaded = util.loadEnvFile(path);
ObjectAssign(process.env, loaded);
Copy link
Member Author

Choose a reason for hiding this comment

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

Given how trivial it is to load a .env file onto process.env I even wonder if we could deprecate/remove process.loadEnvFile.

To me personally it feels much clearer to have this sort of operation more manual and have the user load their .env files withloadEnvFile and then use the values to populate process.env themselves if and how they want (with any overriding, filtering, etc. logic).

@dario-piotrowicz dario-piotrowicz added the semver-minor PRs that contain new features and should be released in the next minor version. label Jul 19, 2025
@dario-piotrowicz dario-piotrowicz force-pushed the dario/util/load-env-file branch from 08e836c to 6d075c6 Compare July 19, 2025 18:06
Copy link

codecov bot commented Jul 19, 2025

Codecov Report

❌ Patch coverage is 84.61538% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.05%. Comparing base (c8d5b39) to head (6d075c6).
⚠️ Report is 443 commits behind head on main.

Files with missing lines Patch % Lines
src/node_util.cc 69.23% 5 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #59125      +/-   ##
==========================================
+ Coverage   90.03%   90.05%   +0.02%     
==========================================
  Files         648      648              
  Lines      190967   190984      +17     
  Branches    37425    37443      +18     
==========================================
+ Hits       171931   171988      +57     
+ Misses      11665    11625      -40     
  Partials     7371     7371              
Files with missing lines Coverage Δ
lib/internal/process/per_thread.js 99.27% <100.00%> (-0.19%) ⬇️
lib/util.js 97.83% <100.00%> (+0.08%) ⬆️
src/node_process.h 25.00% <ø> (ø)
src/node_process_methods.cc 88.55% <ø> (+0.74%) ⬆️
src/node_util.cc 80.74% <69.23%> (-0.93%) ⬇️

... and 40 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.

@aduh95
Copy link
Contributor

aduh95 commented Jul 28, 2025

We already have this logic in place for process.loadEnvFile so I figured that it could make sense to just repurpose it also provide a user facing function.

I don't get it, how is process.loadEnvFile not user facing?

@dario-piotrowicz
Copy link
Member Author

We already have this logic in place for process.loadEnvFile so I figured that it could make sense to just repurpose it also provide a user facing function.

I don't get it, how is process.loadEnvFile not user facing?

Sorry, yeah my wording there is misleading, yes, of course process.loadEnvFile is user facing, my idea here was to add another function (still user facing) that is a generalization of process.loadEnvFile.

Basically process.loadEnvFile loads a dotenv file onto an object, and that object is (necessarily) process.env, so I was thinking, wouldn't it make more sense to have a loadEnvFile utility that allows the user to load a dotenv file onto any object? (and that object could even be process.env itself, making process.loadEnvFile not really necessary anymore).

@dario-piotrowicz
Copy link
Member Author

dario-piotrowicz commented Jul 28, 2025

My idea here was related on having a counterpart for parseEnv, there being a utility for parsing a dotenv content but not one for loading that from a file feels to me like a missing API.

I was also thinking of maybe trying to take this even a step further and have a node:dotenv module where these two functions plus any potential new dotenv function could live (like for serializing an object into a dotenv file, of functions for validating dotenv content, etc...), this could be a nice little new module that gives users a bunch of useful tooling for dealing with dotenv files 🙂 (I am thinking of also tooling authors that might want to use node for doing more complex dotenv operations)

@targos
Copy link
Member

targos commented Jul 29, 2025

It seems redundant when you can basically do the same with util.parseEnv(fs.readFileSync('.env', 'utf8')).

@dario-piotrowicz
Copy link
Member Author

It seems redundant when you can basically do the same with util.parseEnv(fs.readFileSync('.env', 'utf8')).

Mh... under that argument I'd also say that process.loadEnvFile is also redundant since you can do the same with:

Object.assign(process.env, util.parseEnv(fs.readFileSync('.env', 'utf8')))

no? 🤔

@aduh95
Copy link
Contributor

aduh95 commented Jul 29, 2025

Wasn't util.parseEnv introduced because process.loadEnvFile was too narrow?

@dario-piotrowicz
Copy link
Member Author

Wasn't util.parseEnv introduced because process.loadEnvFile was too narrow?

I don't know to be honest... I just saw that they were introduced together (#51476)

@targos
Copy link
Member

targos commented Jul 29, 2025

See #51476 (comment)

@dario-piotrowicz
Copy link
Member Author

Closing since I don't think we're going to go with this API due to the conversation above ( 🥹 )

@dario-piotrowicz dario-piotrowicz deleted the dario/util/load-env-file branch September 21, 2025 21:07
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++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. 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.

4 participants