Skip to content

Lazy subcommand registration (approved API) - #2901

Open
adamsitnik wants to merge 9 commits into
dotnet:mainfrom
adamsitnik:lazy-symbols-approved-api
Open

adamsitnik wants to merge 9 commits into
dotnet:mainfrom
adamsitnik:lazy-symbols-approved-api

Conversation

@adamsitnik

@adamsitnik adamsitnik commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #2894
Fixes dotnet/runtime#138204

Implements the API approved in dotnet/runtime#138204 (comment):

public partial class Command
{
    public void Add(string name, Func<Command> commandFactory);
    public void Add(string name, ReadOnlySpan<string> aliases, Func<Command> commandFactory);
}

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.Subcommands still returns IList<Command>; enumerating it or using the indexer creates lazy subcommands on demand, so existing code (help, completions) keeps working.

Supersedes #2895: CommandList is now internal and the new Command.Add overloads delegate to CommandList.AddLazy.

Tests

  • All tests in CommandTests now run for both eager and lazy subcommand registration (bool useLazy), without duplicating the test bodies.
  • New tests verify the lazy commands are actually lazy: adding doesn't create them, only matched factories run (also when matching by alias), each factory runs once, and parse errors and --help don't create unmatched subcommands.

Behavior notes

  • Aliases must be passed to Add: a factory that returns a command with aliases throws InvalidOperationException. Until the command is created, the parser only knows the aliases passed to Add. 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.
  • Duplicate aliases passed to Add are 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 returns null and 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.
  • CopyTo validates the destination array before it creates any lazy subcommands.
  • The new behavior is documented in the README.

adamsitnik and others added 4 commits November 20, 2026 12:07
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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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
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.Add overloads 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

Comment thread src/System.CommandLine/CommandList.cs Resolved
Comment thread src/System.CommandLine/CommandList.cs Resolved
Comment thread src/System.CommandLine/CommandList.cs Resolved
Comment thread src/System.CommandLine/CommandList.cs Resolved
Comment thread src/System.CommandLine/CommandList.cs Resolved
Comment thread src/System.CommandLine/CommandList.cs Resolved
adamsitnik and others added 5 commits November 25, 2026 15:28
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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

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!);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Lazy subcommand registration [API Proposal]: Lazy subcommand registration and lookup on System.CommandLine.Command

2 participants