- Notifications
You must be signed in to change notification settings - Fork1.4k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Tab completion- ensure a zsh user's prior shell options don't interfere with indexing into an array of arguments#1422
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
…re with indexing into an array of arguments
which the user has over-ridden.
mislav approved these changesSep 14, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Fantastic investigation! Thank you so much
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While studying the
rbenv
codebase, I came across therbenv.zsh
tab completion file. I noticed it indexes into an array of user-provided optionshere, usingwords[2, -2]
. After some experimentation, I learned thatbash
uses 0-based indexing by default for its arrays, whilezsh
uses 1-based indexing.As an experiment, I added an
echo
statement to see what the value ofwords
would be:According tothe zsh docs, the
KSH_ARRAYS
zsh shell option determines whether zsh uses 0-based or 1-based indexing. Therefore, after adding the aboveecho
statement and re-runningeval "$(rbenv init - zsh)"
, I then attempted rbenv tab completion twice: once after runningsetopt KSH_ARRAYS
, and once after runningunsetopt KSH_ARRAYS
.When the user has
KSH_ARRAYS
unset,words[2, -2]
will have one more value in it than when the option is set. Therefore, if anrbenv
user is running zsh and has changed their shell emulation by updating theKSH_ARRAYS
option (perhaps because they prefer 0-based indexing), they may experience unexpected behavior in the options they see during tab completion.This PR adds a line of code to the
_rbenv
function to use all zsh default options, but only within the scope of the function. The options will be restored to the user's original preferences once the function scope is exited. I tested this locally with the same steps that I used to reproduce the issue, and it results inwords[2, -2]
being evaluated the same regardless of whether I had runsetopt KSH_ARRAYS
orunsetopt KSH_ARRAYS
just prior to execution:Admittedly, I'm guessing most zsh users don't override their
KSH_ARRAYS
option, and I checked the Github history for this project and no one has created an issue to address this in its 10+-year history, so I wouldn't blame you if you considered this a solution in search of a problem. This was mostly a learning experience for me as a journeyman open-source contributor, but I'm submitting the pull request anyway on the off-chance that you think it adds a greater-than-zero amount of value. 😊