lib.filesystem.pathType: Improve error for non-existent paths
Previously it would fail with
error: attribute 'nonexistent' missing
at nixpkgs/lib/filesystem.nix:29:10:
28| if dirOf path == path then "directory"
29| else (readDir (dirOf path)).${baseNameOf path};
| ^
30|
This commit is contained in:
@@ -22,10 +22,14 @@ in
|
||||
Returns the type of a path: regular (for file), symlink, or directory.
|
||||
*/
|
||||
pathType = path:
|
||||
if ! pathExists path
|
||||
# Fail irrecoverably to mimic the historic behavior of this function and
|
||||
# the new builtins.readFileType
|
||||
then abort "lib.filesystem.pathType: Path ${toString path} does not exist."
|
||||
# The filesystem root is the only path where `dirOf / == /` and
|
||||
# `baseNameOf /` is not valid. We can detect this and directly return
|
||||
# "directory", since we know the filesystem root can't be anything else.
|
||||
if dirOf path == path
|
||||
else if dirOf path == path
|
||||
then "directory"
|
||||
else (readDir (dirOf path)).${baseNameOf path};
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ checkPathType "$PWD/directory" '"directory"'
|
||||
checkPathType "$PWD/regular" '"regular"'
|
||||
checkPathType "$PWD/symlink" '"symlink"'
|
||||
checkPathType "$PWD/fifo" '"unknown"'
|
||||
checkPathType "$PWD/non-existent" "error: evaluation aborted with the following error message: 'lib.filesystem.pathType: Path $PWD/non-existent does not exist.'"
|
||||
|
||||
checkPathIsDirectory() {
|
||||
local path=$1
|
||||
|
||||
Reference in New Issue
Block a user