Skip to content

Commit 4b5ba8f

Browse files
authored
Load environment file from dotenv-path relative to working directory (#2152)
1 parent 1ce7a05 commit 4b5ba8f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/load_dotenv.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ pub(crate) fn load_dotenv(
2424
}
2525

2626
if let Some(path) = dotenv_path {
27+
let path = working_directory.join(path);
2728
if path.is_file() {
28-
return load_from_file(&working_directory.join(path));
29+
return load_from_file(&path);
2930
}
3031
}
3132

tests/dotenv.rs

+19
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ fn no_dotenv() {
360360
.stderr("echo DEFAULT\n")
361361
.run();
362362
}
363+
363364
#[test]
364365
fn dotenv_env_var_override() {
365366
Test::new()
@@ -375,3 +376,21 @@ fn dotenv_env_var_override() {
375376
.stderr("echo $DOTENV_KEY\n")
376377
.run();
377378
}
379+
380+
#[test]
381+
fn dotenv_path_usable_from_subdir() {
382+
Test::new()
383+
.justfile(
384+
"
385+
set dotenv-path := '.custom-env'
386+
387+
@echo:
388+
echo $DOTENV_KEY
389+
",
390+
)
391+
.create_dir("sub")
392+
.current_dir("sub")
393+
.write(".custom-env", "DOTENV_KEY=dotenv-value")
394+
.stdout("dotenv-value\n")
395+
.run();
396+
}

tests/test.rs

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ impl Test {
9494
self
9595
}
9696

97+
pub(crate) fn create_dir(self, path: impl AsRef<Path>) -> Self {
98+
fs::create_dir_all(self.tempdir.path().join(path.as_ref())).unwrap();
99+
self
100+
}
101+
97102
pub(crate) fn current_dir(mut self, path: impl AsRef<Path>) -> Self {
98103
path.as_ref().clone_into(&mut self.current_dir);
99104
self

0 commit comments

Comments
 (0)