Repository navigation
fix(cli): 输出被 SilenceErrors 吞掉的错误,父命令遇未知子命令报错 - #15
Open
shushenghong wants to merge 1 commit into
Open
shushenghong wants to merge 1 commit into
shushenghong wants to merge 1 commit into
Conversation
rootCmd 开启了 SilenceErrors,Execute() 拿到错误只 os.Exit(1) 不打印,导致: - 未知参数(如 comment add --current-user、bug update --id=1)、位置参数个数不对等 cobra 层错误,以及 RunE 直接 return 的错误,调用方只看到退出码 1、零输出 - 非根父命令不校验子命令名,`tapd bug comment add` 打印 help 后以退出码 0 返回, AI Agent 误以为评论已发出 修复: - Execute() 改用 ExecuteC,统一以 JSON 输出到 stderr(error=command_error,hint 指向对应命令的 --help) - guardUnknownSubcommands 给无 Run 的父命令补兜底 RunE:有多余参数报 unknown command,否则输出 help; 这类父命令跳过客户端初始化,无凭据时也能正常报错/看 help Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
rootCmd设置了SilenceErrors: true,而Execute()拿到错误后只os.Exit(1)、不打印,导致两类「静默失败」,对 AI Agent 尤其致命(看不到原因,只能猜):RunE直接return err的错误,都只有退出码 1、stdout/stderr 全空。tapd comment add --current-user=xx ...(comment add 没有这个 flag)tapd bug update --id=123 --status=resolved(id 是位置参数)tapd comment list --fields=idtapd bug comment add ...会打印bug的 help 后以退出码 0 返回,Agent 以为评论已发出。我们在实际使用中,几次因为这类问题导致评论丢失、bug 状态没改却被当作已完成。
修复
Execute()改用ExecuteC(),把未处理的错误统一以 JSON 输出到 stderr,格式与现有output.PrintError一致:{"error":"command_error","message":"unknown flag: --id","hint":"Run 'tapd bug update --help' for usage"}guardUnknownSubcommands:给没有Run的父命令补兜底RunE。有多余参数时报unknown command "comment" for "tapd bug",没有参数时照常输出 help。这些父命令通过 Annotation 标记,跳过客户端初始化,所以没有凭据时也能看 help、正常报错。PrintError再os.Exit)不经过这里,不会重复输出。验证
tapd bug update --id=1unknown flag: --idtapd comment add --current-user=x ...unknown flag: --current-usertapd bug updateaccepts 1 arg(s), received 0tapd bug comment addunknown command "comment" for "tapd bug"tapd bug/tapd bug --helptapd bug show <id>等正常命令internal/cmd/root_errors_test.go,覆盖未知子命令、父命令输出 help、已知子命令正常执行、错误 JSON 格式和 hint。go vet ./...、go test -race ./...全部通过。🤖 Generated with Claude Code