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 1 commit 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 will error, 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 87.80488% with 5 lines in your changes missing coverage. Please review.

Project coverage is 88.24%. Comparing base (3c4ef34) to head (3decd15).
Report is 96 commits behind head on main.

Files with missing lines Patch % Lines
src/node_dotenv.cc 83.33% 3 Missing and 1 partial ⚠️
src/node.cc 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54913      +/-   ##
==========================================
+ Coverage   88.06%   88.24%   +0.17%     
==========================================
  Files         652      652              
  Lines      183544   183902     +358     
  Branches    35861    35864       +3     
==========================================
+ Hits       161644   162282     +638     
+ Misses      15151    14912     -239     
+ Partials     6749     6708      -41     
Files with missing lines Coverage Δ
src/node_dotenv.h 100.00% <ø> (ø)
src/node_options.cc 88.04% <ø> (-0.02%) ⬇️
src/node_options.h 98.21% <ø> (ø)
src/node.cc 74.42% <94.11%> (+0.56%) ⬆️
src/node_dotenv.cc 91.56% <83.33%> (-0.97%) ⬇️

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

@BoscoDomingo
Copy link
Contributor

BoscoDomingo commented Sep 17, 2024

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.

It's one thing that Dotenv::GetDataFromArgs did. It may simply be incompatible with the goal of this PR? Not knowledgeable enough with the code to assert that with confidence tbh. However, my first guess would be to store the position each argument so you can later on zip both lists together. Again, no idea if possible but a potential solution

e.g.

node --env-file=1.env --some-other-arg --env-file=2.env --inspect --env-file-if-exists=3.env

cli_options->env_files = {
  0: "1.env",
  2: "2.env"
}

cli_options->optional_env_files = {
  4: "3.env"
}

@RedYetiDev
Copy link
Member Author

I fixed the issue by iterating over the arguments. It's kind of a mix between what was done previously.

test/parallel/test-dotenv-edge-cases.js Show resolved Hide resolved
src/node.cc Show resolved Hide resolved
src/node_dotenv.cc Show resolved Hide resolved
@@ -277,12 +229,9 @@ Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path) {
return ParseResult::Valid;
}

void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) const {
Copy link
Member

Choose a reason for hiding this comment

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

This removed implementation doesn't make any unnecessary copy, but your new implementation does.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not unneccessery. It needs to be an std::string for ProcessGlobalArgsInternal

@RedYetiDev RedYetiDev added the wip Issues and PRs that are still a work in progress. label Sep 17, 2024
@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
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++. 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*
6 participants