Skip to content
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

zsh autocompletion not replacing tabs correctly #105587

Closed
fandahao17 opened this issue Oct 9, 2021 · 8 comments · Fixed by #112584
Closed

zsh autocompletion not replacing tabs correctly #105587

fandahao17 opened this issue Oct 9, 2021 · 8 comments · Fixed by #112584
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. sig/cli Categorizes an issue or PR as relevant to SIG CLI.

Comments

@fandahao17
Copy link

fandahao17 commented Oct 9, 2021

What happened?

While using zsh autocompletion:

[fandahao@localhost]~% kubectl ge\t

No completion result is given.

When I turn on $BASH_COMP_DEBUG_FILE, I see the following logs:

========= starting completion logic ==========
CURRENT: 2, words[*]: kubectl ge
Truncated words[*]: kubectl ge,
lastParam: ge, lastChar: e
About to call: eval kubectl __complete ge
completion output: get	Display one or many resources
:4
last line: :4
directive: 4
completions: get	Display one or many resources
flagPrefix: 
Adding completion: :g:e:t:	:D:i:s:p:l:a:y: :o:n:e: :o:r: :m:a:n:y: :r:e:s:o:u:r:c:e:s

I noticed that the last line seems to give weird results. After printing out what source <(kubectl completion zsh) does, I think the bug is caused by the following lines:

            # If requested, completions are returned with a description.
            # The description is preceded by a TAB character.
            # For zsh's _describe, we need to use a : instead of a TAB.
            # We first need to escape any : as part of the completion itself.
            comp=${comp//:/\\:}

            local tab=$(printf '\t')
            comp=${comp//$tab/:}

The last line is supposed to replace all \t's in the completion result with :, but it actually replaced all empty strings with a :.

Replacing the last two lines with the following code seems to solve the problem:

comp=${comp//$(printf '\t')/:}

Honestly, I don't know why this works correctly while the original code does not. I guess tabs may need to be treated differently when doing variable expansions?

What did you expect to happen?

After modifying the code as in last section, I got the following logs. I think this is the correct result.

========= starting completion logic ==========
CURRENT: 2, words[*]: kubectl ge
Truncated words[*]: kubectl ge,
lastParam: ge, lastChar: e
About to call: eval kubectl __complete ge
completion output: get	Display one or many resources
:4
last line: :4
directive: 4
completions: get	Display one or many resources
flagPrefix: 
tab is ''
Adding completion: get:Display one or many resources

How can we reproduce it (as minimally and precisely as possible)?

[fandahao@localhost]~% zsh --version
zsh 5.0.2 (x86_64-redhat-linux-gnu)

A minimal .zshrc:

source <(kubectl completion zsh)

Anything else we need to know?

No response

Kubernetes version

Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T18:03:20Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T17:57:25Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}

Cloud provider

This bug happens on my own machine.

OS version

[fandahao@localhost]~% cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[fandahao@localhost]~% uname -a
Linux localhost.localdomain 5.4.146-1.el7.elrepo.x86_64 #1 SMP Tue Sep 14 08:38:01 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux

Install tools

Container runtime (CRI) and and version (if applicable)

Related plugins (CNI, CSI, ...) and versions (if applicable)

@fandahao17 fandahao17 added the kind/bug Categorizes issue or PR as related to a bug. label Oct 9, 2021
@k8s-ci-robot k8s-ci-robot added needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 9, 2021
@k8s-ci-robot
Copy link
Contributor

@fandahao17: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@fandahao17
Copy link
Author

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 9, 2021
@brianpursley
Copy link
Member

This appears to be related to which zsh version you are using.

At first, I was unable to reproduce the problem, but then realized I have zsh 5.8.

So I downloaded and built zsh 5.0.2 to match the version @fandahao17 reported using and am able to reproduce the problem.

@fandahao17 Are you able to upgrade to a newer version of zsh? 5.8 worked for me, but I don't know if an earlier version might also work.

We should probably figure out what the minimum required version is for zsh completions to work and document it.

@marckhouzam
Copy link
Member

I am also able to reproduce this using Zsh 5.0.x.
Based on @brianpursley realizing this has to do with the version of zsh, I was able to find that the problem disappears with zsh 5.1 (testing was made much easier thanks to https://github.com/zsh-users/zsh-docker)

I can also confirm that the suggested fix does seem to work.

@fandahao17 The fix you found is simple enough that it may be worth opening an issue with the https://github.com/spf13/cobra project which generates the completion scripts to try to get it in.

@eddiezane
Copy link
Member

Please open over on the cobra repo if you'd like to get this fixed there.

/close

@k8s-ci-robot
Copy link
Contributor

@eddiezane: Closing this issue.

In response to this:

Please open over on the cobra repo if you'd like to get this fixed there.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@marckhouzam
Copy link
Member

A fix for this issue will be part of the next Cobra release: spf13/cobra#1665

@brneto
Copy link
Contributor

brneto commented Sep 10, 2022

I've created the PR #112362 to fix this issue, but for those still facing this issue after installing Oh My Zsh with kubectl plugin enabled you can run the command below as a patch to fix it in the meantime:

sed -i '6i\    sed -i '\''s/tab=$(printf \x27\\t\x27)/tab="$(printf \x27\\t\x27)"/'\'' "$ZSH_CACHE_DIR/completions/_kubectl"\' ~/.oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh && \
sed -i '9i\    sed -i '\''s/tab=$(printf \x27\\t\x27)/tab="$(printf \x27\\t\x27)"/'\'' "$ZSH_CACHE_DIR/completions/_kubectl"\' ~/.oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh && \
sed -i '12i\    sed -i '\''s/tab=$(printf \x27\\t\x27)/tab="$(printf \x27\\t\x27)"/'\'' "$ZSH_CACHE_DIR/completions/_kubectl"\' ~/.oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh && \
exec zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
6 participants