Skip to content

Commit f2365e5

Browse files
author
0xArdi-N
committed
src: return error --env-file if file is not found
1 parent 33704c4 commit f2365e5

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/node.cc

+3-1
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

+4-3
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

+1-1
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

+2-2
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.strictEqual(child.stderr.toString().includes('node: .env: not found\n'), true);
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)