Skip to content

Migrate DotnetToolResource to use RequiredCommandValidator#15267

Open
afscrome wants to merge 2 commits intomicrosoft:mainfrom
afscrome:dotnettool-requiredcommand
Open

Migrate DotnetToolResource to use RequiredCommandValidator#15267
afscrome wants to merge 2 commits intomicrosoft:mainfrom
afscrome:dotnettool-requiredcommand

Conversation

@afscrome
Copy link
Contributor

Description

  • Migrated dotnet tool version validation to use RequiredCommandValidator
  • Updated RequiredCommandValidator to include the callback in the cache key, so that resources for the same tool name, but different validation callbacks have the correct callback executed.

Fixes #14304. This is a second attempt of #15265 - changing the base branch seemed to confuse the test automation.

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
  • Did you add public API?
    • No
  • Does the change make any security assumptions or guarantees?
    • No
  • Does the change require an update in our Aspire docs?
    • No

@afscrome afscrome requested a review from mitchdenny as a code owner March 15, 2026 23:16
Copilot AI review requested due to automatic review settings March 15, 2026 23:16
@github-actions
Copy link
Contributor

github-actions bot commented Mar 15, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15267

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15267"

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 15, 2026
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 migrates DotnetToolResource’s .NET SDK version validation to run through the shared RequiredCommandValidator, and adjusts the validator’s caching so different validation callbacks don’t incorrectly share cached results (addressing #14304).

Changes:

  • DotnetToolResourceExtensions now registers a WithRequiredCommand("dotnet", ...) callback instead of performing the SDK check in OnBeforeResourceStarted.
  • RequiredCommandValidator cache key is expanded to include the validation callback (command + callback).
  • Tests updated to validate caching behavior across callbacks and that dotnet tool resources emit a required-command annotation.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/Aspire.Hosting.Tests/RequiredCommandAnnotationTests.cs Adds coverage for caching behavior when callbacks are the same vs different.
tests/Aspire.Hosting.DotnetTool.Tests/AddDotnetToolTests.cs Replaces direct SDK version validation tests with an annotation presence test.
src/Aspire.Hosting/DotnetToolResourceExtensions.cs Moves SDK version validation into a WithRequiredCommand validation callback.
src/Aspire.Hosting/ApplicationModel/RequiredCommandValidator.cs Changes cache key to include the callback to avoid cross-callback caching.

Comment on lines +186 to +192
private readonly record struct CommandValidationCacheKey(string command, Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>>? callback)
{
public string Command { get; } = command;

public Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>>? Callback { get; } = callback;
}

Comment on lines 381 to +385
[Fact]
public void ValidateDotnetSdkVersion_WithNullVersion_DoesNotThrow()
public void AddDotnetTool_IncludesRequiredCommandAnnotation()
{
// Should not throw - null is treated as "unable to determine version"
DotnetToolResourceExtensions.ValidateDotnetSdkVersion(null, "");
var builder = DistributedApplication.CreateBuilder();
var tool = builder.AddDotnetTool("mytool", "dotnet-ef");
Comment on lines +24 to +25
// Track validation state per command/callback pair to coalesce notifications and validation work.
private readonly ConcurrentDictionary<CommandValidationCacheKey, CommandValidationState> _commandStates = new();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For linux & mac, I think case insensitive is actually correct here as dotnet and DOTNET are different executables.

@eerhardt
Copy link
Member

eerhardt commented Mar 18, 2026

I don't think this is critical for 13.2. @afscrome - can you retarget this to main?

@joperezr
Copy link
Member

Thanks a bunch for the change @afscrome! Given 13.2.0 is about to get released, we've bumped the bug bar criteria this week, and unfortunately that means it is a bit late to take this in for next week. As @eerhardt suggests, can we retarget this to main and we can backport it to the release branch after next week if we think this is something we should service in 13.2.1? Thanks again and sorry for the trouble!

@afscrome afscrome changed the base branch from release/13.2 to main March 21, 2026 09:05
@afscrome afscrome closed this Mar 21, 2026
@afscrome afscrome reopened this Mar 21, 2026
- Migrated `dotnet tool` version validation to use `RequiredCommandValidator`
- Updated `RequiredCommandValidator` to include the callback in teh cache key, so that dotnet tools resources with different working directories are evaluated separately.
@afscrome afscrome force-pushed the dotnettool-requiredcommand branch from 809b58d to 0138079 Compare March 21, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should .Net 10 sdk checks be moved to WithRequiredCommand

4 participants