Load environment variables from launch of AzureFunctions project#18689
Load environment variables from launch of AzureFunctions project#18689dsurys wants to merge 12 commits into
Conversation
Updated AzureFunctionsProjectResourceExtensions to load Environment variables from project's launch profile in run mode.
Testing loading environment variables from launch profile
…res-env-variables Bugfix/16206 add azure function ignores env variables
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 18689Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 18689" |
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #16206, where AddAzureFunctionsProject silently ignored environmentVariables defined in the project's launchSettings.json launch profile — unlike the standard AddProject<T> path. The fix adds a run-mode branch to the resource's WithEnvironment callback that copies launch-profile environment variables (with environment-variable expansion) onto the Functions resource, and adds tests validating that these variables are applied in Run mode but not in Publish mode.
Changes:
- Adds an
else if (IsRunMode)branch that reads the effective launch profile and copies its environment variables into the resource viaTryAdd. - Adds two tests covering the Run-mode (applied) and Publish-mode (not applied) behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/Aspire.Hosting.Azure.Functions/AzureFunctionsProjectResourceExtensions.cs |
Adds run-mode branch to load launch-profile env vars; introduces an early return that unintentionally skips the storage configuration when no launch profile exists. |
tests/Aspire.Hosting.Azure.Tests/AzureFunctionsTests.cs |
Adds a test project metadata fixture with launch-profile env vars and two tests for run/publish behavior; contains minor naming/value typos and does not cover the null-launch-profile path. |
Key finding: the new return; inside the run-mode branch returns from the entire WithEnvironment callback, which skips the AzureWebJobsStorage storage configuration when no launch profile is selected/found in run mode — a regression from the prior behavior. The existing tests don't catch this because their fixture always provides a launch profile.
| var launchProfile = context.Resource.GetEffectiveLaunchProfile()?.LaunchProfile; | ||
| if (launchProfile is null) | ||
| { | ||
| return; | ||
| } | ||
| foreach (var envVar in launchProfile.EnvironmentVariables) | ||
| { | ||
| var value = Environment.ExpandEnvironmentVariables(envVar.Value); | ||
| context.EnvironmentVariables.TryAdd(envVar.Key, value); | ||
| } |
| } | ||
|
|
||
| [Fact] | ||
| public async Task AddAzureFunctionsProject_DoesNotAddsEnvironmentVariables_FromLaunchProfile_WhnInPublishMode() |
| { | ||
| CommandLineArgs = "--useHttps", | ||
| LaunchBrowser = false, | ||
| EnvironmentVariables = { { CustomEnvironmentVariable, "Somve value" } } |
@microsoft-github-policy-service agree |
Fixed typos in unit tests
| if (context.ExecutionContext.IsRunMode) | ||
| { | ||
| var launchProfile = context.Resource.GetEffectiveLaunchProfile()?.LaunchProfile; | ||
| if (launchProfile is null) | ||
| { | ||
| return; | ||
| } | ||
| foreach (var envVar in launchProfile.EnvironmentVariables) | ||
| { | ||
| var value = Environment.ExpandEnvironmentVariables(envVar.Value); | ||
| context.EnvironmentVariables.TryAdd(envVar.Key, value); | ||
| } | ||
| } |
Revert "Merge pull request #1 from dsurys/bugfix/16206-Add-AzureFunct…
| if (context.ExecutionContext.IsRunMode) | ||
| { | ||
| var launchProfile = context.Resource.GetEffectiveLaunchProfile()?.LaunchProfile; | ||
| if (launchProfile is not null) | ||
| { | ||
| foreach (var envVar in launchProfile.EnvironmentVariables) | ||
| { | ||
| var value = Environment.ExpandEnvironmentVariables(envVar.Value); | ||
| context.EnvironmentVariables.TryAdd(envVar.Key, value); | ||
| } | ||
| } | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Description
AddAzureFunctionsProject extension method does not add env variables from the project's launch profile.
Added a loop that copies environment variables from launch profile to the resource builder analogously to AddProject method.
Applied only to Run mode.
Fixes #16206
Checklist
<remarks />and<code />elements on your triple slash comments?