From 515b9086908cde0c037bffac265c14bcb401391f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 25 Nov 2022 17:50:44 +0100 Subject: [PATCH] Fix decoding virtual paths that are at the root of the tree --- src/libexpr/paths.cc | 5 ++--- tests/toString-path.sh | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libexpr/paths.cc b/src/libexpr/paths.cc index 56e22a7f6..e6acd206d 100644 --- a/src/libexpr/paths.cc +++ b/src/libexpr/paths.cc @@ -40,9 +40,8 @@ SourcePath EvalState::decodePath(std::string_view s, PosIdx pos) try { auto slash = s.find('/'); - if (slash == std::string::npos) fail(); - size_t number = std::stoi(std::string(s, 0, slash), nullptr, 16); - s = s.substr(slash); + size_t number = std::stoi(std::string(s.substr(0, slash)), nullptr, 16); + s = slash == s.npos ? "" : s.substr(slash); auto accessor = inputAccessors.find(number); if (accessor == inputAccessors.end()) fail(); diff --git a/tests/toString-path.sh b/tests/toString-path.sh index 607e596f8..c17dc262e 100644 --- a/tests/toString-path.sh +++ b/tests/toString-path.sh @@ -4,3 +4,5 @@ mkdir -p $TEST_ROOT/foo echo bla > $TEST_ROOT/foo/bar [[ $(nix eval --raw --impure --expr "builtins.readFile (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; } + \"bar\"))") = bla ]] + +[[ $(nix eval --json --impure --expr "builtins.readDir (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; }))") = '{"bar":"regular"}' ]]