Skip to content

Directory picker skips the last suggestion when Up is pressed initially #44960

In review

Description

@sdivyanshu90

Description

In the directory picker autocomplete, pressing Up before any suggestion is active selects the penultimate result. This makes reverse keyboard navigation skip the last suggestion on the first key press.

Steps to reproduce

  1. Open the directory picker in the web or desktop app.
  2. Type a path or search term that produces at least three autocomplete suggestions.
  3. Leave the suggestions unselected.
  4. Press Up once.

Observed behavior

The penultimate suggestion becomes active.

Expected behavior

The last suggestion should become active so reverse navigation wraps from the unselected state to the end of the list.

Additional context

Down correctly starts at the first suggestion. Up should mirror that behavior from the opposite end. The issue is independent of the operating system and terminal because it occurs in the picker index calculation.

Activity

  1. sdivyanshu90 commented on Sep 19, 2026

    @sdivyanshu90
    ContributorAuthor

    Could you please review this issue?

  2. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the clear report, @sdivyanshu90. I can confirm this, and it still happens on the latest dev (a882cf9).

    When no suggestion is active, the picker stores the index as -1. nextSuggestionIndex then calculates (-1 - 1 + n) % n, which is n - 2, so the first Up press lands on the second-to-last suggestion. Down calculates (-1 + 1 + n) % n, which is 0, so it works.

    In the picker you see this when the input ends in / (or is ~ or /), because those are the cases where nothing is active. With a plain search term the first suggestion is already active, so Up correctly wraps to the last one.

    The script below calls the picker's own index function and shows the wrong result on v2.2.0 and on dev.

    Related: #44998, your PR with a fix, which the stale-PR cleanup closed without merging.

    Reproduction script (repro.ts)
    // Reproduces #44960: directory picker ArrowUp from the unselected state
    // selects the second-to-last suggestion instead of the last.
    // Usage: bun /workspace/repro/repro.ts [path/to/packages/app/src/workspaces/selection/domain.ts]
    const path = process.argv[2] ?? "/workspace/repo/packages/app/src/workspaces/selection/domain.ts"
    const { nextSuggestionIndex } = await import(path)
    
    // dialog.tsx activeSuggestionIndex() returns -1 (nothing active) when the
    // input is "~", "~/", "/" or ends with "/", e.g. "~/projects/".
    const unselected = -1
    const count = 3
    const down = nextSuggestionIndex(unselected, 1, count)
    const up = nextSuggestionIndex(unselected, -1, count)
    console.log(`suggestions=${count}, nothing active`)
    console.log(`ArrowDown -> index ${down} (expected 0)`)
    console.log(`ArrowUp   -> index ${up} (expected ${count - 1})`)
    if (up !== count - 1) {
      console.log("BUG: ArrowUp skipped the last suggestion")
      process.exit(1)
    }
    console.log("OK")

    Run it: bun repro.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions