-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: don't match after --
in Dotenv::GetPathFromArgs
#54237
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are needed
Is there is a specific file I should put the tests in? If not, I'll make a new file for them, but I wanted to make sure first. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #54237 +/- ##
==========================================
- Coverage 87.10% 87.10% -0.01%
==========================================
Files 647 647
Lines 181739 181755 +16
Branches 34886 34886
==========================================
+ Hits 158307 158317 +10
- Misses 16740 16742 +2
- Partials 6692 6696 +4
|
Could you elaborate on that? |
I don't know too much about CPP internals, so I may have got that part wrong. From what I understood, it's faster to use |
I am not familiar with this part of the code base, but any iteration here is just going over the few, typically very short strings that were passed as command-line arguments, right? If this patch actually prevents copying unnecessarily, please explain that and I'm probably going to be in favor of such a change. Optimizing iteration over a few hundred characters, on the other hand, seems like it will be of little benefit. |
Dotenv::GetPathFromArgs
--
in Dotenv::GetPathFromArgs
Thanks a lot for working on a fix, @RedYetiDev. I just noticed I also included the node works.js --env-file nonexistent-file So the argument processing need to stop when this condition is true: arg == "--" || !arg.starts_with("-") When looking at the code, I noticed another bug: every argument that starts with node --env-file-asdf nonexistent-file
node --env-file-asdf=nonexistent-file both exit with
|
That is indeed an issue, @anonrig do you want me to patch it here or in another PR? |
src/node_dotenv.cc
Outdated
@@ -14,21 +14,23 @@ using v8::String; | |||
std::vector<std::string> Dotenv::GetPathFromArgs( | |||
const std::vector<std::string>& args) { | |||
const auto find_match = [](const std::string& arg) { | |||
const std::string_view flag = "--env-file"; | |||
return strncmp(arg.c_str(), flag.data(), flag.size()) == 0; | |||
return arg == "--" || arg.starts_with("--env-file"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return arg == "--" || arg.starts_with("--env-file"); | |
return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file="); |
This is the patch for the behavior described:
(before)
└─$ node --env-file-ABCD TEST
node: TEST: not found
(after)
└─$ node --env-file-ABCD TEST
node: bad option: --env-file-ABCD
@anonrig The for loop @RedYetiDev used was much easier to understand for me. It might be less sophisticated (and a little less performant) as the current code, but wouldn't something like the following be a lot more readable? (probably no valid c++ code) std::vector<std::string> Dotenv::GetPathFromArgs(const std::vector<std::string>& args) {
const std::string_view flag = "--env-file";
std::vector<std::string> paths;
for (size_t i = 0; i < args.size(); ++i) {
const std::string_view arg = args[i];
// argument separator or script file
if (arg == "--" || !arg.starts_with("-")) {
break;
}
// --env-file <file>
if (arg == flag) {
if (i + 1 < args.size()) {
paths.push_back(args[i + 1]);
++i;
}
// TODO else error?
// --env-file=<file>
} else if (arg.starts_with(flag + "=") {
paths.push_back(std::string(arg.substr(flag.size() + 1)));
}
}
return paths;
} |
I'm planning to re-try the loop again in a new PR once this lands. |
efb86ba
to
fc3c121
Compare
Co-Authored-By: Cedric Staniewski <[email protected]>
--env-file supports NODE_OPTIONS. therefore, it needs to be executed/analyzed before V8 is initialized. |
FWIW once this lands, I'll open a seperate PR re-writing the function in a way that fixes all these little bugs mentioned: std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
std::vector<std::string> paths;
for (size_t i = 1; i < args.size(); ++i) {
const auto& arg = args[i];
if (arg == "--" || arg[0] != '-') {
break;
}
if (arg.starts_with("--env-file=")) {
paths.push_back(arg.substr(11)); // Directly extract the path
} else if (arg == "--env-file" && i + 1 < args.size()) {
paths.push_back(args[++i]); // Advance to the next argument
}
}
return paths;
} |
|
Hey, I think this is ready to land. No objections and a passing CI :-) No rush tho |
Commit Queue failed- Loading data for nodejs/node/pull/54237 ✔ Done loading data for nodejs/node/pull/54237 ----------------------------------- PR info ------------------------------------ Title src: don't match after `--` in `Dotenv::GetPathFromArgs` (#54237) ⚠ Could not retrieve the email or name of the PR author's from user's GitHub profile! Branch RedYetiDev:dotenv-fix -> nodejs:main Labels c++, needs-ci, dotenv Commits 1 - src: don't match after `--` in `Dotenv::GetPathFromArgs` Committers 1 - RedYetiDev <[email protected]> PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <[email protected]> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <[email protected]> -------------------------------------------------------------------------------- ℹ This PR was created on Wed, 07 Aug 2024 00:11:34 GMT ✔ Approvals: 1 ✔ - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/54237#pullrequestreview-2226545215 ✘ This PR needs to wait 35 more hours to land (or 0 hours if there is one more approval) ✔ Last GitHub CI successful ℹ Last Full PR CI on 2024-08-10T19:46:38Z: https://ci.nodejs.org/job/node-test-pull-request/61004/ - Querying data for job/node-test-pull-request/61004/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/10351730728 |
I already did change that piece of code in #53060. Might want to add there? |
Commit Queue failed- Loading data for nodejs/node/pull/54237 ✔ Done loading data for nodejs/node/pull/54237 ----------------------------------- PR info ------------------------------------ Title src: don't match after `--` in `Dotenv::GetPathFromArgs` (#54237) ⚠ Could not retrieve the email or name of the PR author's from user's GitHub profile! Branch RedYetiDev:dotenv-fix -> nodejs:main Labels c++, needs-ci, dotenv Commits 1 - src: don't match after `--` in `Dotenv::GetPathFromArgs` Committers 1 - RedYetiDev <[email protected]> PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <[email protected]> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <[email protected]> -------------------------------------------------------------------------------- ℹ This PR was created on Wed, 07 Aug 2024 00:11:34 GMT ✔ Approvals: 1 ✔ - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/54237#pullrequestreview-2226545215 ✘ This PR needs to wait 1 more hours to land (or 0 hours if there is one more approval) ✔ Last GitHub CI successful ℹ Last Full PR CI on 2024-08-12T12:21:23Z: https://ci.nodejs.org/job/node-test-pull-request/61004/ - Querying data for job/node-test-pull-request/61004/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/10378367203 |
Landed in 880c446 |
Co-Authored-By: Cedric Staniewski <[email protected]> PR-URL: #54237 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
Co-Authored-By: Cedric Staniewski <[email protected]> PR-URL: #54237 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
This would need a backport to land on v22.x as it uses C++20 features. |
Fixes #54232