diff --git a/lix/libutil/args.cc b/lix/libutil/args.cc index d8a88f003..bcd142607 100644 --- a/lix/libutil/args.cc +++ b/lix/libutil/args.cc @@ -87,7 +87,8 @@ void RootArgs::parseCmdline(const Strings & _cmdline) if (auto s = getEnv("NIX_GET_COMPLETIONS")) { size_t n = std::stoi(*s); - assert(n > 0 && n <= cmdline.size()); + if (!(n > 0 && n <= cmdline.size())) + throw UsageError("Invalid word number to get completion for: %zu\n. Your autocompletions might be misconfigured", n); *std::next(cmdline.begin(), n - 1) += completionMarker; completions = std::make_shared(); verbosity = lvlError; diff --git a/tests/functional/completions.sh b/tests/functional/completions.sh index d3d5bbd48..610c15529 100644 --- a/tests/functional/completions.sh +++ b/tests/functional/completions.sh @@ -37,6 +37,12 @@ EOF [[ "$(NIX_GET_COMPLETIONS=1 nix buil)" == $'normal\nbuild\t' ]] [[ "$(NIX_GET_COMPLETIONS=2 nix flake metad)" == $'normal\nmetadata\t' ]] +# Test how completion fails if the number is not a valid index for the number of arguments +NIX_GET_COMPLETIONS=0 expectStderr 1 nix \ + | grepQuiet "error: Invalid word number to get completion for:" +NIX_GET_COMPLETIONS=4 expectStderr 1 nix build a \ + | grepQuiet "error: Invalid word number to get completion for:" + # Filename completion [[ "$(NIX_GET_COMPLETIONS=2 nix build ./f)" == $'filenames\n./foo\t' ]] [[ "$(NIX_GET_COMPLETIONS=2 nix build ./nonexistent)" == $'filenames' ]]