From a2591e8cda7e1d1205506a6524cb0216adee1ca0 Mon Sep 17 00:00:00 2001 From: sadan4 <117494111+sadan4@users.noreply.github.com> Date: Wed, 25 Feb 2026 05:37:48 -0500 Subject: [PATCH] fix(shell-completion): give correct suggestions when command line ends with a space (#10607) * fix(shell-completion): give correct suggestions when command line ends with a space fixes an issue where if you tried to complete any part of any subcommand with a space before (eg: `pnpm run `, `pnpm rm react `), pnpm would give you suggestions for the root command, as if you had typed `pnpm ` close #7964 close #5426 --------- Co-authored-by: Zoltan Kochan --- .changeset/young-radios-fly.md | 5 +++++ .../src/completionServer.ts | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .changeset/young-radios-fly.md diff --git a/.changeset/young-radios-fly.md b/.changeset/young-radios-fly.md new file mode 100644 index 0000000000..c68a070c82 --- /dev/null +++ b/.changeset/young-radios-fly.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-completion": patch +--- + +Give correct suggestions when command line ends with a space. #[10607](https://github.com/pnpm/pnpm/pull/10607/changes) diff --git a/completion/plugin-commands-completion/src/completionServer.ts b/completion/plugin-commands-completion/src/completionServer.ts index 0d8f931f09..92cd152c36 100644 --- a/completion/plugin-commands-completion/src/completionServer.ts +++ b/completion/plugin-commands-completion/src/completionServer.ts @@ -26,10 +26,7 @@ export function createCompletionServer ( const env = tabtab.parseEnv(process.env) if (!env.complete) return - // Parse only words that are before the pointer and finished. - // Finished means that there's at least one space between the word and pointer - const finishedArgv = env.partial.slice(0, -env.lastPartial.length) - const inputArgv = splitCmd(finishedArgv).slice(1) + const inputArgv = splitCmd(stripPartialWord(env)).slice(1) // We cannot autocomplete what a user types after "pnpm test --" if (inputArgv.includes('--')) return const { params, options, cmd } = await opts.parseCliArgs(inputArgv) @@ -48,3 +45,14 @@ export function createCompletionServer ( ) } } + +/** + * Returns the portion of the command line that consists of fully typed words, + */ +function stripPartialWord (env: { partial: string, lastPartial: string }): string { + if (env.lastPartial.length > 0) { + // stripping any word the user is currently typing. + return env.partial.slice(0, -env.lastPartial.length) + } + return env.partial +} \ No newline at end of file