Skip to content

Commit

Permalink
src: don't match after -- in Dotenv::GetPathFromArgs
Browse files Browse the repository at this point in the history
Co-Authored-By: Cedric Staniewski <cedric@gmx.ca>
PR-URL: nodejs#54237
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
2 people authored and aduh95 committed Feb 6, 2025
1 parent 93447ab commit 37169e4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ 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;
auto arg_chars = arg.c_str();
auto arg_len = arg.size();
if (arg_chars[0] != '-' || arg_chars[1] != '-') return false;
if (arg_len == 2) return true; // arg == "--"
const std::string_view flag = "env-file";
const auto len = flag.size();
if (strncmp(arg_chars + 2, flag.data(), len) != 0) return false;
return arg_len == 2 + len || arg_chars[2 + len] == '=';
};
std::vector<std::string> paths;
auto path = std::find_if(args.begin(), args.end(), find_match);

while (path != args.end()) {
if (path->size() == 2 && strncmp(path->c_str(), "--", 2) == 0) {
return paths;
}
auto equal_char = path->find('=');

if (equal_char != std::string::npos) {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('should handle when --env-file is passed along with --', async () => {
const child = await common.spawnPromisified(
process.execPath,
[
'--eval', 'assert.strictEqual(process.env.BASIC, undefined);',
'--', '--env-file', validEnvFilePath,
],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});

0 comments on commit 37169e4

Please sign in to comment.