Skip to content

Commit f8a97a7

Browse files
arxngrmartenrichter
authored andcommitted
src: return error --env-file if file is not found
PR-URL: nodejs#50588 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent ad7ccc4 commit f8a97a7

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/node.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,9 @@ static ExitCode InitializeNodeWithArgsInternal(
866866

867867
for (const auto& file_path : file_paths) {
868868
std::string path = cwd + kPathSeparator + file_path;
869-
per_process::dotenv_file.ParsePath(path);
869+
auto path_exists = per_process::dotenv_file.ParsePath(path);
870+
871+
if (!path_exists) errors->push_back(file_path + ": not found");
870872
}
871873

872874
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);

src/node_dotenv.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ void Dotenv::SetEnvironment(node::Environment* env) {
6464
}
6565
}
6666

67-
void Dotenv::ParsePath(const std::string_view path) {
67+
bool Dotenv::ParsePath(const std::string_view path) {
6868
uv_fs_t req;
6969
auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
7070

7171
uv_file file = uv_fs_open(nullptr, &req, path.data(), 0, 438, nullptr);
7272
if (req.result < 0) {
7373
// req will be cleaned up by scope leave.
74-
return;
74+
return false;
7575
}
7676
uv_fs_req_cleanup(&req);
7777

@@ -89,7 +89,7 @@ void Dotenv::ParsePath(const std::string_view path) {
8989
auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr);
9090
if (req.result < 0) {
9191
// req will be cleaned up by scope leave.
92-
return;
92+
return false;
9393
}
9494
uv_fs_req_cleanup(&req);
9595
if (r <= 0) {
@@ -104,6 +104,7 @@ void Dotenv::ParsePath(const std::string_view path) {
104104
for (const auto& line : lines) {
105105
ParseLine(line);
106106
}
107+
return true;
107108
}
108109

109110
void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {

src/node_dotenv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Dotenv {
1818
Dotenv& operator=(const Dotenv& d) = default;
1919
~Dotenv() = default;
2020

21-
void ParsePath(const std::string_view path);
21+
bool ParsePath(const std::string_view path);
2222
void AssignNodeOptionsIfAvailable(std::string* node_options);
2323
void SetEnvironment(Environment* env);
2424

test/parallel/test-dotenv-edge-cases.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('.env supports edge cases', () => {
3434
[ '--env-file=.env', '--eval', code ],
3535
{ cwd: __dirname },
3636
);
37-
assert.strictEqual(child.stderr, '');
38-
assert.strictEqual(child.code, 0);
37+
assert.notStrictEqual(child.stderr.toString(), '');
38+
assert.strictEqual(child.code, 9);
3939
});
4040

4141
it('should not override existing environment variables but introduce new vars', async () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy