Repository navigation
fix: don't let the unambiguous fast path rewrite the word destructively - #597
luochen1990 wants to merge 2 commits into
Conversation
When an external completer feeds bare full-path candidate words alongside the file group (e.g. user-defined completers injecting candidates from their own source), compstate[unambiguous] can be cut at a path-segment boundary and become a proper prefix of the current word. The fast path then honors insert=unambiguous and zsh rewrites the word, silently deleting already-typed characters -- e.g. 'cmd dir/' becomes 'cmd dir' with no menu, or worse, inside quotes the whole path after the opening quote disappears. Skip the fast path when unambiguous is a proper prefix of the current word: the word is already more specific than the common prefix, so inserting it has no information gain. Normal extension insertion (word is shorter than unambiguous) is unaffected.
|
While dogfooding the first guard downstream, I ran into a second way the same fast path can rewrite the word destructively, so I've added a second, complementary guard in edd2329 (title updated to cover both). Where the first guard catches the fast path deleting already-typed characters, the new one catches zsh fabricating characters: with substring matchers ( A comptest regression is included; it fails on current master and also on master + the first guard alone, confirming the two guards are complementary. Happy to split this into a separate PR if you prefer reviewing them independently. |
…ing no candidate has (upstream PRs Aloxaf#597, Aloxaf#576, Aloxaf#600, combined) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What
Two guards on the unambiguous fast path in
-ftb-complete, both rejectingshapes where
compstate[unambiguous]cannot be trusted to extend the wordsafely:
compstate[unambiguous]is a proper prefix ofthe current word (compared as
"$compstate[quote]$IPREFIX$PREFIX");compstate[unambiguous]is not a prefix of anycaptured candidate's full insertion form.
Both guards only fall back to opening the picker; nothing else changes.
Why
The fast path trusts
compstate[unambiguous]: when it differs from the typedword, it sets
compstate[insert]=unambiguous, letting zsh rewrite the word inplace and skipping the picker entirely. That trust is misplaced in two
specific shapes.
Guard 1 — destructive truncation. When candidates come from mixed
sources — e.g. a user completer feeding bare full-path candidate words
(
compadd ... dir/a/mod.nix dir/b/mod.nix) alongside the_filesgroup — zshcan compute an
unambiguousthat is cut at a path-segment boundary and endsup as a proper prefix of the word. Inserting it then deletes already-typed
characters:
cmd dir/<TAB>→ the buffer silently becomescmd dir— the trailing slashis gone, no menu, just a beep
cmd "dir/<TAB>→ the whole path after theopening quote disappears
In this shape the word is strictly more specific than the common prefix, so
the insertion has zero information gain — the sensible behavior here is to
open the picker. Normal extension insertion (word shorter than
unambiguous, e.g.d→dir) is unaffected, and an unquoted empty wordfalls through to the fast path unchanged.
Guard 2 — fabricated strings. Zsh computes
compstate[unambiguous]overmatcher alignments. With substring matchers (
l:|=* r:|=*, part of thedefault matcher-list in Oh My Zsh and a popular fuzzy-completion setting) and
candidates aligning the word at inconsistent offsets — one candidate ending
with the word, another containing it mid-string — the computation fabricates
a string with a duplicated leading character of the word. Observed live:
typing
cmd forgejo-actions<TAB>with candidatesgit.lambda.lc/lc-studio/forgejo-actionsandcodeberg.org/nix/forgejo-actions-runnerrewrote the buffer tocmd fforgejo-actions(and/forgejo-actionsin the comptest run — thefabricated character varies with alignment/order). Such a string prefixes
no candidate: no menu selection could ever reach it, so inserting it
corrupts the word with characters the user never typed. Requiring that at
least one candidate's full insertion form starts with
unambiguouskeeps thefast path exactly for the reachable cases (common-prefix extension, unique
full expansion, path-prefixed and quoted completion) and routes the
fabricated ones to the picker.
The two guards are complementary: a fabricated string is typically not a
prefix of the word (guard 1 misses it), and a truncating
unambiguousmaystill be a genuine candidate prefix (guard 2 misses it).
This bug family overlaps #560 / #589 (the fast path acting on a misleading
compstate[unambiguous]value); #596 is a related-but-different request(opt-in picker after unambiguous insertion). This change only carves out the
clearly-wrong subsets — destructive rewrites of the word; it intentionally
does not change behavior for the misleading-value cases in #560/#589 or
implement #596. Compared to #576, which requires
unambiguousto becontiguous in every candidate word to also address the bias reported in
#560, the reachability check here is deliberately narrower — it only rejects
strings no menu selection could ever produce.
How
Two extra conditions in the existing guard chain, right after the current
"differs from the word" test:
For guard 1, unquoted equality is already excluded by the preceding
condition, so the prefix test only rejects the proper-prefix (truncation)
shape. Inside quotes the preceding condition compares against a form with a
trailing quote, so the new condition additionally catches the exact-equality
shape — where master performs a no-op rewrite, the patched behavior opens
the picker, which matches what master already does for the same word
unquoted. The comparison is a plain case-sensitive string prefix test, so it
does not touch the case-folding scenario of #589. The leading
compstate[quote]matters: inside quotescompstate[unambiguous]itselfbegins with the quote character, while on the word side the quote lives in
compstate[quote], not in$IPREFIX$PREFIX— omitting it would misalignthe two sides and quoted words would silently lose the guard. Any closing
quote on the line is not part of the constructed form, which is harmless for
a prefix test.
For guard 2,
_ftb_amb_is_candis computed just above by scanning_ftb_compcap: each entry is split on NUL, its insertion form isreconstructed as
compstate[quote]+IPREFIX+hpre(thecompadd -ppath prefix) + word, and the flag is set when any entry's form starts with
compstate[unambiguous]. The comparison must use the full insertion form,not the bare word segment:
compstate[unambiguous]lives in the fullcommand-line word space (carrying the leading quote and the path prefix), so
comparing against bare words would block the legitimate fast path for
path-prefixed file completion and for quoted words. The reconstruction
concatenates the kv segments in entry order (
hpreprecedesIPREFIXthere), which diverges from zsh's true insertion space (
IPREFIXfirst) onlywhen both are non-empty — a corner where the mismatch is always
over-blocking (falls back to the picker), never permissive.
apre(-P)and
-iare deliberately omitted: shortening the reconstruction form onlymakes the prefix test harder to pass, i.e. also safely over-blocking. A loop
cannot be inlined into the
\\continuation chain, hence the flag; the outerconditions of the scan gate it to fast-path-relevant cases only.
Testing
Three comptest cases in
test/fzftab.ztst:mixed with
_files(guard 1) — both fail on current master with exactlythe destructive rewrites described above;
forgejo-actionsagainst the two URLs above opens the picker instead ofsilently rewriting the buffer — it fails on current master (and on master
both guards.
The full ztst suite passes (
zsh +Z -f test/ztst.zsh test/fzftab.ztst).Also verified in live interactive shells (zsh 5.9.2, fzf 0.74.3) and deployed
via a downstream NixOS packaging patch.