Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: return error --env-file if file is missing #50588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/node.cc
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/node_dotenv.cc
Expand Up @@ -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);

Expand All @@ -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;
ardinugrxha marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
uv_fs_req_cleanup(&req);
if (r <= 0) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/node_dotenv.h
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-dotenv-edge-cases.js
Expand Up @@ -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 () => {
Expand Down