Skip to content

Add user-friendly error handling for aspire new when directory contains existing files #10496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 19, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 17, 2025

Summary

Fixes #9685 by replacing the long, unfriendly error output from dotnet new with a clear, actionable message when users attempt to create a new Aspire project in a directory that already contains files.

Problem

When running aspire new in a directory that already contains files from a previous project, the command would fail with exit code 73 and display a long, technical error message from dotnet new that mentioned the unsupported --force option. This was confusing for users since the aspire CLI doesn't support the --force flag.

Before:

Creating this template will make changes to existing files:
  Overwrite   Program.cs

To create the template anyway, run the command with '--force' option:
   dotnet new console --name TestConsole2 --output . --force

For details on the exit code, refer to https://aka.ms/templating-exit-codes#73

Solution

Added custom error handling that detects exit code 73 from dotnet new and displays a user-friendly message instead.

After:

The output folder already contains files from a previous project. Please remove or move these files before creating a new project in this location.

Changes Made

Core Implementation

  • Added ProjectAlreadyExistsException: New custom exception in Aspire.Cli.Exceptions namespace
  • Enhanced DotNetCliRunner.NewProjectAsync: Modified to detect exit code 73 from dotnet new and throw the custom exception with reference to issue aspire new asks you to use --force on dotnet new if the folder already exists. #9685
  • Enhanced DotNetTemplateFactory.ApplyTemplateAsync: Added try-catch block to handle ProjectAlreadyExistsException and display the user-friendly error message

Resources

  • Added resource string: New ProjectAlreadyExists string in TemplatingStrings.resx with the user-friendly message
  • Updated Designer.cs: Added corresponding property for the new resource string

Testing

  • Added end-to-end test: NewCommandWithExitCode73ShowsUserFriendlyError verifies the complete flow returns the correct exit code
  • Added unit test: NewProjectAsyncThrowsProjectAlreadyExistsExceptionOnExitCode73 verifies DotNetCliRunner throws the correct exception on exit code 73
  • All 112 CLI tests pass: No regressions introduced

Technical Details

The implementation is surgical and minimal:

  1. Detection: DotNetCliRunner.NewProjectAsync checks if dotnet new returns exit code 73
  2. Exception: Throws ProjectAlreadyExistsException instead of returning the exit code
  3. Handling: DotNetTemplateFactory.ApplyTemplateAsync catches the exception and displays the friendly message
  4. Fallback: All other exit codes continue to work exactly as before

Verification

Functionality verified: Confirmed dotnet new returns exit code 73 when attempting to create templates in directories with existing files
No regressions: All existing CLI functionality preserved
Error handling: Custom exception properly thrown and caught
User experience: Clear, actionable error message displayed

The change provides users with clear guidance on how to resolve the issue (remove or move existing files) without mentioning unsupported options.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • go.microsoft.com

If you need me to access, download, or install something from one of these locations, you can either:

This pull request was created as a result of the following prompt from Copilot chat.

Currently, when running 'aspire new' in a directory that already contains files from a previous project, the command fails because 'dotnet new' returns exit code 73. The error output shown to users is long and includes a mention of the unsupported '--force' option. We want to improve this by detecting exit code 73 and instead displaying a user-friendly error that does not mention '--force'. The message should advise users to remove or move existing files before creating a new project.

Requirements:

  • In the code that executes 'dotnet new' (via IDotNetCliRunner in NewCommand or related templating logic), detect if the exit code is 73.
  • If exit code 73 is detected, throw a custom exception (e.g., ProjectAlreadyExistsException).
  • Catch this exception where user-facing messages are rendered, and show:
    "The output folder already contains files from a previous project. Please remove or move these files before creating a new project in this location."
  • Do not mention '--force' in the error message, since it's not supported by the aspire CLI.
  • Reference issue aspire new asks you to use --force on dotnet new if the folder already exists. #9685 in the PR.

Context:

  • This is to replace the long, unfriendly error output from 'dotnet new' when duplicate project creation is attempted.
  • The goal is to provide clearer, actionable guidance to users.

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Display friendly error when 'aspire new' detects existing project files (exit code 73) Add user-friendly error handling for aspire new when directory contains existing files Jul 17, 2025
@Copilot Copilot AI requested a review from mitchdenny July 17, 2025 09:50
Copilot finished work on behalf of mitchdenny July 17, 2025 09:50
…project creation conflicts

- Remove ProjectAlreadyExistsException from DotNetCliRunner.NewProjectAsync
- Add direct exit code 73 check in DotNetTemplateFactory.ApplyTemplateAsync with descriptive comment
- Update unit test to verify exit code 73 is returned instead of expecting exception
- All 112 CLI tests pass, maintaining full backward compatibility

Co-authored-by: mitchdenny <[email protected]>
@Copilot Copilot AI requested a review from mitchdenny July 17, 2025 10:14
Copilot finished work on behalf of mitchdenny July 17, 2025 10:14
@mitchdenny mitchdenny marked this pull request as ready for review July 17, 2025 10:14
@mitchdenny mitchdenny requested a review from davidfowl July 17, 2025 10:15
@mitchdenny mitchdenny added this to the 9.4 milestone Jul 17, 2025
@mitchdenny
Copy link
Member

Here is a screenshot of the output that this code produces for reference:
image

@copilot you can ignore this comment - no action is necessary.

Copilot finished work on behalf of mitchdenny July 17, 2025 10:18
Copy link
Member

@mitchdenny mitchdenny left a comment

Choose a reason for hiding this comment

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

LGTM @davidfowl @maddymontaquila I think this one is ready to go in (also needs to be backported).

@davidfowl
Copy link
Member

It turns out dotnet new works fine if you put multiple projects in the same folder as long as there are no conflicts:

image

Maybe we update the error message to reflect that. Otherwise this looks good.

Copy link
Contributor Author

Copilot AI commented Jul 18, 2025

It turns out dotnet new works fine if you put multiple projects in the same folder as long as there are no conflicts:

<img width="2574" height="952" alt="image" src="https://github.com/user-attachm...

Updated the error message to be more accurate - it now clarifies that the issue is specifically about conflicting files that would be overwritten, not just any existing files in the directory. See commit 76fe7af.

Copilot finished work on behalf of davidfowl July 18, 2025 14:45
@davidfowl davidfowl merged commit 0efc3dd into main Jul 19, 2025
276 checks passed
@davidfowl davidfowl deleted the copilot/fix-7a650be1-6087-4d9f-b4a6-5d75649b1f69 branch July 19, 2025 00:56
@davidfowl
Copy link
Member

/backport to release/9.4

Copy link
Contributor

Started backporting to release/9.4: https://github.com/dotnet/aspire/actions/runs/16383156860

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aspire new asks you to use --force on dotnet new if the folder already exists.
3 participants