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

src: parse dotenv with the rest of the options #54913

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RedYetiDev
Copy link
Member

@RedYetiDev RedYetiDev commented Sep 12, 2024

Closes #54385
Fixes #54255
Ref #54232


I don't think this breaks anything. IIUC, the Dotenv parser was setup before everything else so that it triggered before V8 initialized, however, even it's setup after the options are parsed, V8 still hasn't been initialized, so it should be fine.


This fixes all currently known dotenv edge cases, as it doesn't need a custom parser.

Such as:

node script.js --env-file .env
node --env-file-ABCD .env
node -- --env-file .env
node --invalid --env-file .env # this would've errored, but the env file is still parsed

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@RedYetiDev RedYetiDev added the dotenv Issues and PRs related to .env file parsing label Sep 12, 2024
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 12, 2024
Copy link

codecov bot commented Sep 13, 2024

Codecov Report

Attention: Patch coverage is 89.23077% with 7 lines in your changes missing coverage. Please review.

Project coverage is 88.40%. Comparing base (9f9069d) to head (6395703).
Report is 166 commits behind head on main.

Files with missing lines Patch % Lines
src/node.cc 77.77% 4 Missing and 2 partials ⚠️
src/node_options.cc 95.83% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54913      +/-   ##
==========================================
+ Coverage   88.39%   88.40%   +0.01%     
==========================================
  Files         652      654       +2     
  Lines      186777   187649     +872     
  Branches    36039    36093      +54     
==========================================
+ Hits       165109   165899     +790     
- Misses      14917    14994      +77     
- Partials     6751     6756       +5     
Files with missing lines Coverage Δ
src/node_dotenv.cc 93.66% <100.00%> (+1.13%) ⬆️
src/node_dotenv.h 100.00% <ø> (ø)
src/node_options-inl.h 81.10% <100.00%> (+0.29%) ⬆️
src/node_options.h 98.28% <100.00%> (+0.05%) ⬆️
src/node_options.cc 87.86% <95.83%> (+0.45%) ⬆️
src/node.cc 74.08% <77.77%> (+0.33%) ⬆️

... and 178 files with indirect coverage changes

@targos
Copy link
Member

targos commented Sep 13, 2024

If it fixes a lot of things, shouldn't it add a lot of tests?

@RedYetiDev RedYetiDev force-pushed the dotenv-fix-1 branch 2 times, most recently from a91e51c to 14e9cd0 Compare September 13, 2024 21:13
@RedYetiDev
Copy link
Member Author

Tests have been added + rebased.

Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

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

The way the test is done is not very robust: if the error message changes in the future, it will be wrong but still pass. It would be better to assert the actual error

@RedYetiDev
Copy link
Member Author

The failed GitHub tests are flakes, could someone start a CI? I don't have the permissions to start one without approvals

@RedYetiDev
Copy link
Member Author

I have to incorporate #53060

@BoscoDomingo
Copy link
Contributor

I have to incorporate #53060

FYI @anonrig had a plan to use string_views instead of strings for the file paths (I couldn't make it work), so you may want to check with him to avoid stepping on each other's work 👍🏻

src/node.cc Outdated Show resolved Hide resolved
Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

Can you rebase your branch with main, with the recent changes, it is extremely hard to review.

@RedYetiDev
Copy link
Member Author

RedYetiDev commented Sep 17, 2024

I'm currently facing the problem of: The files aren't parsed in the same order they were supplied. It's loading all of the optionals, then all of the normals. Any ideas?

@RedYetiDev RedYetiDev force-pushed the dotenv-fix-1 branch 3 times, most recently from 950cb47 to 929a225 Compare September 17, 2024 15:19
@BoscoDomingo
Copy link
Contributor

I'm currently facing the problem of: The files aren't parsed in the same order they were supplied. It's loading all of the optionals, then all of the normals. Any ideas?

Well you are grabbing them all by creating vectors and referencing those via cli_options, then calling process_files on the optional files before the required ones. I'm on my phone so I can't check, but I would assume that could be the reason.

You're likely respecting argument order inside each vector, it's just you're segregating them by optionality first

@RedYetiDev
Copy link
Member Author

You're likely respecting argument order inside each vector, it's just you're segregating them by optionality first

Exactly... I just don't know how to fix that.

@RedYetiDev
Copy link
Member Author

I'm not a expert CPP developer, so LMK if I made a mistake.

@RedYetiDev RedYetiDev removed the wip Issues and PRs that are still a work in progress. label Sep 23, 2024
@RedYetiDev
Copy link
Member Author

RedYetiDev commented Sep 23, 2024

As shocking as it may seem, all Github failures are unrelated flakes.


@anonrig is this better to what you had in mind?

@xduugu
Copy link
Contributor

xduugu commented Sep 26, 2024

First of all, thanks a lot for still working on a proper fix for the env file option issues, @RedYetiDev.

I'm currently facing the problem of: The files aren't parsed in the same order they were supplied. It's loading all of the optionals, then all of the normals. Any ideas?

I saw that you already fixed it, but maybe it could be solved much easier:

From my understanding, the order of --env-file and --env-file-if-exists options is only relevant, if there are variables that occur in multiple env files. Because an env file overwrites variables with the same name from env files read before, it makes no sense to load the "optional" env file before the "required" one, because the latter will always overwrite the former one (please correct me if I miss a use case). So the only order that makes sense, is loading the "required" env file before the "optional" one, because the optional env file is only read if it exists.

My proposed solution is, first reading all the "required" env files in the order they appear in the option list, then all "optional" ones, and adding a note about the env file processing order to the documentation.

@RedYetiDev
Copy link
Member Author

IMHO the current setup makes more sense, that is, parse in the order the options where passed.

src/node_options.h Outdated Show resolved Hide resolved
src/node_options.cc Outdated Show resolved Hide resolved
@RedYetiDev RedYetiDev added cli Issues and PRs related to the Node.js command line interface. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed review wanted PRs that need reviews. labels Oct 10, 2024
@RedYetiDev
Copy link
Member Author

I've modified the implementation to use a new DetailedOption parsing method, which gets both the option value and the option flag, This allows for the program to accurately determine which option index and which option type to parse.

@RedYetiDev RedYetiDev force-pushed the dotenv-fix-1 branch 2 times, most recently from 630ee2f to 31b6f4e Compare October 11, 2024 00:04
@RedYetiDev RedYetiDev removed the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Oct 17, 2024
@RedYetiDev
Copy link
Member Author

RedYetiDev commented Oct 19, 2024

@anonrig are you still blocking? I think I've resolved all your concerns.

As for the string copying, I believe it's needed, as other functions are expecting a string.

@RedYetiDev
Copy link
Member Author

PTAL @nodejs/cpp-reviewers

@RedYetiDev
Copy link
Member Author

Any other concerns, @lemire?

@lemire
Copy link
Member

lemire commented Nov 5, 2024

@RedYetiDev I don't have any concern, but I'd like to hear what @anonrig has to say about this PR. He is still asking for changes which I consider blocking.

One thing is certain: this has been opened in September. We should do something with it.

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++. cli Issues and PRs related to the Node.js command line interface. dotenv Issues and PRs related to .env file parsing needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotenv::GetPathFromArgs matches --env-file*
8 participants