Skip to content

Read ASPIRE_ENVIRONMENT in DistributedApplicationBuilder#16098

Open
davidfowl wants to merge 1 commit intomainfrom
fix/aspire-environment-in-builder
Open

Read ASPIRE_ENVIRONMENT in DistributedApplicationBuilder#16098
davidfowl wants to merge 1 commit intomainfrom
fix/aspire-environment-in-builder

Conversation

@davidfowl
Copy link
Copy Markdown
Contributor

@davidfowl davidfowl commented Apr 12, 2026

Summary

ASPIRE_ENVIRONMENT is the Aspire-native, language-agnostic way to set the environment. Previously it was only read by the Aspire CLI, not by the AppHost itself, meaning it was silently ignored when running via dotnet run, azd, or any non-CLI launch path.

This PR adds ASPIRE_-prefixed environment variables as a configuration source in DistributedApplicationBuilder, using the same AddEnvironmentVariables(prefix:) pattern as the core framework uses for DOTNET_-prefixed variables.

Priority order

Priority Source Example
1 (highest) --environment CLI flag dotnet run -- --environment Production
2 DOTNET_ENVIRONMENT export DOTNET_ENVIRONMENT=Staging
3 ASPIRE_ENVIRONMENT export ASPIRE_ENVIRONMENT=Staging
4 (lowest) Default Production

Changes

  • src/Aspire.Hosting/DistributedApplicationBuilder.cs — Pre-seed ConfigurationManager with AddEnvironmentVariables(prefix: "ASPIRE_") before passing to HostApplicationBuilder
  • tests/Aspire.Hosting.Tests/AspireEnvironmentTests.cs — 5 unit tests using RemoteExecutor with shared CreateEnvironmentOptions helper
  • tests/Aspire.Cli.EndToEnd.Tests/AspireEnvironmentTests.cs — 2 E2E tests verifying --environment and ASPIRE_ENVIRONMENT flow through to publish
  • tests/Aspire.Cli.EndToEnd.Tests/Helpers/CaptureWorkspaceOnFailureAttribute.cs — Capture filtered env vars on failure; preserve workspace locally for debugging
  • tests/Shared/TemporaryRepo.cs — Preserve workspace on disk when tests fail locally (skip cleanup outside CI)

Test coverage

Test Scenario
AspireEnvironmentSetsBuilderEnvironment ASPIRE_ENVIRONMENT=StagingStaging
DotnetEnvironmentTakesPrecedenceOverAspireEnvironment DOTNET_ENVIRONMENT wins
EnvironmentFlagTakesPrecedenceOverAspireEnvironment --environment wins
DefaultEnvironmentIsProductionWithNoEnvVars Default → Production
AspireEnvironmentSetsCustomEnvironmentName Custom name + helper methods
PublishWithEnvironmentFlagSetsEnvironmentName E2E: --environment Staging and default Production
AspireEnvironmentEnvVarSetsEnvironmentName E2E: ASPIRE_ENVIRONMENT=Staging

Fixes #16096

Copilot AI review requested due to automatic review settings April 12, 2026 22:32
@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 -- 16098

Or

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

@davidfowl davidfowl force-pushed the fix/aspire-environment-in-builder branch 2 times, most recently from 4348322 to 8f2fa03 Compare April 12, 2026 22:55
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@davidfowl davidfowl force-pushed the fix/aspire-environment-in-builder branch 2 times, most recently from 4fd32e8 to 587315a Compare April 12, 2026 23:30
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.

2 issues flagged: 1 security concern (environment variable dump in test artifacts), 1 unused constant.

@davidfowl davidfowl force-pushed the fix/aspire-environment-in-builder branch from 32c5ff4 to 8536962 Compare April 13, 2026 02:34
@davidfowl davidfowl force-pushed the fix/aspire-environment-in-builder branch 3 times, most recently from f46f348 to 6a172fa Compare April 13, 2026 03:52
Add ASPIRE_-prefixed environment variables as a configuration source
in DistributedApplicationBuilder, using the same AddEnvironmentVariables
pattern as the core framework uses for DOTNET_-prefixed variables.

Priority order (highest to lowest):
1. --environment CLI flag
2. DOTNET_ENVIRONMENT
3. ASPIRE_ENVIRONMENT
4. Default (Production)

Fixes #16096

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl davidfowl force-pushed the fix/aspire-environment-in-builder branch from 6a172fa to 358336b Compare April 13, 2026 04:19
@github-actions
Copy link
Copy Markdown
Contributor

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

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 #24325469900

private static RemoteInvokeOptions CreateEnvironmentOptions(
string? aspireEnvironment = null,
string? dotnetEnvironment = null,
string? aspNetCoreEnvironment = null)
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.

When do we use aspNetCoreEnvironment?


// Pre-seed the configuration with ASPIRE_-prefixed environment variables.
// HostApplicationBuilder will then add DOTNET_-prefixed env vars and command line args on top.
// This gives us the priority order: --environment > DOTNET_ENVIRONMENT > ASPIRE_ENVIRONMENT > default.
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.

Should we add docs for this?

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_ENVIRONMENT should be read by DistributedApplicationBuilder, not just the CLI

4 participants