Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 9, 2025

Fix: Add NuGet.config creation for solution initialization in aspire init

Problem

When aspire init is used with a solution file, it doesn't create/update a NuGet.config file. The single-file AppHost path already does this correctly via PromptToCreateOrUpdateNuGetConfigAsync, but the solution path skips this step.

Solution

Created a shared NuGetConfigPrompter helper class to eliminate code duplication between InitCommand and DotNetTemplateFactory, ensuring consistent NuGet.config creation behavior across both commands.

Changes Made

1. Created NuGetConfigPrompter helper class

File: src/Aspire.Cli/Packaging/NuGetConfigPrompter.cs

  • Extracts NuGet.config prompting logic into a reusable component
  • Provides two methods:
    • PromptToCreateOrUpdateAsync: Prompts user before creating/updating (for in-place creation)
    • CreateOrUpdateWithoutPromptAsync: Creates/updates without prompting (for subdirectory creation)
  • Handles both explicit channel cases (preview, daily builds) and implicit channels (default NuGet.org)

2. Updated InitCommand to use the shared helper

File: src/Aspire.Cli/Commands/InitCommand.cs

  • Removed duplicate CreateOrUpdateNuGetConfigAsync method
  • Uses NuGetConfigPrompter.PromptToCreateOrUpdateAsync for solution directory
  • Maintains existing behavior with user prompting

3. Refactored DotNetTemplateFactory to use the shared helper

File: src/Aspire.Cli/Templating/DotNetTemplateFactory.cs

  • Simplified PromptToCreateOrUpdateNuGetConfigAsync to use NuGetConfigPrompter
  • Removed duplicate logic for checking config existence and prompting
  • Removed unused TryFindNuGetConfigInDirectory helper method
  • Maintains existing behavior for both in-place and subdirectory creation

Benefits

  • ✅ Eliminates code duplication between InitCommand and DotNetTemplateFactory
  • ✅ Ensures consistent NuGet.config generation logic across aspire init and aspire new commands
  • ✅ Easier to maintain and update NuGet.config prompting behavior in one place
  • ✅ All 423 existing tests pass
  • ✅ Build succeeds with no warnings

Testing

  • ✅ All 423 CLI tests pass
  • ✅ Build succeeds with no warnings
  • ✅ Behavior is identical to previous implementation
Original prompt

When the aspire init command is used to initialize a project that has a solution file we need to drop a nuget.config file. The single file path already does this we need to replicate this behavior.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Add nuget.config file on project initialization Add NuGet.config creation to solution initialization in aspire init Oct 9, 2025
@Copilot Copilot AI requested a review from mitchdenny October 9, 2025 08:38
Copilot finished work on behalf of mitchdenny October 9, 2025 08:38
Copy link
Contributor

github-actions bot commented Oct 9, 2025

🚀 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/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 11921

Or

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

@mitchdenny
Copy link
Member

@copilot this change duplicates some logic that already exists in DotNetTempalteFactory, specifically the PromptToCreateOrUpdateNuGetConfigAsync method. Refactor this method into a helper class that can be instansiated and used from both the DotNetTemplateFactory and the InitCommand so that we can be sure that the NuGet.config generation logic is consistently applied across both the aspire init and aspire new commands.

Copy link
Contributor Author

Copilot AI commented Oct 9, 2025

@copilot this change duplicates some logic that already exists in DotNetTempalteFactory, specifically the PromptToCreateOrUpdateNuGetConfigAsync method. Refactor this method into a helper class th...

Refactored the NuGet.config prompting logic into a new NuGetConfigPrompter helper class in the Packaging namespace. Both InitCommand and DotNetTemplateFactory now use this shared class to ensure consistent behavior. The prompter supports two modes:

  1. PromptToCreateOrUpdateAsync - Prompts user before creating/updating (used for in-place creation)
  2. CreateOrUpdateWithoutPromptAsync - Creates/updates without prompting (used for subdirectory creation)

All duplicate code has been removed, and all 423 tests pass. Changes in commit e5e0c2f.

Copilot finished work on behalf of mitchdenny October 9, 2025 12:50
@mitchdenny mitchdenny force-pushed the copilot/add-nuget-config-file branch from ebe72ea to 872cc30 Compare October 13, 2025 02:06
@mitchdenny mitchdenny marked this pull request as ready for review October 13, 2025 03:52
Copy link
Contributor

@Copilot 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

Adds NuGet.config creation/prompting to solution-based aspire init and centralizes the logic to remove duplication with aspire new.

  • Introduces NuGetConfigPrompter to encapsulate create/update and prompting behavior.
  • Refactors DotNetTemplateFactory and InitCommand to use the shared helper.
  • Adds solution init call to ensure explicit channel sources are offered when initializing into an existing solution directory.

Reviewed Changes

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

File Description
src/Aspire.Cli/Packaging/NuGetConfigPrompter.cs New helper class encapsulating NuGet.config prompting and creation logic.
src/Aspire.Cli/Templating/DotNetTemplateFactory.cs Replaces inline NuGet.config logic with NuGetConfigPrompter usage; removes duplicated helper.
src/Aspire.Cli/Commands/InitCommand.cs Adds NuGet.config prompting during solution init and a wrapper method delegating to NuGetConfigPrompter.

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.

2 participants