Repository navigation
Lazy subcommand registration (approved API) - #2901
Open
adamsitnik wants to merge 9 commits into
Open
adamsitnik wants to merge 9 commits into
adamsitnik wants to merge 9 commits into
Conversation
Command.Subcommands is now a public CommandList that supports registering subcommands lazily. The parser creates only the subcommands that were matched. Co-authored-by: Copilot <[email protected]> Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
Co-authored-by: Copilot <[email protected]> Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
Co-authored-by: Copilot <[email protected]> Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
- Add Command.Add(string, Func<Command>) and Command.Add(string, ReadOnlySpan<string>, Func<Command>) that delegate to CommandList.AddLazy - Make CommandList internal, Subcommands returns IList<Command> again - Run all CommandTests for both eager and lazy subcommand registration, add laziness tests Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
There was a problem hiding this comment.
🟡 Changes recommended
A lazy-command factory can reenter its own slot and cause a process-terminating stack overflow, alongside unresolved alias, lookup, initialization, and collection-contract regressions.
7 open issues
Detect recursive lazy factory initialization · New Deduplicate aliases before storing registrations · New Validate CopyTo arguments before writing or materializing entries · New Skip unpublished entries in lookup operations · New Cache factory initialization failures · New Reject aliases not declared by lazy registrations · New Preserve names for uncreated lazy commands · New
What changed in this PR
Adds the approved lazy subcommand registration API to reduce upfront allocation in large command trees while preserving the public IList<Command> interface.
Changes:
- Adds two factory-based
Command.Addoverloads backed by an internal lazy collection. - Updates tokenization and result lookup to avoid constructing unmatched subcommands.
- Expands eager/lazy tests, adds allocation benchmarks, and updates the API approval baseline.
| File | Description |
|---|---|
src/System.CommandLine/Parsing/SymbolResultTree.cs |
Skips uncreated subcommands when indexing result names. |
src/System.CommandLine/Parsing/StringExtensions.cs |
Registers deferred command tokens and materializes matched commands. |
src/System.CommandLine/CommandList.cs |
Implements mixed eager and lazy subcommand storage. |
src/System.CommandLine/Command.cs |
Exposes the approved lazy registration overloads. |
src/System.CommandLine.Tests/CommandTests.cs |
Covers eager/lazy behavior, factory invocation, aliases, and help. |
src/System.CommandLine.Benchmarks/CommandLine/Perf_LazySubcommands.cs |
Compares eager and lazy tree construction and parsing. |
src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt |
Records the two new public overloads. |
Review effort: Balanced
Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
Aliases of lazy subcommands must be specified when the command is added, as aliases defined by the factory can't be used for routing before the command is created. Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
Co-authored-by: Copilot <[email protected]> Copilot-Session: 22b4ede8-e49e-440c-8074-a85a012afabf
There was a problem hiding this comment.
🟡 Changes recommended
The name cache can become stale when lazy siblings are materialized after an initial lookup, causing later option lookups to throw instead of returning null; cache rebuilding and a regression test are needed.
1 open issue
7 resolved since last review
Detect recursive lazy factory initialization Preserve names for uncreated lazy commands Reject aliases not declared by lazy registrations Cache factory initialization failures Skip unpublished entries in lookup operations Validate CopyTo arguments before writing or materializing entries Deduplicate aliases before storing registrations
Review effort: Balanced
| throw new ArgumentException($"No symbol result found with name \"{name}\"."); | ||
| // The name is either unknown (e.g. a typo) or belongs to a lazy subcommand that was not created yet | ||
| // or to one of its descendants. Misses are rare, so creating the lazy subcommands here is acceptable. | ||
| AddUncreatedSubcommands(_rootCommand!); |
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.


Fixes #2894
Fixes dotnet/runtime#138204
Implements the API approved in dotnet/runtime#138204 (comment):
The parser registers only the names and aliases of lazy subcommands and creates just the subcommands that were actually matched, so unused parts of a large symbol tree are never allocated.
Command.Subcommandsstill returnsIList<Command>; enumerating it or using the indexer creates lazy subcommands on demand, so existing code (help, completions) keeps working.Supersedes #2895:
CommandListis now internal and the newCommand.Addoverloads delegate toCommandList.AddLazy.Tests
CommandTestsnow run for both eager and lazy subcommand registration (bool useLazy), without duplicating the test bodies.--helpdon't create unmatched subcommands.Behavior notes
Add: a factory that returns a command with aliases throwsInvalidOperationException. Until the command is created, the parser only knows the aliases passed toAdd. If the factory set aliases too, they would only work after the command had been created by something else, and would be silently ignored otherwise. Throwing turns that hidden bug into a clear error.Addare ignored, the same as for eagerly created commands.GetResult(string)/GetValue<T>(string)behave the same as for eager commands: a name of a symbol from an uncreated (unselected) lazy subcommand returnsnulland an unknown name throws. Names found among created commands (the common case) still create nothing; only a miss creates the uncreated lazy subcommands to tell the two cases apart.CopyTovalidates the destination array before it creates any lazy subcommands.