From 880c446d9558b5c8b90228b077b2721c3bb6070f Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Tue, 13 Aug 2024 19:56:56 -0400 Subject: [PATCH] src: don't match after `--` in `Dotenv::GetPathFromArgs` Co-Authored-By: Cedric Staniewski PR-URL: https://github.com/nodejs/node/pull/54237 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- src/node_dotenv.cc | 6 ++++-- test/parallel/test-dotenv-edge-cases.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 9e0205b4e6249f..8bc027b24c718f 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -14,13 +14,15 @@ using v8::String; std::vector Dotenv::GetPathFromArgs( const std::vector& 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 == "--env-file" || arg.starts_with("--env-file="); }; std::vector paths; auto path = std::find_if(args.begin(), args.end(), find_match); while (path != args.end()) { + if (*path == "--") { + return paths; + } auto equal_char = path->find('='); if (equal_char != std::string::npos) { diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index f8cd262c8a8092..5528d0b47fecb1 100644 --- a/test/parallel/test-dotenv-edge-cases.js +++ b/test/parallel/test-dotenv-edge-cases.js @@ -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', `require('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); + }); });