diff --git a/src/function.rs b/src/function.rs index 2e31e196e8..e0fc2c77ad 100644 --- a/src/function.rs +++ b/src/function.rs @@ -496,10 +496,16 @@ fn os_family(_context: Context) -> FunctionResult { } fn parent_directory(_context: Context, path: &str) -> FunctionResult { - Utf8Path::new(path) + let parent = Utf8Path::new(path) .parent() .map(Utf8Path::to_string) - .ok_or_else(|| format!("Could not extract parent directory from `{path}`")) + .ok_or_else(|| format!("Could not extract parent directory from `{path}`"))?; + + if parent.is_empty() { + Ok(".".into()) + } else { + Ok(parent) + } } fn path_exists(context: Context, path: &str) -> FunctionResult { diff --git a/tests/functions.rs b/tests/functions.rs index a9eba0b980..4d3c80c8a3 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -325,6 +325,19 @@ fn broken_directory_function2() { .failure(); } +#[test] +fn parent_directory_of_bare_filename() { + Test::new() + .justfile( + " + foo: + @echo {{parent_directory('foo')}} + ", + ) + .stdout(".\n") + .success(); +} + #[test] fn env_var_functions_windows() { if cfg!(not(windows)) {