Skip to content

Commit 127d263

Browse files
anonrigYagiz Nizipli
authored andcommitted
src: allow absolute paths for --env-file
1 parent 484ad83 commit 127d263

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

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

7-
const validEnvFilePath = '../fixtures/dotenv/valid.env';
8-
const relativePath = '../fixtures/dotenv/node-options.env';
9+
const validEnvFilePath = path.relative(__dirname, fixtures.path('dotenv/valid.env'));
10+
const absolutePath = fixtures.path('dotenv/node-options.env');
11+
const relativePath = path.relative(__dirname, absolutePath);
912

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

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

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

0 commit comments

Comments
 (0)