-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(cmdutil): default flag completions to disabled #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| ) | ||
|
|
||
| // TestBuild_DefaultNoCompletionLeak verifies that, without any call to | ||
| // SetFlagCompletionsEnabled, repeated cmd.Build invocations do not leak | ||
| // *cobra.Command instances into cobra's package-global flag-completion map. | ||
| // | ||
| // This guards the new default (completions disabled) — if someone flips the | ||
| // zero-value back to "enabled", the per-Build memory growth observed under | ||
| // `scripts/bench_build` would resurface in production hot paths that build | ||
| // the root command without serving a completion request. | ||
| func TestBuild_DefaultNoCompletionLeak(t *testing.T) { | ||
| if cmdutil.FlagCompletionsEnabled() { | ||
| t.Fatalf("precondition: FlagCompletionsEnabled() = true, want false (state polluted by another test)") | ||
| } | ||
|
|
||
| snap := func() (heapMB float64, objs uint64) { | ||
| runtime.GC() | ||
| runtime.GC() | ||
| runtime.GC() | ||
| var m runtime.MemStats | ||
| runtime.ReadMemStats(&m) | ||
| return float64(m.HeapAlloc) / 1024 / 1024, m.HeapObjects | ||
| } | ||
|
|
||
| // Warm one-time caches (registry JSON decode, embed reads) so the first | ||
| // Build's lazy allocations don't skew the per-iteration delta. | ||
| _ = Build(context.Background(), cmdutil.InvocationContext{}) | ||
| baseMB, baseObj := snap() | ||
|
|
||
| const N = 20 | ||
| for range N { | ||
| _ = Build(context.Background(), cmdutil.InvocationContext{}) | ||
| } | ||
| mb, obj := snap() | ||
|
|
||
| deltaMB := mb - baseMB | ||
| deltaObj := int64(obj) - int64(baseObj) | ||
| perBuildKB := deltaMB * 1024 / float64(N) | ||
| perBuildObj := deltaObj / int64(N) | ||
|
|
||
| t.Logf("%d builds: +%.2f MB, +%d objects (%.1f KB/build, %d objs/build)", | ||
| N, deltaMB, deltaObj, perBuildKB, perBuildObj) | ||
|
|
||
| // With completions disabled (the default), per-Build retained growth | ||
| // should be minimal. Threshold is conservative: the previously observed | ||
| // leak with completions enabled was ~hundreds of KB and thousands of | ||
| // objects per Build, well above this bound. | ||
| const maxKBPerBuild = 50.0 | ||
| const maxObjsPerBuild = 500 | ||
| if perBuildKB > maxKBPerBuild { | ||
| t.Errorf("per-build heap growth = %.1f KB, want <= %.1f KB (completion registration may be leaking)", perBuildKB, maxKBPerBuild) | ||
| } | ||
| if perBuildObj > maxObjsPerBuild { | ||
| t.Errorf("per-build object growth = %d, want <= %d", perBuildObj, maxObjsPerBuild) | ||
| } | ||
| } | ||
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.