summary refs log tree commit diff
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-04-05 16:58:29 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-05-22 14:13:57 +0200
commitd064d972f07b908db3efb909d6663bee3adf8d40 (patch)
tree3ba519508fcd3f5e27ea95d567cbb870cc554d3b
parentbb6eab0bdbe060cb578fc9be5925a76a1635424f (diff)
downloadnixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar.gz
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar.bz2
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar.lz
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar.xz
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.tar.zst
nixpkgs-d064d972f07b908db3efb909d6663bee3adf8d40.zip
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|
-rw-r--r--lib/filesystem.nix6
-rwxr-xr-xlib/tests/filesystem.sh1
2 files changed, 6 insertions, 1 deletions
diff --git a/lib/filesystem.nix b/lib/filesystem.nix
index 13f4ebb2640..db120a48c64 100644
--- a/lib/filesystem.nix
+++ b/lib/filesystem.nix
@@ -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};
 
diff --git a/lib/tests/filesystem.sh b/lib/tests/filesystem.sh
index 61710da92ba..4a5ffeb1243 100755
--- a/lib/tests/filesystem.sh
+++ b/lib/tests/filesystem.sh
@@ -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