Skip to content

Commit b42914a

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

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/node.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,10 @@ 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 file_is_exist = per_process::dotenv_file.ParsePath(path);
870+
871+
if (!file_is_exist)
872+
errors->push_back(std::string(file_path) + ": not found");
870873
}
871874

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

src/node_dotenv.cc

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

@@ -87,10 +87,6 @@ void Dotenv::ParsePath(const std::string_view path) {
8787

8888
while (true) {
8989
auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr);
90-
if (req.result < 0) {
91-
// req will be cleaned up by scope leave.
92-
return;
93-
}
9490
uv_fs_req_cleanup(&req);
9591
if (r <= 0) {
9692
break;
@@ -104,6 +100,7 @@ void Dotenv::ParsePath(const std::string_view path) {
104100
for (const auto& line : lines) {
105101
ParseLine(line);
106102
}
103+
return true;
107104
}
108105

109106
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-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ describe('.env supports edge cases', () => {
3434
[ '--env-file=.env', '--eval', code ],
3535
{ cwd: __dirname },
3636
);
37-
assert.strictEqual(child.stderr, '');
37+
assert.strictEqual(child.stderr.toString().includes('node: .env: not found\n'), true);
38+
assert.strictEqual(child.code, 9);
3839
assert.strictEqual(child.code, 0);
3940
});
4041

0 commit comments

Comments
 (0)