Skip to content

fix: graceful handling of shell completion KeyError crash - #27

Merged
codeasier merged 2 commits into
mainfrom
fix/completion-keyerror-crash
Apr 17, 2026
Merged

codeasier merged 2 commits into
mainfrom
fix/completion-keyerror-crash

Conversation

@codeasier

Copy link
Copy Markdown
Owner

📝 Description

Fix a crash where pressing Tab in some terminal environments causes a KeyError: 'COMP_WORDS' traceback instead of failing gracefully.

Root cause: Click's shell completion logic accesses os.environ["COMP_WORDS"] directly when it detects completion mode. In non-standard terminal environments (e.g., IDE terminals, terminal wrappers), completion may be triggered without the expected environment variables being set.

Fix: Added a _safe_call() wrapper function that catches these specific KeyErrors and displays a helpful message:

Error: shell completion is not properly configured. Run 'pt-snap --install-completion' and restart your shell.
  • Updated console script entry point (pyproject.toml) to use _safe_call
  • Updated __main__ block to use _safe_call
  • Unrelated KeyErrors are still raised normally

🔗 Related Issue

Fixes #26

🧪 Testing

  • Tests added — 3 new test cases for _safe_call
  • Manual testing completed
  • 194 tests pass

✅ Checklist

  • Code follows project guidelines
  • No new warnings introduced
  • Changes tested locally

codeasier added 2 commits April 17, 2026 22:05
Click's shell completion raises KeyError when COMP_WORDS/COMP_LINE
env vars are missing in non-standard terminal environments. Added
a _safe_call() wrapper that catches these specific KeyErrors and
displays a helpful message instead of crashing with a traceback.

- Updated console script entry point to use _safe_call
- Updated __main__ block to use _safe_call
- Added tests for the wrapper (happy path, completion error, unrelated error)

Closes #26
The previous fix printed an error message to stdout, which zsh
completion captured and tried to execute as a command, causing
'(eval):1: command not found: Error:'. Exit silently instead.

Closes #26
@codeasier
codeasier merged commit d68d441 into main Apr 17, 2026
5 checks passed
@codeasier
codeasier deleted the fix/completion-keyerror-crash branch April 17, 2026 14:12
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 this pull request may close these issues.

bug: KeyError: 'COMP_WORDS' crash on shell tab completion

1 participant