From 0e2602dea1ab2ad24b6a20ae0c9b1894bef21c97 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 6 Jan 2024 07:43:23 +0100 Subject: [PATCH] fish completions: fix double-evaluation of commandline We capture the commandline tokens using fish's "commandline --tokenize" (-o). Given a commandline of some-cobra-command 'some argument $(123)' "$HOME" into three arguments while removing the quotes some-cobra-command some argument $(123) $HOME Later we pass "some argument $(123)" without quotes to the shell's "eval". This is wrong and causes spurious evaluation of the parenthesis as command substitution. Fix this by escaping the arguments. To avoid regressing the expansions that did work (variable expansion), only do this if we have "commandline -x" (part of fish 4) which expands tokens. Reproduce the issue by pasting the completion script at https://github.com/fish-shell/fish-shell/issues/10194#issuecomment-1879563545 to a file "grafana-manager.fish" and running function grafana-manager; end source grafana-manager.fish Then type (without pressing Enter) grafana-manager public-dashboards delete --organization-id 3 --dashboard-name "k8s (public)" Fixes https://github.com/fish-shell/fish-shell/issues/10194 --- fish_completions.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fish_completions.go b/fish_completions.go index 12d61b69..f337dcdf 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -45,7 +45,13 @@ function __%[1]s_perform_completion __%[1]s_debug "Starting __%[1]s_perform_completion" # Extract all args except the last one - set -l args (commandline -opc) + set -l args ( + if commandline -x >/dev/null 2>&1 + commandline -xpc | string escape + else + commandline -opc + end + ) # Extract the last arg and escape it in case it is a space set -l lastArg (string escape -- (commandline -ct))