Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

fix: Wrong completion context for chained commands in zsh

未關閉
#139 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
82/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
dart, zsh
領域
cli

研究方向

Start with lib/src/installer/shell_completion_configuration.dart and CompletionState.fromEnvironment, which the issue identifies as passing and reading the completion context. Check the existing completion tests and how the words passed after completion -- relate to COMP_CWORD. Done means commands after |, && or ; suggest the current command’s options, while existing bash completion still works.

由索引模型根據 Issue 內容生成。

描述

bug
Description

Thank you for this cool lib! I'm facing a minor bug when used with zsh.

In zsh, completing a command that isn't at the start of the command line (after |, && or ;) uses the wrong context. The zsh script passes COMP_LINE="$BUFFER", which is zsh's whole edit buffer, and CompletionState.fromEnvironment reads the arguments from COMP_LINE. So everything typed before the current command is treated as CLI arguments.

Steps To Reproduce

Take a CLI my_cli built on CompletionCommandRunner, with commands foo and bar that have different options, and install its completion in zsh with my_cli install-completion-files.

  1. my_cli foo | my_cli bar <TAB> suggests foo's options. The request is parsed as foo | my_cli bar '', so foo is taken as the command being completed.
  2. cd somewhere && my_cli bar <TAB> suggests nothing, so zsh falls back to file names.

Without a shell, this is the request zsh sends for step 1:

ZSH_NAME=zsh COMP_LINE='my_cli foo | my_cli bar ' COMP_POINT=24 COMP_CWORD=2 \
  my_cli completion -- my_cli bar ''

It prints foo's options.

Expected Behavior

A command after |, && or ; completes the same way as at the start of the line. Both my_cli foo | my_cli bar <TAB> and cd somewhere && my_cli bar <TAB> suggest bar's options, just like my_cli bar <TAB>. Bash already behaves this way.

Additional Context

Cause

  • The zsh template in lib/src/installer/shell_completion_configuration.dart runs COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" … completion -- "${words[@]}". $BUFFER is the whole edit buffer, while $words and $CURRENT cover only the current command.
  • CompletionState.fromEnvironment takes the arguments from COMP_LINE (line.trimLeft().split(' ').skip(1)) and ignores the words passed after completion --.
  • Bash isn't affected, because bash itself limits COMP_LINE to the current command. With bash 3.2, echo x | mycmd a <TAB> sets COMP_LINE to mycmd a.

Suggested fix

Build the arguments from the words the scripts already pass after completion --, up to COMP_CWORD, instead of from COMP_LINE. Both scripts pass only the current command's words there, so this fixes zsh without changing the scripts or making users reinstall them.

Workaround

Until this is fixed, a CLI can narrow COMP_LINE itself before the request is handled, in its CompletionCommandRunner subclass:

@override
Future<int?> runCommand(ArgResults topLevelResults) {
  final command = topLevelResults.command;
  if (command?.name == HandleCompletionRequestCommand.commandName) {
    final words = command!.rest; // the words after `completion --`
    final environment = Platform.environment;
    final cword = int.tryParse(environment['COMP_CWORD'] ?? '');
    if (cword != null && cword >= 0 && cword < words.length) {
      final line = words.take(cword + 1).join(' ');
      environmentOverride = {...environment, 'COMP_LINE': line, 'COMP_POINT': '${line.length}'};
    }
  }
  return super.runCommand(topLevelResults);
}

Environment

  • cli_completion 0.6.0
  • Dart 3.13.3
  • zsh 5.9, macOS (arm64)
主要語言
Dart
星號
54
分支
6
平均合併
24 分鐘
30 天內合併 PR
1

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

VeryGoodOpenSource/cli_completion 的其他 Issue

查看 VeryGoodOpenSource/cli_completion 的全部 Issue

相似的 Issue

更多 Dart Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。