Skip to content

fish completions: fix double-evaluation of commandline #2095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

krobelus
Copy link
Contributor

@krobelus krobelus commented Jan 6, 2024

We capture the commandline tokens using fish's "commandline --tokenize" (-o).
That function turns

echo 'some argument $(123)'

into two arguments while removing the quotes

echo
some argument $(123)

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.

The downside of this change is that things like "$HOME" or "~" will
no longer be escaped. Changing this requires changes in fish, which
I'm working on.

Reproduce the issue by pasting the completion script at
fish-shell/fish-shell#10194 (comment)
to a file "grafana-manager.fish" and running inside fish

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)" <TAB>

Fixes fish-shell/fish-shell#10194

@CLAassistant
Copy link

CLAassistant commented Jan 6, 2024

CLA assistant check
All committers have signed the CLA.

@@ -45,7 +45,7 @@ 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 (commandline -opc | string escape)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @marckhouzam

FWIW the blame for this mistake is on me, I made the suggestion to use it

Copy link

@stellarblaze stellarblaze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you fixing this.

@stellarblaze
Copy link

@krobelus, do you know what needs to be done for this PR to be merged and included in the next release?

@stellarblaze
Copy link

@krobelus
Copy link
Contributor Author

The downside of this change is that things like "$HOME" or "~" will no
longer be escaped.

The API to avoid this downside while still fixing the issue at hand has been merged in fish-shell/fish-shell#10212, which will be in the upcoming fish release.
It's fairly straightforward to upgrade to the new API, I can roll a patch by tomorrow.

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.

The upcoming fish 3.8 has a new flag, "commandline -x"
which will expand variables like $HOME properly, see
fish-shell/fish-shell#10212 Use that if
available.

Reproduce the issue by pasting the completion script at
fish-shell/fish-shell#10194 (comment)
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)" <TAB>

Fixes fish-shell/fish-shell#10194
@krobelus krobelus force-pushed the fix-fish-completions branch from eabe4e5 to 4e07448 Compare April 26, 2024 16:33
@ichekrygin
Copy link

Circling back on this issue - do you think this can be merged?

@ccoVeille
Copy link
Contributor

Thanks for resurrecting this old PR.

I'm a bit lost, not only because I'm new to the project.

I tried to read the issue, the discussion that happened on it. Then this PR and what was told here too. Then I looked at the fish issue.

From my understanding, and I could be totally wrong:

  • there was a bug in fish
  • the bug in fish was causing a bug with cobra when using fish
  • this PR was opened to fix the bug in cobra when using fish.

For now, I think I'm good.

But then, from what I can see:

  • the bug in fish is now fixed
  • this PR is still not merged

Here is where I am.

So now, let me ask:

  • is there still a bug with cobra when using fish, now the bug in fish is now fixed ?
  • is there still a need for this PR ?

@ichekrygin
Copy link

ichekrygin commented Apr 25, 2025

Hello @ccoVeille , thank you for looking into this.

I understand that we still need this PR to address the issue of fish shell completions. The fix in Fish shell was necessary to avoid the undesirable side effects that this PR would introduce if merged as-is. As noted in the PR description:

"The downside of this change is that things like $HOME or ~ will no longer be escaped. Changing this requires changes in fish, which I'm working on."

Now that the upstream fix has been merged in Fish, this PR can be updated to take advantage of the new API—avoiding the side effects while still resolving the original problem.

@ccoVeille
Copy link
Contributor

OK thanks @ichekrygin.

It's way clearer now.

I might be wrong but based on your comment, I understand the PR shouldn't be merged like this, and requires more work.

@krobelus
Copy link
Contributor Author

I can update it but we'll also need someone with commit access

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants