Skip to content

src: extend --env-file to also accept sections #58782

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

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

Conversation

dario-piotrowicz
Copy link
Member

Fixes: #58729

@dario-piotrowicz dario-piotrowicz requested a review from anonrig June 21, 2025 21:49
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/config
  • @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 Jun 21, 2025
@dario-piotrowicz dario-piotrowicz force-pushed the dario/58729/env-ini-sections branch from ff88ca1 to c1edd49 Compare June 21, 2025 21:59
@dario-piotrowicz

This comment was marked as resolved.

Copy link

codecov bot commented Jun 21, 2025

Codecov Report

Attention: Patch coverage is 88.79310% with 13 lines in your changes missing coverage. Please review.

Project coverage is 90.08%. Comparing base (e679e38) to head (5af1b31).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/node_dotenv.cc 87.32% 4 Missing and 5 partials ⚠️
src/node_util.cc 76.92% 0 Missing and 3 partials ⚠️
src/node.cc 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58782      +/-   ##
==========================================
- Coverage   90.08%   90.08%   -0.01%     
==========================================
  Files         640      640              
  Lines      188316   188370      +54     
  Branches    36922    36936      +14     
==========================================
+ Hits       169650   169687      +37     
+ Misses      11406    11404       -2     
- Partials     7260     7279      +19     
Files with missing lines Coverage Δ
lib/internal/process/per_thread.js 99.46% <100.00%> (+<0.01%) ⬆️
lib/util.js 97.79% <100.00%> (+0.02%) ⬆️
src/node_dotenv.h 100.00% <ø> (ø)
src/node_process_methods.cc 88.09% <100.00%> (+0.23%) ⬆️
src/node.cc 74.86% <50.00%> (+0.03%) ⬆️
src/node_util.cc 81.79% <76.92%> (+<0.01%) ⬆️
src/node_dotenv.cc 85.65% <87.32%> (+1.04%) ⬆️

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

@dario-piotrowicz dario-piotrowicz force-pushed the dario/58729/env-ini-sections branch from c1edd49 to ee7e6c3 Compare June 22, 2025 17:41
@dario-piotrowicz dario-piotrowicz requested a review from jasnell June 22, 2025 17:43
Make sure that `--env-file` can accept sections,
also extend the `parseEnv` and `process.loadEnvFile`
APIs to accept sections as well
@dario-piotrowicz dario-piotrowicz force-pushed the dario/58729/env-ini-sections branch from ee7e6c3 to d6e8866 Compare June 22, 2025 17:56
@vitaly-t
Copy link
Contributor

vitaly-t commented Jun 22, 2025

Based on the issue discussion, I believe we are only short of testing variables with dots in the name:

one.two.three = some value

[section]
four.five = another value

At the moment we only test section names with dots.

The rest looks good! 👍

@@ -26,6 +27,33 @@ std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
arg.starts_with("--env-file-if-exists=");
};

const auto get_sections = [](const std::string& path) {
Copy link
Member

Choose a reason for hiding this comment

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

I recommend always using std::string_view since you'll always know when you'll copy (by calling std::string(val))

Suggested change
const auto get_sections = [](const std::string& path) {
const auto get_sections = [](std::string_view path) {

@@ -154,6 +215,33 @@ void Dotenv::ParseContent(const std::string_view input) {
continue;
}

if (content.front() == '[') {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment to here explaining why you're checking for [?

Copy link
Contributor

Choose a reason for hiding this comment

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

He is checking for the start of a section, I believe - [section_name]

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but a year from now, a contributor might have some hard time understanding the reasoning.

Copy link
Member Author

Choose a reason for hiding this comment

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

comment added 🙂

@@ -303,7 +391,8 @@ void Dotenv::ParseContent(const std::string_view input) {
}
}

Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path) {
Dotenv::ParseResult Dotenv::ParsePath(const std::string_view path,
const std::set<std::string> sections) {
Copy link
Member

Choose a reason for hiding this comment

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

If you don't add &, you'll always copy.

Suggested change
const std::set<std::string> sections) {
const std::set<std::string>& sections) {

@@ -590,21 +590,42 @@ static void Execve(const FunctionCallbackInfo<Value>& args) {
}
#endif

struct IterateSectionsData {
Isolate* isolate;
std::set<std::string>* sections;
Copy link
Member

Choose a reason for hiding this comment

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

This is considered unsafe. Why do you need to have a pointer to a set?

ToNamespacedPath(env, &path_value);
auto path = path_value.ToString();

CHECK(args[1]->IsArray());
Copy link
Member

Choose a reason for hiding this comment

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

If you make this optional, you don't need to allocate a std::set every time this function is called.


THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path);

Dotenv dotenv{};

switch (dotenv.ParsePath(path)) {
switch (dotenv.ParsePath(path, sections)) {
Copy link
Member

Choose a reason for hiding this comment

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

This copies the function.

If you want to avoid copy you should make it set&, if you want to move the ownership to the function since you don't need it inside this function, you should set function parameter to set&& and pass the argument as std::move(sections)

dario-piotrowicz and others added 2 commits June 23, 2025 00:44
validate sections only when present in options object
update jsdoc types for options

Co-authored-by: Yagiz Nizipli <[email protected]>
@dario-piotrowicz
Copy link
Member Author

The rest looks good! 👍

Thanks 😄

Based on the issue discussion, I believe we are only short of testing variables with dots in the name:

one.two.three = some value

[section]
four.five = another value

At the moment we only test section names with dots.

As I mentioned in the discussion I believe that variable key testing is unrelated to the sections support addition (and that more testing is missing not just only around dots) so I don't see a strong need for adding it here and I would much rather do that in its own dedicated PR, do you disagree? 🙂

@anonrig
Copy link
Member

anonrig commented Jun 23, 2025

I agree that dot based variable overriding is unrelated and probably an overkill feature to implement.

@vitaly-t
Copy link
Contributor

vitaly-t commented Jun 23, 2025

I agree that dot based variable overriding is unrelated and probably an overkill feature to implement.

Dots in in the variables names work currently (I tested it in v24.2.0), so it is not a feature, and extra tests would be just to cover that we do not break that.

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend --env-file with support for INI sections
5 participants