Skip to content

v2: fix(completion): --flag=val breaks zsh completion - #5680

Merged
bacongobbler merged 5 commits into
helm:masterfrom
VilledeMontreal:fix/zshFlagBug
Sep 26, 2019
Merged

bacongobbler merged 5 commits into
helm:masterfrom
VilledeMontreal:fix/zshFlagBug

Conversation

@marckhouzam

@marckhouzam marckhouzam commented May 7, 2019 •

Copy link
Copy Markdown
Member

What this PR does / why we need it:

zsh completion cannot handle the flag format --flag=val.

Special notes for your reviewer:

This is a bug I ran into when working on Helm completion.
I was surprised that it didn't happen when I was using
kubectl, so I investigated and found a PR that fixed this
bug in kubectl:
kubernetes/kubernetes#48553

I've marked the original author, @superbrothers , as a co-author to this PR.

I duplicated the code in this commit which:

Removes __helm_declare, which is safe to do since
declare -F is already replaced to whence -w by
__helm_convert_bash_to_zsh().

The problem was that calling "declare" from inside a function
scopes the declaration to that function only. So "declare"
should not be called through __helm_declare() but instead
directly.

To reproduce:
1- setup helm completion in zsh
2- helm --kubeconfig=$HOME/.kube/config statu<TAB>

you will get the error:
__helm_handle_flag:27: bad math expression: operand expected at end of string

Co-authored-by: Kazuki Suda [email protected]
Signed-off-by: Marc Khouzam [email protected]

I realized that this bug was also the root of the problem of #5046. With this PR
fixing the issue in the proper way, I've added a commit to remove
the previous fix for #5046 (PR #5072 and #5406).

However, for completely other reasons, we still need to remove the quotes from the aliashash associative array in zsh. I've therefore put it back.

This is a bug I ran into when working on Helm completion.
I was surprised that it didn't happen when I was using
kubectl, so I investigated and found a PR that fixed this
bug in kubectl:
kubernetes/kubernetes#48553

I duplicated the code in this commit which:

Removes __helm_declare, which is safe to do since
`declare -F` is already replaced to `whence -w` by
__helm_convert_bash_to_zsh().

The problem was that calling "declare" from inside a function
scopes the declaration to that function only.  So "declare"
should not be called through __helm_declare() but instead
directly.

To reproduce:
1- setup helm completion in zsh
2- helm --kubeconfig=$HOME/.kube/config statu<TAB>

you will get the error:
__helm_handle_flag:27: bad math expression: operand expected at end of string

Co-authored-by: Kazuki Suda <[email protected]>
Signed-off-by: Marc Khouzam <[email protected]>
@helm-bot helm-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 7, 2019
PR helm#5072 followed by helm#5406 tweaked the handling of the associative
array 'aliashash' when in zsh.  However, upon further investigation
the root of the problem was the 'aliashash' was not being declared
properly as it was declared within a method and therefore not
accessible to the rest of the completion script.

The previous commit of this PR makes the necessary change to properly
declare 'aliashash' which makes the previous tweak unecessary.  This
commit removes the tweak.

Signed-off-by: Marc Khouzam <[email protected]>
@helm-bot helm-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 20, 2019

@eddiezane eddiezane left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. Should be the actual fix. Same thing I did in digitalocean/doctl#403 albeit I didn't quite understand it at the time.

marckhouzam added a commit to VilledeMontreal/helm that referenced this pull request Jun 5, 2019
This reverts commit d2ab3f5.
The reverted commit turned out to be a workaroud another problem.
The real fix is submitted in PR helm#5680
marckhouzam added a commit to VilledeMontreal/helm that referenced this pull request Jun 5, 2019
This reverts commit d2ab3f5.
The reverted commit turned out to be a workaroud another problem.
The real fix is submitted in PR helm#5680

Signed-off-by: Marc Khouzam <[email protected]>
Turns out that removing the quotes from the associate array
'aliashash' is still required, but because it is needed for
completion of alias commands.  For example
  helm dep <TAB>
does not complete as expected (as if it was 'helm dependency')
if the quotes are not removed.

This is because although bash ignores quotes when using
associative arrays, zsh does not.  So when looking for an alias
  aliashash[dep]
will not match the entry
  aliashash["dep"]
in zsh but will for bash.  Therefore, removing the quotes fixes
things.

Signed-off-by: Marc Khouzam <[email protected]>
@helm-bot helm-bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 6, 2019
@marckhouzam

Copy link
Copy Markdown
Member Author

@eddiezane you may be interested in applying a similar commit as 48f0e31 for doctl if you use command aliases.

@marckhouzam

Copy link
Copy Markdown
Member Author

/retest

@hickeyma

hickeyma commented Jun 6, 2019

Copy link
Copy Markdown
Contributor

@marckhouzam Try doing a forced push to do a rebuild.

eddiezane added a commit to digitalocean/doctl that referenced this pull request Jul 3, 2019
eddiezane added a commit to digitalocean/doctl that referenced this pull request Jul 3, 2019
nlee737 pushed a commit to nlee737/helm that referenced this pull request Jul 20, 2019
This reverts commit d2ab3f5.
The reverted commit turned out to be a workaroud another problem.
The real fix is submitted in PR helm#5680

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam marckhouzam changed the title fix(completion): --flag=val breaks zsh completion v2: fix(completion): --flag=val breaks zsh completion Sep 8, 2019
@allamand

Copy link
Copy Markdown

I run on this problem, would this fix be merged ?

@marckhouzam

marckhouzam commented Sep 20, 2019 •

Copy link
Copy Markdown
Member Author

The https://github.com/helm/acceptance-testing repo can help maintainers get a feel that this change is safe.

If you run the completion tests on the master branch now they will pass.
If you run them again after applying this PR they will fail with an UNEXPECTED SUCCESS.
This is because this PR fixes a specific test that was marked as a known failure.
The PR to mark the test as expected to pass is ready here helm/acceptance-testing#35

(I know this PASS -> FAIL after a PR is merged is not ideal, but with the tests being in a different repo, it is difficult to handle corresponding PRs).

For helm v2, to run the completion tests you can do the following.
(there is a PR to make it easier #6388 )

make && make build-cross && cd $GOPATH/src/helm.sh/acceptance-testing && \
ROBOT_RUN_TESTS=shells.robot ROBOT_HELM_PATH=$GOPATH/src/k8s.io/helm/bin make; cd -

And then look at the logs in $GOPATH/src/helm.sh/acceptance-testing/.acceptance/log.html

@marckhouzam

Copy link
Copy Markdown
Member Author

@mattfarina As discussed, this PR is important to eventually get v2 auto-complete to play well with v3 auto-complete during the migration period.
The change of this PR is the same as #6389 which was merged in v3

@bacongobbler bacongobbler added this to the 2.15.0 milestone Sep 26, 2019

@bacongobbler bacongobbler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch! LGTM

@bacongobbler
bacongobbler merged commit e4f0f73 into helm:master Sep 26, 2019
@marckhouzam
marckhouzam deleted the fix/zshFlagBug branch September 26, 2019 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants