Skip to content

Surface build output on failure for Docker, Podman, and dotnet publish#16093

Open
davidfowl wants to merge 3 commits intomainfrom
davidfowl/surface-build-output-on-failure
Open

Surface build output on failure for Docker, Podman, and dotnet publish#16093
davidfowl wants to merge 3 commits intomainfrom
davidfowl/surface-build-output-on-failure

Conversation

@davidfowl
Copy link
Copy Markdown
Contributor

Description

Surface build output on failure for Docker, Podman, and dotnet publish container builds.

Problem

When a container image build fails, users see only:

Docker build failed with exit code 1.

The actual build output (compiler errors, missing files, Dockerfile issues) is logged at Debug level only. Users have to re-run with --log-level debug to see what went wrong.

Solution

Introduce ProcessFailedException that captures stdout/stderr from failed build processes. The exception's Message property includes the last 50 lines of output so it surfaces through the pipeline step reporter automatically.

Changes

  • ProcessFailedException — new internal exception type with ExitCode, BuildOutput (raw lines), and GetFormattedOutput(). Message is overridden to include formatted output.
  • Docker buildx — deduped to use base class ExecuteContainerCommandWithExitCodeAsync, throws ProcessFailedException with captured output
  • Podman build — throws ProcessFailedException with captured output
  • dotnet publish /t:PublishContainer — throws ProcessFailedException with captured output
  • Buildkit instance creation — also throws ProcessFailedException
  • Uses ConcurrentQueue<string> for thread-safe output buffering (callbacks fire on different threads)
  • Integration test for Docker build failure verifying exception carries output

Before

(build-myapi) ✗ Docker build failed with exit code 1.

After

(build-myapi) ✗ Docker build failed with exit code 1.
  #8 [builder 4/5] COPY nonexistent-file.txt /app/
  #8 ERROR: "/nonexistent-file.txt": not found
  Dockerfile:4
  >>> COPY nonexistent-file.txt /app/
  ERROR: failed to build: failed to compute cache key

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
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No
  • Does the change require an update in our Aspire docs?
    • Yes
    • No

Introduce ProcessFailedException that captures stdout/stderr from
failed build processes. The exception's Message property includes the
last 50 lines of output so it surfaces through the pipeline step
reporter without requiring --log-level debug.

All three container build paths now throw ProcessFailedException
consistently:
- Docker buildx (deduped to use base class ExecuteContainerCommand)
- Podman build
- dotnet publish /t:PublishContainer

Uses ConcurrentQueue<string> for thread-safe output buffering since
OnOutputData/OnErrorData fire on different threads.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl davidfowl requested a review from mitchdenny as a code owner April 12, 2026 17:47
Copilot AI review requested due to automatic review settings April 12, 2026 17:47
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 12, 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 -- 16093

Or

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

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

This PR improves diagnostics for container image builds by capturing and surfacing build stdout/stderr when Docker, Podman, or dotnet publish /t:PublishContainer fails, so users can see actionable error output without re-running with --log-level debug.

Changes:

  • Added ProcessFailedException to carry exit code plus captured build output and include formatted tail output in Message.
  • Updated Docker and Podman build paths (including buildkit instance creation) to capture output and throw ProcessFailedException on non-zero exit codes.
  • Updated dotnet publish container build path to capture output and throw ProcessFailedException, plus added an integration test asserting build output is present.
Show a summary per file
File Description
tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs Adds Docker build failure integration test validating captured output is present on failure.
src/Aspire.Hosting/Publishing/ResourceContainerImageManager.cs Captures dotnet publish output and throws ProcessFailedException on failure.
src/Aspire.Hosting/Publishing/ProcessFailedException.cs Introduces new exception type that formats and surfaces tail build output in Message.
src/Aspire.Hosting/Publishing/PodmanContainerRuntime.cs Captures Podman build output and throws ProcessFailedException on failure.
src/Aspire.Hosting/Publishing/DockerContainerRuntime.cs Refactors Docker buildx path to shared execution helper, captures output, throws ProcessFailedException (including for buildkit creation failures).
src/Aspire.Hosting/Publishing/ContainerRuntimeBase.cs Extends command execution helper to optionally buffer stdout/stderr lines for callers to include in exceptions.

Copilot's findings

Comments suppressed due to low confidence (1)

tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs:1457

  • This Docker build-failure test uses a remote base image (mcr.microsoft.com/cbl-mariner/...). If the pull fails (network, throttling, outage), the build may fail before the COPY nonexistent-file-12345.txt step and the assertion on that filename can become flaky. Consider using FROM scratch (or another already-cached test image used elsewhere) so the failure deterministically comes from the missing file.
        await File.WriteAllTextAsync(dockerfilePath, """
            FROM mcr.microsoft.com/cbl-mariner/base/core:2.0
            COPY nonexistent-file-12345.txt /app/
            """);
  • Files reviewed: 6/6 changed files
  • Comments generated: 6

…gging

- Add using to TestTempDirectory in test to prevent temp folder leaks
- Add catch/log pattern in BuildProjectContainerImageAsync for
  consistent error logging across build paths

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The existing BuildImageAsync_ProjectBuildFailureIncludesResourceName
test expected InvalidOperationException but now gets
ProcessFailedException after our changes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

🎬 CLI E2E Test Recordings — 68 recordings uploaded (commit bc5d7cc)

View recordings
Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_DefaultSelection_InstallsSkillOnly ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AllPublishMethodsBuildDockerImages ▶️ View Recording
AspireAddPackageVersionToDirectoryPackagesProps ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
Banner_NotDisplayedWithNoLogoFlag ▶️ View Recording
CertificatesClean_RemovesCertificates ▶️ View Recording
CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ▶️ View Recording
CertificatesTrust_WithUntrustedCert_TrustsCertificate ▶️ View Recording
ConfigSetGet_CreatesNestedJsonFormat ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunEmptyAppHostProject ▶️ View Recording
CreateAndRunJavaEmptyAppHostProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateAndRunTypeScriptEmptyAppHostProject ▶️ View Recording
CreateAndRunTypeScriptStarterProject ▶️ View Recording
CreateJavaAppHostWithViteApp ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DashboardRunWithOtelTracesReturnsNoTraces ▶️ View Recording
DeployK8sBasicApiService ▶️ View Recording
DeployK8sWithGarnet ▶️ View Recording
DeployK8sWithMongoDB ▶️ View Recording
DeployK8sWithMySql ▶️ View Recording
DeployK8sWithPostgres ▶️ View Recording
DeployK8sWithRabbitMQ ▶️ View Recording
DeployK8sWithRedis ▶️ View Recording
DeployK8sWithSqlServer ▶️ View Recording
DeployK8sWithValkey ▶️ View Recording
DeployTypeScriptAppToKubernetes ▶️ View Recording
DescribeCommandResolvesReplicaNames ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
GlobalMigration_HandlesCommentsAndTrailingCommas ▶️ View Recording
GlobalMigration_HandlesMalformedLegacyJson ▶️ View Recording
GlobalMigration_PreservesAllValueTypes ▶️ View Recording
GlobalMigration_SkipsWhenNewConfigExists ▶️ View Recording
GlobalSettings_MigratedFromLegacyFormat ▶️ View Recording
InitTypeScriptAppHost_AugmentsExistingViteRepoAtRoot ▶️ View Recording
InvalidAppHostPathWithComments_IsHealedOnRun ▶️ View Recording
LegacySettingsMigration_AdjustsRelativeAppHostPath ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
PsFormatJsonOutputsOnlyJsonToStdout ▶️ View Recording
PublishWithDockerComposeServiceCallbackSucceeds ▶️ View Recording
RestoreGeneratesSdkFiles ▶️ View Recording
RestoreSupportsConfigOnlyHelperPackageAndCrossPackageTypes ▶️ View Recording
RunFromParentDirectory_UsesExistingConfigNearAppHost ▶️ View Recording
SecretCrudOnDotNetAppHost ▶️ View Recording
SecretCrudOnTypeScriptAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StartAndWaitForTypeScriptSqlServerAppHostWithNativeAssets ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording
UnAwaitedChainsCompileWithAutoResolvePromises ▶️ View Recording

📹 Recordings uploaded automatically from CI run #24314128290

Comment on lines +49 to +51
IEnumerable<string> lines = BuildOutput.Count > maxLines
? BuildOutput.Skip(BuildOutput.Count - maxLines)
: BuildOutput;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the number of lines is truncated then should that be included in the message? e.g. Build output truncated: showing last 50 of 200 lines.

Copy link
Copy Markdown
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

1 issue found (correctness). The new exception type won't pass through the pipeline step executor cleanly — it will get wrapped with a redundant step-name prefix instead of surfacing the formatted build output directly.

/// <summary>
/// Exception thrown when a container image build or dotnet publish operation fails.
/// </summary>
internal sealed class ProcessFailedException : Exception
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ProcessFailedException extends Exception directly, but the pipeline step executor in DistributedApplicationPipeline.ExecuteStepAsync re-throws DistributedApplicationException subtypes unchanged while wrapping everything else in InvalidOperationException($"Step '{step.Name}' failed: {ex.Message}").

Since build image calls flow through pipeline steps (e.g. ContainerResourceBuilderExtensions and ProjectResource.BuildProjectImage), this exception will be wrapped and the user-facing message will include a redundant "Step 'build-myapi' failed:" prefix — even though the step name is already shown by the pipeline reporter context ((build-myapi) ✗).

Consider either making ProcessFailedException extend DistributedApplicationException, or adding it to the pipeline's pass-through catch clause so the formatted build output surfaces cleanly without the extra wrapping.

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.

3 participants