From 37169e4f1f1c9cb69e74a48ad6dfbec181b6ff1e 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 1/3] 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 | 13 +++++++++++-- test/parallel/test-dotenv-edge-cases.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 12eb0446de0a7a..dfbf0a0bfa0acb 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -14,13 +14,22 @@ 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; + 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 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) { diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index f8cd262c8a8092..baad85fa6b7988 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', '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); + }); }); From c4e38c42e7ca156536a4ea18966f898e14926ed0 Mon Sep 17 00:00:00 2001 From: Bosco Domingo <46006784+BoscoDomingo@users.noreply.github.com> Date: Mon, 16 Sep 2024 02:58:16 +0200 Subject: [PATCH 2/3] src: add `--env-file-if-exists` flag Fixes: https://github.com/nodejs/node/issues/50993 Refs: https://github.com/nodejs/node/issues/51451 test: remove unnecessary comment src: conform to style guidelines src: change flag to `--env-file-optional` test: revert automatic linter changes doc: fix typos src: change flag to `--env-file-if-exists` src: refactor `env_file_data` and `GetEnvFileDataFromArgs` test: clean up tests src: print error when file not found test: remove unnecessary extras PR-URL: https://github.com/nodejs/node/pull/53060 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- doc/api/cli.md | 16 +++++++ src/node.cc | 18 +++++--- src/node_dotenv.cc | 55 ++++++++++++++++++------- src/node_dotenv.h | 6 ++- src/node_options.cc | 4 ++ src/node_options.h | 1 + test/parallel/test-dotenv-edge-cases.js | 54 ++++++++++++++++++++---- 7 files changed, 122 insertions(+), 32 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 63d2d00efb8eb6..b408d1b6d022d2 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -801,6 +801,8 @@ in the file, the value from the environment takes precedence. You can pass multiple `--env-file` arguments. Subsequent files override pre-existing variables defined in previous files. +An error is thrown if the file does not exist. + ```bash node --env-file=.env --env-file=.development.env index.js ``` @@ -840,6 +842,9 @@ Export keyword before a key is ignored: export USERNAME="nodejs" # will result in `nodejs` as the value. ``` +If you want to load environment variables from a file that may not exist, you +can use the [`--env-file-if-exists`][] flag instead. + ### `-e`, `--eval "script"` + +Behavior is the same as [`--env-file`][], but an error is not thrown if the file +does not exist. + ### `--pending-deprecation` +> Stability: 1.1 - Active development + Behavior is the same as [`--env-file`][], but an error is not thrown if the file does not exist.