src: don't match after -- in Dotenv::GetPathFromArgs#54237
Conversation
|
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-fileSo 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-fileboth exit with |
That is indeed an issue, @anonrig do you want me to patch it here or in another PR? |
| 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.
| 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. |
Co-Authored-By: Cedric Staniewski <cedric@gmx.ca>
|
Once this lands, I'll open a PR fixing the bug with |
|
@redyetidev, there is one last thing still missing from the PR to fully fix #54232, which is being able to use a script argument This command will now work: node script.js -- --env-file nonexistent-filebut it is quite unusual to require a I would like to call the script like this: node script.js --env-file nonexistent-fileor ./script.js --env-file nonexistent-fileTo archive this, it should be enough to check for arguments that do not start with a hyphen everywhere where you check for Change return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file=");to return arg == "--" || !arg.starts_with("-") || arg == "--env-file" || arg.starts_with("--env-file=");Change if (*path == "--") {to if (*path == "--" || !(*path).starts_with("-")) { |
|
No, the current behavior IMO is correct. Any arguments passed after the script are treated as script arguments, not arguments to Node itself. Anything after |
|
So this works now with your changes? node script.js --env-file nonexistent-fileBecause in the latest release it exists with |
|
No, that doesn't work, but I don't believe it should. Any arguments passed after the script to run are passed to the script, not to the node executable. |
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 <38299977+RedYetiDev@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> -------------------------------------------------------------------------------- ℹ 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 |
|
This would need a backport to land on v22.x as it uses C++20 features. |
Fixes #54232