Skip to content

Fix DNX command casting error with DnxCommandDefinition#52820

Merged
marcpopMSFT merged 4 commits intorelease/10.0.2xxfrom
copilot/fix-dotnet-tool-execution-error
Feb 5, 2026
Merged

Fix DNX command casting error with DnxCommandDefinition#52820
marcpopMSFT merged 4 commits intorelease/10.0.2xxfrom
copilot/fix-dotnet-tool-execution-error

Conversation

Copy link
Contributor

Copilot AI commented Feb 3, 2026

The dnx command throws a casting exception when executing tools:

Unable to cast object of type 'Microsoft.DotNet.Cli.Commands.Dnx.DnxCommandDefinition' 
to type 'Microsoft.DotNet.Cli.Commands.Tool.Execute.ToolExecuteCommandDefinition'.

Changes

ToolExecuteCommand was parameterized with CommandBase<ToolExecuteCommandDefinition>, but DnxCommandParser creates a parse result containing DnxCommandDefinition. Both types inherit from ToolExecuteCommandDefinitionBase, which defines all arguments/options the command uses.

Changed ToolExecuteCommand to use the base class:

-internal sealed class ToolExecuteCommand : CommandBase<ToolExecuteCommandDefinition>
+internal sealed class ToolExecuteCommand : CommandBase<ToolExecuteCommandDefinitionBase>

This allows the command to work with both DnxCommandDefinition (dnx) and ToolExecuteCommandDefinition (tool execute/exec).

Tests Added

Added parameterized tests to ensure both dnx and exec commands work correctly:

  • Converted InstallAndRunToolFromAnyRid from [Fact] to [Theory] with test cases for both "exec" and "dnx" commands
  • Converted InstallAndRunToolFromAnyRidWhenOtherRidsArePresentButIncompatible from [Fact] to [Theory] with test cases for both "exec" and "dnx" commands

These tests verify that both command variants function correctly with the same tool execution logic.

Original prompt

This section details on the original issue you should resolve

<issue_title>DNX: "Unable to cast object of type 'Microsoft.DotNet.Cli.Commands.Dnx.DnxCommandDefinition ...' error while running dotnet tool</issue_title>
<issue_description>With SDK 10.0.200-preview.0.26102.104, the dnx command fails to run a dotnet tool with the following error:

C:\>dotnet --version
10.0.200-preview.0.26102.104

C:\>dnx dotnetsay
Unhandled exception: Unable to cast object of type 'Microsoft.DotNet.Cli.Commands.Dnx.DnxCommandDefinition' to type 'Microsoft.DotNet.Cli.Commands.Tool.Execute.ToolExecuteCommandDefinition'.

</issue_description>

Comments on the Issue (you are @copilot in this section)

@anvillan FYI @tlmii

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unable to cast object error in dotnet tool Fix DNX command casting error with DnxCommandDefinition Feb 4, 2026
Copilot AI requested a review from marcpopMSFT February 4, 2026 00:08
@marcpopMSFT marcpopMSFT marked this pull request as ready for review February 4, 2026 00:09
Copilot AI review requested due to automatic review settings February 4, 2026 00:09
Copy link
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 casting exception in the dnx command by changing ToolExecuteCommand to use the base class ToolExecuteCommandDefinitionBase instead of the concrete ToolExecuteCommandDefinition class.

Changes:

  • Modified ToolExecuteCommand to accept ToolExecuteCommandDefinitionBase instead of ToolExecuteCommandDefinition, allowing it to work with both DnxCommandDefinition and ToolExecuteCommandDefinition

@baronfel
Copy link
Member

baronfel commented Feb 4, 2026

@copilot there should be tests for the tool exec command - these tests should be updated to be parameterized to make sure that dnx works as well.

Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 4, 2026

@copilot there should be tests for the tool exec command - these tests should be updated to be parameterized to make sure that dnx works as well.

Added parameterized tests for both dnx and exec commands in cb85019. The tests InstallAndRunToolFromAnyRid and InstallAndRunToolFromAnyRidWhenOtherRidsArePresentButIncompatible now verify that both commands work correctly.

Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Copilot AI requested a review from baronfel February 4, 2026 00:34
@marcpopMSFT
Copy link
Member

Tested the fix in a codespace and looks good to me. @tmat I think #52162 broke this. What are the likelihood that there are other breaks from this refactor? We map dnx to tool exec so do we have any other similar mappings we should double check work? I don't think so within the workload or tooling space.

@tmat
Copy link
Member

tmat commented Feb 4, 2026

The only reasonable way is to check if we have code coverage.

@marcpopMSFT marcpopMSFT merged commit d74666a into release/10.0.2xx Feb 5, 2026
24 checks passed
@marcpopMSFT marcpopMSFT deleted the copilot/fix-dotnet-tool-execution-error branch February 5, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants