diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 9e0205b4e6249ff..9d00fd3fd8ad986 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -14,21 +14,23 @@ 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.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) { paths.push_back(path->substr(equal_char + 1)); } else { auto next_path = std::next(path); - - if (next_path == args.end()) { + if (next_path == args.end() || *next_path == "--") { return paths; } diff --git a/test/parallel/should-not-write.txt b/test/parallel/should-not-write.txt new file mode 100644 index 000000000000000..b6fc4c620b67d95 --- /dev/null +++ b/test/parallel/should-not-write.txt @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index f8cd262c8a80925..f652d6467bf3382 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); + }); });