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

Fish completion fails with quotes in argument #1214

Closed
Luap99 opened this issue Aug 28, 2020 · 8 comments · Fixed by #1249
Closed

Fish completion fails with quotes in argument #1214

Luap99 opened this issue Aug 28, 2020 · 8 comments · Fixed by #1249

Comments

@Luap99
Copy link
Contributor

Luap99 commented Aug 28, 2020

The fish completion script prints ugly traces if quotes are not balanced in the argument.

To reproduce: ./testprog '[TAB]

> ./testprog '- (line 1): Unexpected end of string, quotes are not balanced
./testprog __complete ' 
                      ^
in command substitution
        called on line 35 of file -
in function '__testprog_perform_completion' with arguments './testprog\ \''
        called on line 1 of file -
in command substitution
        called on line 71 of file -
in function '__testprog_prepare_completions'
in command substitution

This happens with single and double quotes. Technically the completion still works and offers file completion but the traces makes this unusable.

I also checked this with zsh, bash, #1146 and #1208 but only fish is affected. All other shells offer file completion, which is in my eyes the right thing.

@Luap99
Copy link
Contributor Author

Luap99 commented Aug 28, 2020

@marckhouzam PTAL

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator

Sorry @Luap99 I forgot about this one. I'll try to take a look soon.

@marckhouzam
Copy link
Collaborator

I'm finally looking into this and I believe the issue is that for fish, command errors cannot be re-directed. This means that if a command has unbalanced quoting, like our case, it will print an error.
See fish-shell/fish-shell#6991 for an explanation.

This seems to mean that to avoid the error printouts being visible, we would need to catch the case of unbalanced quoting ourselves, which I don't think is trivial (e.g, ./testprog 'TAB vs ./testprog ''TAB vs ./testprog ''compTAB' etc)

One idea I thought of is that we could execute the command not directly with fish but using sh. So instead of effectively having the completion script call

eval "testprog/bin/testprog __complete '" 2> /dev/null

which prints the error message
we could call

sh -c "testprog/bin/testprog __complete '" 2> /dev/null

which properly redirects the error output to /dev/null

We should maybe check for the presence of sh first? Which makes wonder if jumping through those hoops is worth it?

@Luap99 could you give some more info on when this situation may occur?

@Luap99
Copy link
Contributor Author

Luap99 commented Dec 30, 2020

Why not fish -c "testprog/bin/testprog __complete '" 2> /dev/null? This should work and I think it is safe to assume fish is in $PATH. Or maybe use $SHELL -c ...

@Luap99 could you give some more info on when this situation may occur?

I only hit this issue while testing. I don't think I would have encountered this problem in a normal use case. The fact that no one has opened a similar issue or commented on this one leads me to believe that it is not important to fix this problem.

@marckhouzam
Copy link
Collaborator

Why not fish -c "testprog/bin/testprog __complete '" 2> /dev/null? This should work and I think it is safe to assume fish is in $PATH. Or maybe use $SHELL -c ...

Hey that works (I didn't expect it to)! And I think that is safer than using sh.

In trying to test further I thought of trying completion when using:

$HOME/testprog [TAB]
~/testprog [TAB]

and these currently don't work for fish. This is because of the use of type -q in the script, which was a workaround to not being able to redirect error to /dev/null. So, now that we have a possible solution to the redirection, it suddenly becomes even more interesting to use it.

I'll open a new issue for this new bug and post a PR to address this one and the new one.

@marckhouzam
Copy link
Collaborator

$HOME/testprog [TAB]
~/testprog [TAB]

and these currently don't work for fish.

I've opened #1306

marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Dec 31, 2020
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@marckhouzam
Copy link
Collaborator

I've added the discussed fix for this issue to #1249

marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Jan 3, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Jan 25, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Jan 30, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Feb 14, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Feb 15, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Mar 4, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue May 3, 2021
Fixes spf13#1214
Fixes spf13#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
jpmcb pushed a commit that referenced this issue May 3, 2021
* Fix fish for ShellDirectiveNoSpace and file comp

For fish shell we achieve ShellDirectiveNoSpace by outputing a fake
second completion with an extra character.  However, this extra
character was being added after the description string, instead of
before.  This commit fixes that.

It also cleans up the script of useless code, now that fish completion
details are better understood.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Handle case when completion starts with a space

Fixes #1303

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Support fish completion with env vars in the path

Fixes #1214
Fixes #1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Update based on review

1- We use `set -l` for local variable to make sure there are no
   conflicts with global variables
2- We use `commandline -opc` which:
   a) splits the command line into tokens (-o)
   b) only considers the current command (-p) (e.g., echo hello; helm <TAB>)
   c) stops at the cursor (-c)
3- We extract the last arg with `commandline -ct` and escape it to handle
   the case where it is a space, or unmatched quote.
4- We avoid looping when filtering on prefix.
5- We don't add a fake comp for ShellCompDirectiveNoSpace when the
   completion ends with any of @=/:., as fish won't add a space

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
muscliary pushed a commit to muscliary/cobra that referenced this issue Sep 12, 2023
* Fix fish for ShellDirectiveNoSpace and file comp

For fish shell we achieve ShellDirectiveNoSpace by outputing a fake
second completion with an extra character.  However, this extra
character was being added after the description string, instead of
before.  This commit fixes that.

It also cleans up the script of useless code, now that fish completion
details are better understood.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Handle case when completion starts with a space

Fixes #1303

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Support fish completion with env vars in the path

Fixes spf13/cobra#1214
Fixes spf13/cobra#1306

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

* Update based on review

1- We use `set -l` for local variable to make sure there are no
   conflicts with global variables
2- We use `commandline -opc` which:
   a) splits the command line into tokens (-o)
   b) only considers the current command (-p) (e.g., echo hello; helm <TAB>)
   c) stops at the cursor (-c)
3- We extract the last arg with `commandline -ct` and escape it to handle
   the case where it is a space, or unmatched quote.
4- We avoid looping when filtering on prefix.
5- We don't add a fake comp for ShellCompDirectiveNoSpace when the
   completion ends with any of @=/:., as fish won't add a space

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants