From 3740cdb654f3320c34954bf13d915708fed09553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Fri, 1 Mar 2024 16:51:41 +0100 Subject: [PATCH] Fix the parsing of path flake refs with a query `/path?foo=bar` was parsed as `/path`, omitting the query altogether (`/path?foo=bar#` was parsed correctly though). Rewrite the path flake ref parser to fix this --- src/libexpr/flake/flakeref.cc | 28 +++++++++++++++++----------- tests/functional/flakes/inputs.sh | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/libexpr/flake/flakeref.cc b/src/libexpr/flake/flakeref.cc index 4fff0f905..a43356cba 100644 --- a/src/libexpr/flake/flakeref.cc +++ b/src/libexpr/flake/flakeref.cc @@ -78,19 +78,25 @@ std::pair parsePathFlakeRefWithFragment( std::string path = url; std::string fragment = ""; std::map query; - auto pathEnd = url.find_first_of("#?"); - auto fragmentStart = pathEnd; - if (pathEnd != std::string::npos && url[pathEnd] == '?') { - fragmentStart = url.find("#"); - } + auto pathEnd = url.find_first_of("?#"); if (pathEnd != std::string::npos) { + // There's something (either a query string or a fragment) in addition + // to the path path = url.substr(0, pathEnd); - } - if (fragmentStart != std::string::npos) { - fragment = percentDecode(url.substr(fragmentStart+1)); - } - if (pathEnd != std::string::npos && fragmentStart != std::string::npos) { - query = decodeQuery(url.substr(pathEnd+1, fragmentStart-pathEnd-1)); + std::string non_path_part = url.substr(pathEnd + 1); + if (url[pathEnd] == '#') { + // Not query, just a fragment + fragment = percentDecode(non_path_part); + } else { + // We have a query, and maybe a fragment too + auto fragmentStart = non_path_part.find("#"); + if (fragmentStart != std::string::npos) { + query = decodeQuery(non_path_part.substr(0, fragmentStart)); + fragment = percentDecode(non_path_part.substr(fragmentStart+1)); + } else { + query = decodeQuery(non_path_part); + } + } } if (baseDir) { diff --git a/tests/functional/flakes/inputs.sh b/tests/functional/flakes/inputs.sh index 80620488a..f9324e8f1 100644 --- a/tests/functional/flakes/inputs.sh +++ b/tests/functional/flakes/inputs.sh @@ -24,7 +24,7 @@ test_subdir_self_path() { } EOF ( - nix build $baseDir?dir=b-low --no-link + nix build $baseDir/b-low --no-link ) } test_subdir_self_path