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: fix dotenv parsing commented env #52363

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ using v8::String;
* available at https://github.com/motdotla/dotenv
*/
const std::regex LINE(
"\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']"
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?"
":#.*)?"); // NOLINT(whitespace/line_length)
"(?:^|^)\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?"
Copy link
Contributor Author

@climba03003 climba03003 Apr 4, 2024

Choose a reason for hiding this comment

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

The previous implementation is copied from dotenv but the regexp is missing the beginning part (?:^|^).

So, it cannot determine the commented env properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like it is removed because std::regex::multiline is not supported.
The current workflow using C++11 only and that required C++17

Copy link

Choose a reason for hiding this comment

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

I'm not sure that (?:^|^) part is makes much sense, and can be simplified just to (?:^)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After testing, it can be simplified to (?:^).
Due the problem of std::regex::multiline not supported in some environment.
I believe the option of just changing the regex is a no-go.

":\\\\'|[^'])*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^"
"#\r\n]+)?\\s*(?:#.*)?", std::regex::multiline); // NOLINT(whitespace/line_length)

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ MULTI_NOT_VALID_QUOTE="
MULTI_NOT_VALID=THIS
IS NOT MULTILINE
export EXAMPLE = ignore export

#COMMENTED_ENV=should not shown
#COMMENTED_ENV_FOLLOW_COMMENTED_ENV=should not shown
#COMMENTED_ENV_FOLLOW_COMMENTED_ENV_2=should not shown
4 changes: 4 additions & 0 deletions test/parallel/test-dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines');
assert.strictEqual(process.env.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines');
// Ignore export before key
assert.strictEqual(process.env.EXAMPLE, 'ignore export');
// Commented environment should be undefined
assert.strictEqual(process.env.COMMENTED_ENV, undefined);
assert.strictEqual(process.env.COMMENTED_ENV_FOLLOW_COMMENTED_ENV, undefined);
assert.strictEqual(process.env.COMMENTED_ENV_FOLLOW_COMMENTED_ENV_2, undefined);
Loading