Skip to content

Fix aspire run hang when defaultWatchEnabled and build fails#16702

Open
Bertolossi wants to merge 2 commits intomicrosoft:mainfrom
Bertolossi:fix-13142-default-watch-build-error
Open

Fix aspire run hang when defaultWatchEnabled and build fails#16702
Bertolossi wants to merge 2 commits intomicrosoft:mainfrom
Bertolossi:fix-13142-default-watch-build-error

Conversation

@Bertolossi
Copy link
Copy Markdown

Description

Fixes a hang in aspire run when the defaultWatchEnabled feature flag is set and the AppHost has a build error.

Repro: With the following in .aspire/settings.json:

"features": {
  "defaultWatchEnabled": true
}

introduce any compile error in the solution and run aspire run. The CLI hangs indefinitely at "Connecting to apphost..." and must be killed manually.

Root cause: In DotNetAppHostProject.RunAsync, the CLI's pre-build step was gated on !watch. In watch mode the CLI delegated building entirely to dotnet watch, which by design does not exit on a build error — it idles waiting for a file change. The CLI then sits on BackchannelCompletionSource forever because the AppHost never starts.

Fix: Drop the !watch condition so the CLI always performs an explicit pre-build (when shouldBuildInCli). On failure, the existing path sets OutputCollector, signals BuildCompletionSource(false), and returns ExitCodeConstants.FailedToBuildArtifacts, so build errors are surfaced to the user and the process exits cleanly. After a successful pre-build, watch mode still passes noBuild: false to dotnet watch run so it can manage its own incremental builds and hot reload.

A regression test (RunCommand_WhenDefaultWatchEnabledFeatureFlagIsTrueAndBuildFails_ReturnsBuildFailure) was added in RunCommandTests to lock the behavior in.

No new dependencies.

Fixes #13142

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

When DefaultWatchEnabled was set, the CLI skipped its pre-build and
relied on dotnet watch, which idles instead of exiting on build errors.
Always pre-build in the CLI so build failures surface and short-circuit
to FailedToBuildArtifacts.

Fixes microsoft#13142

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 2, 2026 23:37
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 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 -- 16702

Or

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

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR ensures the run command exits with a build-failure code (and does not start the AppHost) when default watch mode is enabled and the initial build fails.

Changes:

  • Added a regression test asserting build failures in default watch mode return FailedToBuildArtifacts and do not invoke AppHost run.
  • Changed AppHost project execution to perform the initial build even when watch mode is enabled.
  • Updated inline comments describing noBuild behavior for watch vs non-watch runs.

Reviewed changes

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

File Description
tests/Aspire.Cli.Tests/Commands/RunCommandTests.cs Adds coverage for watch-default behavior when the initial build fails.
src/Aspire.Cli/Projects/DotNetAppHostProject.cs Builds even in watch mode to fail fast before starting AppHost; updates comments around noBuild.

Comment thread tests/Aspire.Cli.Tests/Commands/RunCommandTests.cs
Comment thread src/Aspire.Cli/Projects/DotNetAppHostProject.cs
Comment thread src/Aspire.Cli/Projects/DotNetAppHostProject.cs Outdated
@Bertolossi
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

aspire run hangs on build errors when using defaultWatchEnabled

2 participants