Skip to content

Fix crash when multiple plugins register the same command#1691

Merged
waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:waldekmastykarz/fix-duplicate-plugin-commands
Jun 4, 2026
Merged

Fix crash when multiple plugins register the same command#1691
waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:waldekmastykarz/fix-duplicate-plugin-commands

Conversation

@waldekmastykarz
Copy link
Copy Markdown
Collaborator

Summary

Fixes the ArgumentException: An item with the same key has already been added. Key: mocks crash when using EntraMockResponsePlugin and MockResponsePlugin together, or multiple instances of MockResponsePlugin.

Root cause

EntraMockResponsePlugin inherits from MockResponsePlugin, which registers a "mocks" command via GetCommands(). When both plugins are loaded, two "mocks" commands are registered, causing System.CommandLine to throw. The same crash occurs with two MockResponsePlugin instances using different config sections.

This is a regression introduced in v1.1.0 when GetCommands() was added to MockResponsePlugin (PR #1262).

Fix

Deduplicate plugin commands by name before registering them — matching the existing approach for options (line 589-593). When duplicates are found, the first registration wins and a warning is logged so users and plugin authors aren't left guessing.

Closes #1690

When using EntraMockResponsePlugin and MockResponsePlugin together,
or multiple instances of MockResponsePlugin, the proxy crashes with
ArgumentException due to duplicate 'mocks' command registration.

Deduplicate plugin commands by name (matching the existing approach
for options) and log a warning when duplicates are found.

Closes dotnet#1690

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 4, 2026 13:19
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner June 4, 2026 13:19
@waldekmastykarz waldekmastykarz added the pr-bugfix Fixes a bug label Jun 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a startup crash in Dev Proxy’s CLI when multiple loaded plugins register a command with the same name (e.g., "mocks" from both EntraMockResponsePlugin and MockResponsePlugin). It updates the root DevProxyCommand command registration flow to deduplicate plugin commands by name, preventing System.CommandLine from throwing during tokenization/parsing.

Changes:

  • Collect plugin-provided commands and group them by Command.Name before registration.
  • Register only the first command per name and log a warning when duplicates are detected.

@waldekmastykarz waldekmastykarz merged commit c22743e into dotnet:main Jun 4, 2026
4 checks passed
@waldekmastykarz waldekmastykarz deleted the waldekmastykarz/fix-duplicate-plugin-commands branch June 4, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: When using EntraMockResponsePlugin and MockResponsePlugin it crashes with ArgumentException

3 participants