summary refs log tree commit diff
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-04-05 16:47:30 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-05-22 14:13:57 +0200
commitbb6eab0bdbe060cb578fc9be5925a76a1635424f (patch)
tree4cf21d6c37591521c4ae509f5640aa1583b9fc0e
parent5346636c20ff57410b25a6a8897d4e95f75e040c (diff)
downloadnixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar.gz
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar.bz2
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar.lz
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar.xz
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.tar.zst
nixpkgs-bb6eab0bdbe060cb578fc9be5925a76a1635424f.zip
lib.filesystem.pathType: Fix for filesystem root argument
Previously this function couldn't handle / being passed, it would throw
an error:

error: attribute '' missing

       at nixpkgs/lib/filesystem.nix:24:20:

           23|   */
           24|   pathType = path: (readDir (dirOf path)).${baseNameOf path};
             |                    ^
           25|

Consequently this also fixes the
lib.filesystem.{pathIsDirectory,pathIsRegularFile} functions.
-rw-r--r--lib/filesystem.nix7
-rwxr-xr-xlib/tests/filesystem.sh3
2 files changed, 9 insertions, 1 deletions
diff --git a/lib/filesystem.nix b/lib/filesystem.nix
index f604eb90434..13f4ebb2640 100644
--- a/lib/filesystem.nix
+++ b/lib/filesystem.nix
@@ -22,7 +22,12 @@ in
     Returns the type of a path: regular (for file), symlink, or directory.
   */
   pathType = path:
-    (readDir (dirOf path)).${baseNameOf path};
+    # 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
+    then "directory"
+    else (readDir (dirOf path)).${baseNameOf path};
 
   /*
     Returns true if the path exists and is a directory, false otherwise.
diff --git a/lib/tests/filesystem.sh b/lib/tests/filesystem.sh
index ab1504abb49..61710da92ba 100755
--- a/lib/tests/filesystem.sh
+++ b/lib/tests/filesystem.sh
@@ -46,6 +46,7 @@ checkPathType() {
     fi
 }
 
+checkPathType "/" '"directory"'
 checkPathType "$PWD/directory" '"directory"'
 checkPathType "$PWD/regular" '"regular"'
 checkPathType "$PWD/symlink" '"symlink"'
@@ -62,6 +63,7 @@ checkPathIsDirectory() {
     fi
 }
 
+checkPathIsDirectory "/" "true"
 checkPathIsDirectory "$PWD/directory" "true"
 checkPathIsDirectory "$PWD/regular" "false"
 checkPathIsDirectory "$PWD/symlink" "false"
@@ -79,6 +81,7 @@ checkPathIsRegularFile() {
     fi
 }
 
+checkPathIsRegularFile "/" "false"
 checkPathIsRegularFile "$PWD/directory" "false"
 checkPathIsRegularFile "$PWD/regular" "true"
 checkPathIsRegularFile "$PWD/symlink" "false"