File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 124
124
#include < cstdio>
125
125
#include < cstdlib>
126
126
#include < cstring>
127
+ #include < filesystem>
127
128
128
129
#include < string>
129
130
#include < tuple>
@@ -844,10 +845,16 @@ static ExitCode InitializeNodeWithArgsInternal(
844
845
auto file_path = node::Dotenv::GetPathFromArgs (*argv);
845
846
846
847
if (file_path.has_value ()) {
847
- auto cwd = Environment::GetCwd (Environment::GetExecPath (*argv));
848
- std::string path = cwd + kPathSeparator + file_path.value ();
849
848
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
+ }
851
858
per_process::dotenv_file.AssignNodeOptionsIfAvailable (&node_options);
852
859
}
853
860
Original file line number Diff line number Diff line change 2
2
3
3
const common = require ( '../common' ) ;
4
4
const assert = require ( 'node:assert' ) ;
5
+ const path = require ( 'node:path' ) ;
5
6
const { describe, it } = require ( 'node:test' ) ;
6
7
7
8
const validEnvFilePath = '../fixtures/dotenv/valid.env' ;
8
9
const relativePath = '../fixtures/dotenv/node-options.env' ;
10
+ const absolutePath = path . join ( __dirname , relativePath ) ;
9
11
10
12
describe ( '.env supports edge cases' , ( ) => {
11
13
@@ -35,4 +37,17 @@ describe('.env supports edge cases', () => {
35
37
assert . strictEqual ( child . code , 0 ) ;
36
38
} ) ;
37
39
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
+
38
53
} ) ;
You can’t perform that action at this time.
0 commit comments