fix(cli): restore shell completion script generation

AN
2mo ago
+98-16

Problem

moss completions bash and moss completions zsh always fail on a healthy install:

$ moss completions bash
Shell completion is unavailable in this Typer installation.
$ echo $?
1

The two existing tests in tests/test_completions.py fail on main for this reason (28 passed, 2 failed).

Root cause

Two separate defects in packages/moss-cli/src/moss_cli/commands/completions.py:

1. The working import path was never tried. The fallback chain attempts typer.main.get_completion_script, then typer._completion_shared.get_completion_script. Neither resolves on current Typer — but typer.completion.get_completion_script does, with an identical signature:

>>> from typer.completion import get_completion_script
>>> inspect.signature(get_completion_script)
(*, prog_name: str, complete_var: str, shell: str) -> str

2. The fallback could not succeed even if the import worked. After the inner except, control fell through to an unconditional print_error(...) / raise typer.Exit(1), so a successful typer._completion_shared import would still have errored out.

Fix

  • Add typer.completion to the import chain.
  • Nest the handlers so the failure message is raised only once every path is exhausted.
  • Lift the message to a module constant instead of duplicating the string.

Verification

beforeafter
pytest tests/28 passed, 2 failed30 passed
black --check—clean
isort --check-only—clean
mypy—clean
flake8 E501 in this file21

moss completions bash now emits a valid script:

$ moss completions bash
_moss_completion() {
    local IFS=$'\n'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   _MOSS_COMPLETE=complete_bash $1 ) )

Relationship to #404

#404 includes a shell-completion fix among other changes. This PR is a targeted alternative to just that portion — one file, +10/−12, no lockfile churn — since #404 currently reports conflicts. Happy to close this if #404 lands first.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JJrKvKssS5wMSUEU5ePWuw

Summary by CodeRabbit

  • Bug Fixes
    • Improved shell completion setup across supported CLI environments.
    • Increased compatibility with different completion support configurations.
    • Added a clear error message when completion generation is unavailable.
    • The CLI now exits with a failure status when completion scripts cannot be generated, making setup issues easier to identify.
    • Existing completion behavior remains available across environments using different supported completion mechanisms.