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

bash completion has syntax errors #6486

Closed
3 tasks done
danielhoherd opened this issue Sep 12, 2022 · 5 comments
Closed
3 tasks done

bash completion has syntax errors #6486

danielhoherd opened this issue Sep 12, 2022 · 5 comments
Labels
kind/bug Something isn't working as expected

Comments

@danielhoherd
Copy link

  • I am on the latest Poetry version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

  • OS version and name: raspios, macos

  • Poetry version: 1.2.0

  • Link of a Gist with the contents of your pyproject.toml file: N/A, this is just for the completion code

  • Bash versions tested:

    • GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
    • GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
    • GNU bash, version 5.1.4(1)-release (aarch64-unknown-linux-gnu)

Issue

poetry completions bash generates syntactically incorrect bash code.

Steps to reproduce

$ poetry completions bash > poetry-completion.sh
$ source poetry-completion.sh
-bash: poetry-completion.sh: line 40: syntax error near unexpected token `clear'
-bash: poetry-completion.sh: line 40: `            (cache clear)'
$ shellcheck poetry-completion.sh

In poetry-completion.sh line 26:
        case "$com" in
        ^-- SC1009 (info): The mentioned syntax error was in this case expression.


In poetry-completion.sh line 40:
            (cache clear)
            ^-- SC1073 (error): Couldn't parse this case item. Fix to allow more checks.
                   ^-- SC1072 (error): Expected ) to open a new case item. Fix any mentioned problems and try again.
                   ^-- SC1085 (error): Did you forget to move the ;; after extending this case item?

For more information:
  https://www.shellcheck.net/wiki/SC1085 -- Did you forget to move the ;; aft...
  https://www.shellcheck.net/wiki/SC1072 -- Expected ) to open a new case ite...
  https://www.shellcheck.net/wiki/SC1073 -- Couldn't parse this case item. Fi...
@danielhoherd danielhoherd added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Sep 12, 2022
@danielhoherd
Copy link
Author

I see a few issues with the completion code.

First, bash case statement items are not wrapped in parenthesis, they are delimited by a closing parenthesis.

        case "$com" in
            (about)
            opts="${opts} "
            ;;

Should be

        case "$com" in
            about)
            opts="${opts} "
            ;;

Secondly, and a bigger problem to solve, the comparisons being made in the case statement are trying to compare multiple words when only one word is being checked. This is a problem both syntactically and in terms of how the code is structured.

            cache clear)
            opts="${opts} --all"
            ;;

That code is syntactically invalid because there is unquoted whitespace in it. cache clear) is an invalid case match. "cache clear") would work, but that would be wrong for poetry, requiring users to run poetry "cache clear" or equivalent. EG:

#!/usr/bin/env bash
# cat example-1.sh
case "$1" in
  "cache clear")
    echo "1 is $1"
    ;;
  *)
    echo "default target"
    ;;
esac
$ shellcheck example-1.sh ; echo $? ;
0
$ ./example-1.sh cache clear
default target
$ ./example-1.sh "cache clear"
1 is cache clear

So the way this needs to be structured is to check each word individually, then any more words beyond that one, in a tree structure of completions rather than a flat list of possible completions:

#!/usr/bin/env bash
# example-2.sh
case "$1" in
  cache)
    echo "1 is $1"
    case "$2" in
      clear)
        echo "2 is $2"
        ;;
      *)
        echo "default cache target"
        ;;
      esac
    ;;
  *)
    echo "default target"
    ;;
esac
$ shellcheck example-2.sh ; echo $? ;
0
$ ./example-2.sh cache clear
1 is cache
2 is clear
$ ./example-2.sh cache jklsjdfk
1 is cache
default cache target

@dimbleby
Copy link
Contributor

duplicate #4572, and the several other duplicates linked from that

@Secrus
Copy link
Member

Secrus commented Sep 12, 2022

As mentioned in the previous comment, it's a duplicate. What's more, a fix was made upstream in cleo, pending release.

@Secrus Secrus closed this as completed Sep 12, 2022
@danielhoherd
Copy link
Author

danielhoherd commented Sep 12, 2022

Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

4 participants