Skip to content

Commit

Permalink
src: don't match after -- in Dotenv::GetPathFromArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Aug 7, 2024
1 parent 3ed9f98 commit 073daa7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
std::vector<std::string> 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;
}

Expand Down
1 change: 1 addition & 0 deletions test/parallel/should-not-write.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
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', `require('assert').strictEqual(process.env.BASIC, undefined);`,
'--', '--env-file', validEnvFilePath

Check failure on line 107 in test/parallel/test-dotenv-edge-cases.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
],
{ cwd: fixtures.path('dotenv') },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});

0 comments on commit 073daa7

Please sign in to comment.