Skip to content

fix(zsh): isolate generated completion options#686

Merged
jdx merged 1 commit into
mainfrom
codex/fix-zsh-completion-emulation
Jun 17, 2026
Merged

fix(zsh): isolate generated completion options#686
jdx merged 1 commit into
mainfrom
codex/fix-zsh-completion-emulation

Conversation

@jdx

@jdx jdx commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • add emulate -L zsh to generated zsh completion functions
  • update zsh completion snapshots and the checked-in _usage completion asset
  • add a regression test for KSH_ARRAYS leaking into generated completions

Root Cause

Generated zsh completions parse complete-word --shell zsh rows into arrays and index those arrays using normal zsh one-based semantics. If a user's shell has options such as KSH_ARRAYS enabled, those semantics can leak into the completion function and shift or empty the parsed insert values, causing tab-delimited completion rows to be inserted incorrectly.

Validation

  • cargo build --all
  • cargo test -p usage-lib complete::zsh --all-features
  • cargo test -p usage-cli test_zsh_completion_ignores_ksh_arrays_option --all-features
  • mise run render:usage-cli-completions (includes cargo clippy --all --all-features --fix --allow-dirty --allow-staged -- -D warnings, cargo fmt --all, and prettier -w .)

This PR was generated by an AI coding assistant.


Note

Low Risk
Localized change to generated zsh shell scripts and test/snapshot updates; no runtime CLI or auth/data paths affected.

Overview
Generated zsh completion scripts now start each completion function with emulate -L zsh so user shell options (notably KSH_ARRAYS) do not change how tab-split complete-word rows are indexed into parts[1..3] and passed to compadd.

The generator in lib/src/complete/zsh.rs applies this to per-binary _{bin} handlers and the shebang _usage_default_complete init path; snapshots, cli/assets/completions/_usage, and a new integration test assert completions still work when KSH_ARRAYS is enabled.

Reviewed by Cursor Bugbot for commit fba8bf3. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed zsh shell completions to work correctly when KSH_ARRAYS option is enabled, ensuring completion suggestions display properly and array indexing remains accurate.
  • Tests

    • Added regression test to validate zsh completion functionality with KSH_ARRAYS option enabled in shell environments.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 00ac245a-9964-4ed2-9531-60c9ff1d29eb

📥 Commits

Reviewing files that changed from the base of the PR and between 30faca6 and fba8bf3.

⛔ Files ignored due to path filters (4)
  • lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh-2.snap is excluded by !**/*.snap
  • lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh-3.snap is excluded by !**/*.snap
  • lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh.snap is excluded by !**/*.snap
  • lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh_init.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • cli/assets/completions/_usage
  • cli/tests/shell_completions_integration.rs
  • lib/src/complete/zsh.rs

📝 Walkthrough

Walkthrough

emulate -L zsh is prepended to two locations in the Rust zsh completion generator (complete_zsh and complete_zsh_init) and to the static _usage completion script. A new integration test verifies that completions work correctly when the parent shell has KSH_ARRAYS enabled.

Changes

Zsh emulate -L fix and regression test

Layer / File(s) Summary
Add emulate -L zsh to generator and static asset
lib/src/complete/zsh.rs, cli/assets/completions/_usage
emulate -L zsh is inserted at the start of the generated per-bin completion function body, the generated _usage_default_complete handler, and the static _usage script entry point.
KSH_ARRAYS regression integration test
cli/tests/shell_completions_integration.rs
New test test_zsh_completion_ignores_ksh_arrays_option generates completions from a minimal spec, invokes zsh with setopt KSH_ARRAYS and a stubbed compadd, and asserts the doctor insert is present in the output.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 A zsh array starts at one, not zero,
But KSH_ARRAYS makes it a different hero!
So I added emulate -L with glee,
To keep completions as they should be.
No mis-indexing shall pass — hop hop, hooray! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(zsh): isolate generated completion options' directly addresses the main objective of the PR, which is to fix zsh completion functions by isolating them from user environment options using emulate -L zsh.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where user shell options (e.g. KSH_ARRAYS) could leak into generated zsh completion functions, causing tab-split array indexing to use zero-based semantics and produce incorrect or empty insert values. The fix adds emulate -L zsh at the top of both the per-bin completion function and the _usage_default_complete shebang fallback handler.

  • lib/src/complete/zsh.rs: emulate -L zsh is emitted as the first statement in _{bin_snake}() and _usage_default_complete(), resetting array-indexing and other option semantics to standard zsh defaults for the duration of each call.
  • cli/tests/shell_completions_integration.rs: A new integration test (test_zsh_completion_ignores_ksh_arrays_option) sets KSH_ARRAYS before sourcing the generated script and asserts that doctor appears correctly in compadd's inserts.
  • All four affected snapshots and the checked-in _usage asset are updated to match.

Confidence Score: 5/5

Safe to merge — the change is a one-liner in two well-isolated spots, all snapshots and the checked-in asset are updated, and a targeted regression test guards the exact failure mode.

The fix is minimal and well-understood: emulate -L zsh is the canonical zsh idiom for isolating completion functions from user options, and it has no known negative side-effects on the completion machinery used here. The regression test directly exercises the KSH_ARRAYS edge case end-to-end, and all snapshot diffs are mechanical additions of the single new line.

No files require special attention.

Important Files Changed

Filename Overview
lib/src/complete/zsh.rs Adds emulate -L zsh at the top of both generated completion functions to isolate array-indexing semantics from user shell options like KSH_ARRAYS
cli/tests/shell_completions_integration.rs Adds regression test that sets KSH_ARRAYS before invoking generated completions and asserts "doctor" is correctly inserted
cli/assets/completions/_usage Checked-in _usage completion asset updated to include emulate -L zsh consistent with generated output
lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh.snap Snapshot updated to reflect emulate -L zsh addition in generated function
lib/src/complete/snapshots/usage__complete__zsh__tests__complete_zsh_init.snap Snapshot updated for _usage_default_complete() with emulate -L zsh

Reviews (1): Last reviewed commit: "fix(zsh): isolate generated completion o..." | Re-trigger Greptile

@jdx jdx merged commit 52b4d09 into main Jun 17, 2026
6 checks passed
@jdx jdx deleted the codex/fix-zsh-completion-emulation branch June 17, 2026 02:00
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.

1 participant