Skip to content

Commit

Permalink
src: allow absolute paths for --env-file
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored and Yagiz Nizipli committed Aug 21, 2023
1 parent 484ad83 commit 127d263
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>

#include <string>
#include <tuple>
Expand Down Expand Up @@ -844,10 +845,15 @@ static ExitCode InitializeNodeWithArgsInternal(
auto file_path = node::Dotenv::GetPathFromArgs(*argv);

if (file_path.has_value()) {
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
std::string path = cwd + kPathSeparator + file_path.value();
CHECK(!per_process::v8_initialized);
per_process::dotenv_file.ParsePath(path);

if (file_path->is_absolute()) {
per_process::dotenv_file.ParsePath(file_path->string());
} else {
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
std::string path = cwd + kPathSeparator + file_path->string();
per_process::dotenv_file.ParsePath(path);
}
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace node {
using v8::NewStringType;
using v8::String;

std::optional<std::string> Dotenv::GetPathFromArgs(
std::optional<std::filesystem::path> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
std::string_view flag = "--env-file";
// Match the last `--env-file`
Expand All @@ -26,7 +26,7 @@ std::optional<std::string> Dotenv::GetPathFromArgs(
auto equal_char = path->find('=');

if (equal_char != std::string::npos) {
return path->substr(equal_char + 1);
return std::filesystem::path(path->substr(equal_char + 1));
}

auto next_arg = std::prev(path);
Expand All @@ -35,7 +35,7 @@ std::optional<std::string> Dotenv::GetPathFromArgs(
return std::nullopt;
}

return *next_arg;
return std::filesystem::path(*next_arg);
}

void Dotenv::SetEnvironment(node::Environment* env) {
Expand Down
3 changes: 2 additions & 1 deletion src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "util-inl.h"

#include <filesystem>
#include <map>
#include <optional>

Expand All @@ -23,7 +24,7 @@ class Dotenv {
void AssignNodeOptionsIfAvailable(std::string* node_options);
void SetEnvironment(Environment* env);

static std::optional<std::string> GetPathFromArgs(
static std::optional<std::filesystem::path> GetPathFromArgs(
const std::vector<std::string>& args);

private:
Expand Down
20 changes: 18 additions & 2 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

const common = require('../common');
const assert = require('node:assert');
const path = require('node:path');
const fixtures = require('../common/fixtures');
const { describe, it } = require('node:test');

const validEnvFilePath = '../fixtures/dotenv/valid.env';
const relativePath = '../fixtures/dotenv/node-options.env';
const validEnvFilePath = path.relative(__dirname, fixtures.path('dotenv/valid.env'));
const absolutePath = fixtures.path('dotenv/node-options.env');
const relativePath = path.relative(__dirname, absolutePath);

describe('.env supports edge cases', () => {

Expand Down Expand Up @@ -35,4 +38,17 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0);
});

it('should support absolute paths', async () => {
const code = `
require('assert').strictEqual(process.env.CUSTOM_VARIABLE, 'hello-world');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ '--env-file', absolutePath, '--eval', code ],
{ cwd: __dirname },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

});

0 comments on commit 127d263

Please sign in to comment.