fix(cmdutil): default flag completions to disabled#688
Conversation
The previous default (atomic.Bool zero-value = enabled) meant any *cobra.Command built without first calling configureFlagCompletions leaked into cobra's package-global flagCompletionFunctions map. Bench runs (scripts/bench_build) showed hundreds of KB and thousands of objects retained per Build call. Flip the semantics so the zero-value matches the safe default: - Rename internal var to flagCompletionsEnabled (zero = disabled). - Rename public API to SetFlagCompletionsEnabled / FlagCompletionsEnabled. - Update call sites in cmd/root.go and scripts/bench_build/main.go. - Add cmd.TestBuild_DefaultNoCompletionLeak: asserts that, with no setter call at all, repeated cmd.Build invocations stay under 50 KB and 500 objects per build (observed: ~0.7 KB, 3 objs/build). This closes the gap that let the wrong default ship — every previous test explicitly Set the switch before exercising it. Change-Id: Ifefb04af5fd45eea9676a344a64ad071b6a4cd1a
📝 WalkthroughWalkthroughThis PR inverts the flag-completion control API from a negative ("disabled") model to a positive ("enabled") model, replacing Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #688 +/- ##
=======================================
Coverage 62.92% 62.92%
=======================================
Files 484 484
Lines 41522 41522
=======================================
Hits 26127 26127
Misses 13117 13117
Partials 2278 2278 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cmd/build_memstats_test.go`:
- Around line 22-25: In TestBuild_DefaultNoCompletionLeak, make the test isolate
CLI config state by creating a temporary directory (via os.MkdirTemp), setting
the environment variable LARKSUITE_CLI_CONFIG_DIR to that temp dir with
os.Setenv before calling cmdutil.FlagCompletionsEnabled(), and defer restoring
the previous LARKSUITE_CLI_CONFIG_DIR value and removing the temp dir; this
ensures FlagCompletionsEnabled() observes an isolated config and keeps memstats
deterministic.
🪄 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: 100f303c-58c0-4e7d-95e0-0cb569858780
📒 Files selected for processing (6)
cmd/build_memstats_test.gocmd/root.gocmd/root_test.gointernal/cmdutil/completion.gointernal/cmdutil/completion_test.goshortcuts/common/runner_flag_completion_test.go
🚀 PR Preview Install Guide🧰 CLI updatenpm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@ba30be788c5e3a5d4beb8ba93997b1eb4506e92a🧩 Skill updatenpx skills add larksuite/cli#fix/flag-completions-default-disabled -y -g |
Summary
flagCompletionsDisabledwasfalse(i.e. "enabled"), so any*cobra.Commandbuilt beforeconfigureFlagCompletionsran got registered into cobra's process-globalflagCompletionFunctionsmap and never freed.scripts/bench_buildmeasured hundreds of KB and thousands of retained objects percmd.Build.flagCompletionsEnabled(zero = disabled) and the public API toSetFlagCompletionsEnabled/FlagCompletionsEnabled. Noinit()needed. The API isinternal/-only, so the rename blast radius is contained.cmd/root.go::configureFlagCompletions,scripts/bench_build/main.go, and the affected tests.cmd.TestBuild_DefaultNoCompletionLeak: with no setter call at all, runcmd.Build20 times and assert ≤ 50 KB / 500 objects per build (observed: ~0.7 KB / 3 objs). This closes the gap that let the wrong default ship — every previous test set the switch explicitly before exercising it, so the default value itself was never covered.Test plan
go test -race ./internal/cmdutil/ ./cmd/ ./shortcuts/common/go test -run TestBuild_DefaultNoCompletionLeak -v ./cmd/go build ./...