Skip to content

vp -C dev run gives no Bash task completions when the directory is named dev #3608

In review

Description

@RSS1102

Steps to reproduce

Using the v1.8.0 global CLI in Bash on macOS, create a project with a different task in each directory:

cd "$(mktemp -d)"
mkdir dev
echo '{"name":"root-demo","private":true,"scripts":{"review-root":"pwd"}}' > package.json
echo '{"name":"child-demo","private":true,"scripts":{"review-child":"pwd"}}' > dev/package.json
npm install --save-exact vite-plus@1.8.0
source <(VP_COMPLETE=bash vp)

From the project root, type each command and press Tab immediately after review-.

Input Expected candidate Actual candidate
vp -C dev run review- review-child No candidates
vp -C ./dev run review- review-child review-root

Typing review-child in full and pressing Enter runs the task in the dev directory with either spelling of the path.

This came up while reviewing Vitest option completion. The behavior above reproduces with vp run on the released v1.8.0 CLI. Related discussion in PR #3190

Activity

  1. added theissue type on Nov 29, 2026
  2. voidzero-bot commented on Nov 29, 2026

    @voidzero-bot
    Contributor
    • Reproduced: Yes — both reported cases (vp -C dev run review-<TAB> → no candidates, vp -C ./dev run review-<TAB> → review-root) reproduce on current main, and the bug is broader than the dev name: any -C <dir> completes tasks from the invocation directory.
    • Exploration: Yes — View branch
    • Unit Test: Yes — 4 tests added in the tests module of crates/vp_global_cli/src/main.rs (qualify_completion_chdir_*)
    • Priority: Priority P2: Has Workaround. Only shell completion is affected — execution (vp -C dev run review-child) works correctly with either path spelling, and users can simply cd into the directory or type the task name. It does break the documented -C completion behavior for every directory target, not just the reported one.

    Two defects combined to cause this. First, the pre-completion -C peek in crates/vp_global_cli/src/main.rs was dead code: the shell glue invokes the completer as vp -- <bin> <words...>, so parse_leading_chdir(&args[1..]) always saw the -- separator first and the process cwd was never changed, leaving the cwd-sensitive task completer in the invocation directory. Second, clap_complete 4.6.11's engine matches words against subcommand names before consuming a pending option value, so a directory named like a subcommand (dev, build, ...) routed -C dev run ... into the vp dev subcommand, producing zero candidates.

    I found a potential fix for this issue. On branch triagebot/fix-3608, complete_request() now drives the completion via the public CompleteEnv::try_complete, consuming the leading -C <dir> from the completion words (applied with the existing apply_chdir) and rewriting the bare value to a path spelling (dev → ./dev) so the engine consumes it as the option value instead of a subcommand name — the word count is unchanged, so every shell's cursor-index derivation keeps working. This needs human review before merging.

    Full Triage Report

    Triage report — issue #3608

    Issue summary

    vp -C dev run offers no Bash task completions when the target directory is named dev, and vp -C ./dev run offers the root project's tasks instead of the child directory's tasks. Expected: both spellings complete tasks from the -C target directory (review-child).

    Environment

    • Vite+ global CLI: vp v1.8.0 on PATH (bootstrapped from this checkout; crates/vp_global_cli/Cargo.toml also reports 1.8.0 at current main, HEAD 5cf0b72c8)
    • vite-plus local package: 1.8.0 (installed from npm in the repro project)
    • Node.js v24.22.3, pnpm v12.11.2, GNU bash 5.3.9, Ubuntu Linux x86_64
    • Reporter environment: v1.8.0 global CLI, Bash, macOS (shell-level behavior reproduced identically on Linux, so not host-specific)

    Reproduction path

    /tmp/vite-plus-triage/gh-3608/repro — the exact project from the issue, plus a web/ control child with script review-web.

    Commands attempted and results

    1. Direct clap_complete protocol invocation (what the Bash glue calls): VP_COMPLETE=bash vp -- vp -C dev run review- → no candidates; vp -C ./dev run review- → review-root (wrong).
    2. Faithful Bash Tab-press simulation (sourced VP_COMPLETE=bash vp register script, invoked _clap_complete_vp with COMP_WORDS/COMP_CWORD/COMP_TYPE=9):
    Input line Candidates Expected
    vp run review- (control) review-root review-root ✓
    vp -C dev run review- (none) review-child ✗
    vp -C ./dev run review- review-root review-child ✗
    vp -C web run review- (control) review-root review-web ✗
    vp -C build run review- (control) (none) —
    1. Execution control (tasks themselves work): vp -C dev run review-child exits 0 and runs in the dev directory, confirming only completion is broken.

    Result: reproduced on the current checkout, both reported observations match exactly.

    Diagnosis

    The behavior is the visible result of two independent defects, one in Vite+ and one an upstream clap_complete engine limitation. Both live in the global-CLI completion path; the local JS CLI has no completion code of its own, so the fix layer is crates/vp_global_cli.

    Bug 1 — the -C-before-completion peek never fires (explains ./dev → review-root)

    • Files/lines: crates/vp_global_cli/src/main.rs:462-474 (parse_leading_chdir(&args[1..]) + apply_chdir before CompleteEnv::...complete()).
    • All shell glue invokes the completer as vp -- "${words[@]}" (bash: crates/vp_global_cli/completion-register.bash; clap_complete's generated zsh/fish/powershell glue). clap_complete's CompleteEnv::try_complete_ (4.6.11, src/env/mod.rs:239-246) drains argv up to and including --, but the peek in main.rs runs before complete() and sees the raw argv [<vp path>, "--", "vp", "-C", "dev", "run", "review-"].
    • parse_leading_chdir(&args[1..]) inspects args[1], which is "--", so it always returned None. The process cwd therefore stayed the invocation directory, and the cwd-sensitive task completer (crates/vp_global_cli/src/cli.rs:646, run_tasks_completions → commands::delegate::execute_output(cwd, "run", ...)) suggested the root project's tasks.
    • Introduced in 43047d1 (PR feat(cli): global -C flag for working-directory switching #2031); the peek never fired through the real glue. The existing parse_leading_chdir unit tests pass because they never include the -- + bin-name prefix.
    • Empirical proof: applying the chdir by hand (running the completer from inside dev/) yields review-child for vp run review-; the glued invocation from the root yields review-root.

    Bug 2 — clap_complete routes an option value that matches a subcommand name into that subcommand (explains dev → zero candidates)

    • Upstream: clap_complete 4.6.11, src/engine/complete.rs word loop (~lines 59-65): for every non-flag word it calls current_cmd.find_subcommand(value) before consulting ParseState::Opt (pending option value).
    • For vp -C dev run review-, after -C sets ParseState::Opt, the next word dev matches the vp dev subcommand (cli.rs:126), so the engine descends into vp dev; run becomes a positional of Dev { args: Vec<String> } (trailing var args, no completer), and review- completes against those positionals → zero candidates.
    • Any -C target colliding with a vp subcommand triggers this: confirmed dev, build → no candidates; web → root candidates. Verified behaviorally: vp dev run review- (no -C at all) produces the same zero-candidate result, confirming the misroute.
    • Worth reporting upstream to clap_complete (pending option value should win over the subcommand name match), but it can be worked around in vp_global_cli.

    Verification

    Verdict: bug — contradicts the documented contract for -C (docs/guide/monorepo.md:210-220: "The global -C flag runs any vp command *as if you had cd'd into the package first, identical to cd <dir> && vp <command>"; the cd dev form completes review-child), the explicit intent in the completion code comment, and execution-vs-completion consistency. No issue/PR establishes an intent for this behavior. Confidence: high.

    Fix (branch triagebot/fix-3608)

    Changed files: crates/vp_global_cli/src/main.rs only (136 insertions, 11 deletions, including 4 unit tests).

    complete_request() now owns the completion request, calling the public CompleteEnv::try_complete with the argv instead of complete() (which re-reads raw argv), after qualify_completion_chdir() has:

    • dug a leading -C <dir> out of the words behind the -- separator and applied it with the existing apply_chdir (best-effort, silent), and
    • rewritten the separate -C <dir> form's value to a path spelling (dev → ./dev), which resolves to the same directory but can never equal a subcommand name, so the engine consumes it as the option value. Attached forms (-Cdev, -C=dev) keep the value inside the flag token, and values with path separators or .// prefixes cannot collide, so only bare relative names are rewritten.

    Word count is never changed, so the shell-exported _CLAP_COMPLETE_INDEX stays valid for every glue (bash/zsh set it explicitly; fish/PowerShell writers derive the index as args.len() - 1). No env mutation, no new clap arguments, no completion-command-surface changes. While the cursor is still on the -C flag or its value (vp -C <TAB>, vp -C dev<TAB>), the request passes through untouched.

    Regression tests

    Four unit tests beside the existing parse_leading_chdir tests in crates/vp_global_cli/src/main.rs: qualify_completion_chdir_qualifies_subcommand_named_directory, qualify_completion_chdir_keeps_path_spellings_intact, qualify_completion_chdir_keeps_attached_forms_intact, qualify_completion_chdir_passes_through_mid_chdir_requests. No snapshot fixtures exercise VP_COMPLETE completions, and the PTY snapshot runner drives command execution rather than shell completion glue, so end-to-end behavior was validated manually.

    Validation

    • cargo test -p vp_global_cli — 581 passed (dev and release), including the 4 new tests.
    • cargo fmt --all --check, crate clippy, just check, and just lint — clean.
    • End-to-end from the issue's reproduction project (before → after):
    Input Before After
    vp -C dev run review-<TAB> (none) review-child
    vp -C ./dev run review-<TAB> review-root review-child
    vp run review-<TAB> (control) review-root review-root
    vp -C web run review-<TAB> (control) review-root review-web
    vp -Cdev run review-<TAB> review-root review-child
    vp -C=dev run review-<TAB> review-root review-child
    vp -C ../repro/dev run review-<TAB> review-root review-child
    vp -C dev run <TAB> (new word) (none) task + subcommand candidates
    vp -C dev<TAB> / vp -C <TAB> (none) (none) (unchanged)

    Verified through the real Bash glue (_clap_complete_vp with COMP_WORDS/COMP_CWORD/COMP_TYPE=9) and the fish/PowerShell writers (different cursor-index derivation), plus unchanged VP_COMPLETE=bash|zsh vp registration output, VP_COMPLETE=0/VP_COMPLETE= falling through to a normal run, and vp -C dev run review-child execution still running in dev.

    Skipped checks and reasons

    • pnpm test / full JS suites — no TypeScript changes.
    • just snapshot-test — no snapshot covers shell completions.
    • Upstream clap_complete engine fix — worth reporting upstream, but the in-repo rewrite fully works around it.

    Alternatives considered

    • Strip the -C tokens and rewrite _CLAP_COMPLETE_INDEX: also works, but requires mutating a process env var after the Tokio runtime exists (edition 2024 unsafe); the path-qualify rewrite achieves the same parse with no index bookkeeping.
    • Only fix the dead peek: would fix the ./dev case but leave -C dev completing nothing.
    • A hidden clap flag for completion-only chdir: the engine's subcommand check still misroutes the value word, so it does not work without rewriting the value anyway.

    Known remaining limitation

    A -C that is not leading (e.g. vp run -C dev <TAB>, or vpr -C dev ... where the glue prepends run) is not consumed by completion; it falls back to the invocation-directory suggestions, matching the pre-existing scope of the leading-only parse_leading_chdir pre-parse used for real runs.

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

    I've opened a pull request with this fix: #3609

  3. added
    - P2: has workaround
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundtriage: fix verified

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Target date

    None yet

    Start date

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions