Skip to content

Commit

Permalink
Fix the parsing of path flake refs with a query
Browse files Browse the repository at this point in the history
`/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
  • Loading branch information
Théophane Hufschmitt committed Mar 2, 2024
1 parent fbf3f93 commit 3740cdb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/libexpr/flake/flakeref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,25 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
std::string path = url;
std::string fragment = "";
std::map<std::string, std::string> 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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/flakes/inputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3740cdb

Please sign in to comment.