Skip to content

Commit 6b9587b

Browse files
committed
src: allow absolute paths for --env-file
1 parent 3af6585 commit 6b9587b

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/node.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#include <cstdio>
125125
#include <cstdlib>
126126
#include <cstring>
127+
#include <filesystem>
127128

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

846847
if (file_path.has_value()) {
847-
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
848-
std::string path = cwd + kPathSeparator + file_path.value();
849848
CHECK(!per_process::v8_initialized);
850-
per_process::dotenv_file.ParsePath(path);
849+
850+
if (file_path->is_absolute()) {
851+
per_process::dotenv_file.ParsePath(file_path->string());
852+
} else {
853+
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
854+
std::string path = cwd + kPathSeparator + file_path->string();
855+
per_process::dotenv_file.ParsePath(path);
856+
}
851857
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
852858
}
853859

src/node_dotenv.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace node {
88
using v8::NewStringType;
99
using v8::String;
1010

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

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

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

38-
return *next_arg;
38+
return std::filesystem::path(*next_arg);
3939
}
4040

4141
void Dotenv::SetEnvironment(node::Environment* env) {

src/node_dotenv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "util-inl.h"
77

8+
#include <filesystem>
89
#include <map>
910
#include <optional>
1011

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

26-
static std::optional<std::string> GetPathFromArgs(
27+
static std::optional<std::filesystem::path> GetPathFromArgs(
2728
const std::vector<std::string>& args);
2829

2930
private:

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
const common = require('../common');
44
const assert = require('node:assert');
5+
const path = require('node:path');
56
const { describe, it } = require('node:test');
67

78
const validEnvFilePath = '../fixtures/dotenv/valid.env';
89
const relativePath = '../fixtures/dotenv/node-options.env';
10+
const absolutePath = path.join(__dirname, relativePath);
911

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

@@ -35,4 +37,17 @@ describe('.env supports edge cases', () => {
3537
assert.strictEqual(child.code, 0);
3638
});
3739

40+
it('should support absolute paths', async () => {
41+
const code = `
42+
require('assert').strictEqual(process.env.CUSTOM_VARIABLE, 'hello-world');
43+
`.trim();
44+
const child = await common.spawnPromisified(
45+
process.execPath,
46+
[ `--env-file=${absolutePath}`, '--eval', code ],
47+
{ cwd: __dirname },
48+
);
49+
assert.strictEqual(child.stderr, '');
50+
assert.strictEqual(child.code, 0);
51+
});
52+
3853
});

0 commit comments

Comments
 (0)