Skip to content

fix: skip flag-completion registration outside completion path#598

Merged
tuxedomm merged 2 commits intomainfrom
fix/cobra-flag-completion-leak
Apr 22, 2026
Merged

fix: skip flag-completion registration outside completion path#598
tuxedomm merged 2 commits intomainfrom
fix/cobra-flag-completion-leak

Conversation

@tuxedomm
Copy link
Copy Markdown
Collaborator

@tuxedomm tuxedomm commented Apr 22, 2026

Summary

  • Cobra stores every RegisterFlagCompletionFunc entry in a package-global map keyed by *pflag.Flag with no removal path (cobra/completions.go: flagCompletionFunctions). Each entry retains the flag's backing value pointer, which in turn keeps the owning command tree alive after the caller drops its reference.
  • Added cmdutil.RegisterFlagCompletion wrapper with a global switch; routed all seven in-repo call sites through it.
  • cmd/root.go:Execute enables registration only when os.Args contains __complete / completion, matching the existing isCompletionCommand gate already used for update checks.

Impact

Measured over 30 dropped cmd.Build() calls with GC forced between:

mode retained per Build
registration enabled (old behavior) ~202 KB / 2180 objects
registration gated by this fix ~0 KB / 0 objects

Only cmd/service/service.go:192 (--format registered with StringVar(&opts.Format, ...)) transitively pulled *cmdutil.Factory into the global map; the other six sites retained only the flag objects themselves. Both classes are addressed uniformly.

Test plan

  • go test ./internal/cmdutil/ -race — three new tests (switch round-trip, finalizer-based retention check, end-to-end register/lookup).
  • go test ./cmd/... ./shortcuts/common/ -race — no regressions.
  • go build ./... green.
  • Manual CLI smoke:
    • lark-cli --help, lark-cli im --help, lark-cli api --help render as before.
    • lark-cli __complete api --as ""user, bot.
    • lark-cli __complete api --format ""json/ndjson/table/csv.
    • lark-cli __complete im +chat-search --sort-by "" → shortcut enum values.
    • lark-cli __complete auth login --domain "cal" → dynamic calendar completion works.
    • lark-cli completion bash generates the full 426-line script.
    • lark-cli api GET /dummy --dry-run runs normally (completion off path).

Summary by CodeRabbit

  • Refactor
    • Centralized shell flag completion registration and added a global toggle to control when completions are registered (enabled only for completion-related invocations).
  • Tests
    • Added unit tests validating completion enable/disable behavior and ensuring correct registration or absence of flag completions.

Cobra keeps completion callbacks in a package-global map keyed by
*pflag.Flag with no removal path, so registrations made during Build()
outlive the command itself. Route all seven call sites through
cmdutil.RegisterFlagCompletion and enable registration only when the
invocation actually serves a __complete request.

Measured over 30 dropped Builds: ~202 KB / 2180 retained objects per
Build before, ~0 after.

Change-Id: I734d598a4c91a92c33b02e0f292f640cc0e224c6
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Walkthrough

Refactors flag completion registration to a centralized helper and adds a process-wide toggle to enable/disable flag completions; root command now enables completions only for completion-related invocations.

Changes

Cohort / File(s) Summary
Flag completion sites
cmd/api/api.go, cmd/auth/login.go, cmd/schema/schema.go, cmd/service/service.go, internal/cmdutil/identity_flag.go, shortcuts/common/runner.go
Replaced in-place cmd.RegisterFlagCompletionFunc(...) calls with cmdutil.RegisterFlagCompletion(cmd, "<flag>", ...). Completion callbacks and candidates unchanged.
Completion control infrastructure
internal/cmdutil/completion.go, internal/cmdutil/completion_test.go
Added atomic toggle flagCompletionsDisabled, accessors SetFlagCompletionsDisabled/FlagCompletionsDisabled, and RegisterFlagCompletion which conditionally no-ops. Added tests exercising toggle and registration behavior.
Root gating & tests
cmd/root.go, cmd/root_test.go
Added configureFlagCompletions(os.Args) called from Execute() to set the global toggle based on whether invocation is a shell-completion request; added unit tests for the gating logic.
Shortcut mount tests
shortcuts/common/runner_flag_completion_test.go
Added tests asserting that shortcut mounting registers (or does not register) flag completions depending on the global toggle.

Sequence Diagram(s)

sequenceDiagram
    participant CLI as CLI (os.Args)
    participant Root as cmd/root.Execute
    participant Config as configureFlagCompletions
    participant CmdUtil as internal/cmdutil
    participant Cobra as cobra.Command

    CLI->>Root: start Execute()
    Root->>Config: configureFlagCompletions(args)
    Config->>CmdUtil: SetFlagCompletionsDisabled(!isCompletionCommand)
    note right of CmdUtil: atomic toggle set
    Root->>Cobra: build commands and register flags
    Cobra->>CmdUtil: RegisterFlagCompletion(cmd, flag, fn)
    alt completions enabled
        CmdUtil->>Cobra: cmd.RegisterFlagCompletionFunc(flag, fn)
    else completions disabled
        CmdUtil-->>Cobra: no-op (skip registration)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • liangshuo-1
  • liuxinyanglxy

Poem

🐰
I hopped through flags both near and far,
Collected completions in one neat jar,
A toggle here, a helper there,
Now tab-complete breathes easier air,
Hooray — I nibbled the ribbon of par!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 70.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: skipping flag-completion registration outside completion paths to prevent memory leaks.
Description check ✅ Passed The description includes a comprehensive Summary explaining the memory leak problem, detailed Changes listing the main modifications and impact metrics, and a thorough Test Plan documenting verification steps with checkmarks.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cobra-flag-completion-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the size/L Large or sensitive change across domains or core paths label Apr 22, 2026
liangshuo-1
liangshuo-1 previously approved these changes Apr 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 65.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.81%. Comparing base (f369929) to head (dce3c93).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
cmd/root.go 50.00% 2 Missing ⚠️
cmd/api/api.go 0.00% 0 Missing and 1 partial ⚠️
cmd/auth/login.go 0.00% 0 Missing and 1 partial ⚠️
cmd/schema/schema.go 0.00% 0 Missing and 1 partial ⚠️
cmd/service/service.go 0.00% 0 Missing and 1 partial ⚠️
internal/cmdutil/identity_flag.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #598      +/-   ##
==========================================
+ Coverage   59.75%   59.81%   +0.06%     
==========================================
  Files         403      404       +1     
  Lines       42513    42526      +13     
==========================================
+ Hits        25403    25439      +36     
+ Misses      15105    15082      -23     
  Partials     2005     2005              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 22, 2026

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@dce3c931cbe8fa1a504e023b6f432d896f5d7fc3

🧩 Skill update

npx skills add larksuite/cli#fix/cobra-flag-completion-leak -y -g

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cmd/root.go (1)

151-158: ⚠️ Potential issue | 🟠 Major

Include Cobra's no-description completion request command.

Cobra uses both __complete and __completeNoDesc for shell completion flows (set as primary command and alias). The helper currently checks only for __complete, which disables flag completions for the no-description path, so custom flag completions (e.g., --format, --as) can be missing. Use Cobra's exported constants instead of string literals.

🐛 Proposed fix
 func isCompletionCommand(args []string) bool {
 	for _, arg := range args {
-		if arg == "completion" || arg == "__complete" {
+		if arg == "completion" ||
+			arg == cobra.ShellCompRequestCmd ||
+			arg == cobra.ShellCompNoDescRequestCmd {
 			return true
 		}
 	}
 	return false
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` around lines 151 - 158, The isCompletionCommand helper only
checks literal "__complete" and "completion", missing Cobra's no-description
completion; update isCompletionCommand to detect Cobra's exported constants
(cobra.BashCompRequestCmd and cobra.BashCompNoDescRequestCmd) instead of string
literals so both completion paths are recognized and custom flag completions
(e.g., --format, --as) are enabled; reference the existing isCompletionCommand
function and replace the string checks with comparisons to
cobra.BashCompRequestCmd and cobra.BashCompNoDescRequestCmd.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/cmdutil/completion_test.go`:
- Around line 39-56: The test must assert that no Cobra registration occurred by
either capturing cmd in the completion callback or performing a negative lookup;
modify the test around RegisterFlagCompletion so the closure captures the
specific cmd (e.g., use func(c *cobra.Command) { ... } capturing cmd) and have
the callback reference cmd (or call cmd.Flags().Lookup("foo") after
RegisterFlagCompletion and assert it is nil), keep the
runtime.SetFinalizer(collected.Add) logic intact and then assert
collected.Load() == N to ensure the finalizers only fire when completions are
disabled; update the test body that calls RegisterFlagCompletion, the completion
callback, and the negative lookup to guarantee the regression would fail.

---

Outside diff comments:
In `@cmd/root.go`:
- Around line 151-158: The isCompletionCommand helper only checks literal
"__complete" and "completion", missing Cobra's no-description completion; update
isCompletionCommand to detect Cobra's exported constants
(cobra.BashCompRequestCmd and cobra.BashCompNoDescRequestCmd) instead of string
literals so both completion paths are recognized and custom flag completions
(e.g., --format, --as) are enabled; reference the existing isCompletionCommand
function and replace the string checks with comparisons to
cobra.BashCompRequestCmd and cobra.BashCompNoDescRequestCmd.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bd50fe19-98d9-4b3e-926d-86cd9d927c9d

📥 Commits

Reviewing files that changed from the base of the PR and between f369929 and 5c279d7.

📒 Files selected for processing (9)
  • cmd/api/api.go
  • cmd/auth/login.go
  • cmd/root.go
  • cmd/schema/schema.go
  • cmd/service/service.go
  • internal/cmdutil/completion.go
  • internal/cmdutil/completion_test.go
  • internal/cmdutil/identity_flag.go
  • shortcuts/common/runner.go

Comment thread internal/cmdutil/completion_test.go
- Extract configureFlagCompletions helper in cmd/root.go so the gate
  between normal invocations and __complete requests can be unit-tested.
- Add TestConfigureFlagCompletions with table cases for plain commands,
  --help, __complete, and the completion subcommand.
- Add TestShortcutMount_FlagCompletions{Registered,Disabled} in
  shortcuts/common to exercise the two RegisterFlagCompletion call
  sites in registerShortcutFlagsWithContext (enum flag and --format).

Change-Id: Idcb302bb40045b1c5b34580ec90d586eae8b8707
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cmd/root.go (1)

149-162: ⚠️ Potential issue | 🟠 Major

Include Cobra's __completeNoDesc request in the completion gate.

The isCompletionCommand function only checks for "completion" and "__complete", but Cobra v1.10.2 also invokes a __completeNoDesc path for no-description completion scripts. Without handling this, users with no-description completions will have custom flag completions disabled. Use Cobra's exported constants to align with the dependency API.

The test coverage at cmd/root_test.go TestConfigureFlagCompletions is also missing a case for the __completeNoDesc request.

Proposed fix
 func isCompletionCommand(args []string) bool {
 	for _, arg := range args {
-		if arg == "completion" || arg == "__complete" {
+		switch arg {
+		case "completion", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
 			return true
 		}
 	}
 	return false
 }

Add test case to TestConfigureFlagCompletions:

 		{"__complete request", []string{"__complete", "im", "+send", ""}, false},
+		{"__completeNoDesc request", []string{"__completeNoDesc", "im", "+send", ""}, false},
 		{"completion subcommand", []string{"completion", "bash"}, false},
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` around lines 149 - 162, The completion gate currently only
checks for "completion" and "__complete" in isCompletionCommand, missing Cobra's
no-description completion; update isCompletionCommand to also check Cobra's
exported no-desc constant (use cobra.ShellCompRequestCmd and the corresponding
no-desc constant such as cobra.ShellCompRequestNoDesc or the exact exported name
in your Cobra version) instead of hardcoded strings, and ensure
configureFlagCompletions still calls
cmdutil.SetFlagCompletionsDisabled(!isCompletionCommand(args)); also add a
TestConfigureFlagCompletions unit test case that passes the __completeNoDesc
request (using the same Cobra constant) to verify custom flag completions are
enabled for that request.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@cmd/root.go`:
- Around line 149-162: The completion gate currently only checks for
"completion" and "__complete" in isCompletionCommand, missing Cobra's
no-description completion; update isCompletionCommand to also check Cobra's
exported no-desc constant (use cobra.ShellCompRequestCmd and the corresponding
no-desc constant such as cobra.ShellCompRequestNoDesc or the exact exported name
in your Cobra version) instead of hardcoded strings, and ensure
configureFlagCompletions still calls
cmdutil.SetFlagCompletionsDisabled(!isCompletionCommand(args)); also add a
TestConfigureFlagCompletions unit test case that passes the __completeNoDesc
request (using the same Cobra constant) to verify custom flag completions are
enabled for that request.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3a9fdd88-f28e-4709-967a-a37b50030215

📥 Commits

Reviewing files that changed from the base of the PR and between 5c279d7 and dce3c93.

📒 Files selected for processing (3)
  • cmd/root.go
  • cmd/root_test.go
  • shortcuts/common/runner_flag_completion_test.go

@tuxedomm tuxedomm merged commit 11191df into main Apr 22, 2026
21 checks passed
@tuxedomm tuxedomm deleted the fix/cobra-flag-completion-leak branch April 22, 2026 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Large or sensitive change across domains or core paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants