From 0d3aa725cf888e3d04319e58b58bcddfe7879066 Mon Sep 17 00:00:00 2001 From: Ardi Nugraha <33378542+ardi-nugraha@users.noreply.github.com> Date: Tue, 14 Nov 2023 11:05:57 +0700 Subject: [PATCH] src: return error --env-file if file is not found PR-URL: https://github.com/nodejs/node/pull/50588 Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum --- src/node.cc | 4 +++- src/node_dotenv.cc | 7 ++++--- src/node_dotenv.h | 2 +- test/parallel/test-dotenv-edge-cases.js | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/node.cc b/src/node.cc index a9336a14304fc6..524f80ee69ee52 100644 --- a/src/node.cc +++ b/src/node.cc @@ -866,7 +866,9 @@ static ExitCode InitializeNodeWithArgsInternal( for (const auto& file_path : file_paths) { std::string path = cwd + kPathSeparator + file_path; - per_process::dotenv_file.ParsePath(path); + auto path_exists = per_process::dotenv_file.ParsePath(path); + + if (!path_exists) errors->push_back(file_path + ": not found"); } per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options); diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 0633ef51269959..992633c50b9a14 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -64,14 +64,14 @@ void Dotenv::SetEnvironment(node::Environment* env) { } } -void Dotenv::ParsePath(const std::string_view path) { +bool Dotenv::ParsePath(const std::string_view path) { uv_fs_t req; auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); uv_file file = uv_fs_open(nullptr, &req, path.data(), 0, 438, nullptr); if (req.result < 0) { // req will be cleaned up by scope leave. - return; + return false; } uv_fs_req_cleanup(&req); @@ -89,7 +89,7 @@ void Dotenv::ParsePath(const std::string_view path) { auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr); if (req.result < 0) { // req will be cleaned up by scope leave. - return; + return false; } uv_fs_req_cleanup(&req); if (r <= 0) { @@ -104,6 +104,7 @@ void Dotenv::ParsePath(const std::string_view path) { for (const auto& line : lines) { ParseLine(line); } + return true; } void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) { diff --git a/src/node_dotenv.h b/src/node_dotenv.h index ee74f9ff84a353..cc87008d149f43 100644 --- a/src/node_dotenv.h +++ b/src/node_dotenv.h @@ -18,7 +18,7 @@ class Dotenv { Dotenv& operator=(const Dotenv& d) = default; ~Dotenv() = default; - void ParsePath(const std::string_view path); + bool ParsePath(const std::string_view path); void AssignNodeOptionsIfAvailable(std::string* node_options); void SetEnvironment(Environment* env); diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index ae2b3dc2a35f35..9735a89618172c 100644 --- a/test/parallel/test-dotenv-edge-cases.js +++ b/test/parallel/test-dotenv-edge-cases.js @@ -34,8 +34,8 @@ describe('.env supports edge cases', () => { [ '--env-file=.env', '--eval', code ], { cwd: __dirname }, ); - assert.strictEqual(child.stderr, ''); - assert.strictEqual(child.code, 0); + assert.notStrictEqual(child.stderr.toString(), ''); + assert.strictEqual(child.code, 9); }); it('should not override existing environment variables but introduce new vars', async () => {