Skip to content

Commit 430d035

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

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/node.cc

Lines changed: 10 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,16 @@ 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+
auto filesystem_path = std::filesystem::path(*file_path);
850+
851+
if (filesystem_path.is_absolute()) {
852+
per_process::dotenv_file.ParsePath(*file_path);
853+
} else {
854+
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
855+
std::string path = cwd + kPathSeparator + file_path.value();
856+
per_process::dotenv_file.ParsePath(path);
857+
}
851858
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
852859
}
853860

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)