Fix AsHostedAgent project reference missing in publish mode#17680
Closed
maddymontaquila wants to merge 3 commits into
Closed
Fix AsHostedAgent project reference missing in publish mode#17680maddymontaquila wants to merge 3 commits into
maddymontaquila wants to merge 3 commits into
Conversation
The asHostedAgent API was correctly calling WithReference(project) in run mode but was missing it in publish mode. This caused environment variables that reference the Foundry project endpoint to fail resolution during deployment. The fix adds the WithReference(project) call to the original resource builder in ConfigurePublishMode so that environment variable resolution during the deployment phase can find the project reference. Test: Added AsHostedAgent_InPublishMode_WithProject_AddsProjectReference to verify the project reference is properly wired in publish mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nv vars
Hosted agents validate deployed environment variable names against
^[A-Za-z0-9_]+$ at deploy time. Project names containing '-' (including
the auto-generated default '{name}-proj') would emit
'ConnectionStrings__{name}-proj' and fail deployment.
Route both run and publish mode through a shared AddProjectReference helper
that injects the project reference with an encoded connection name, keeping
run and deploy environment variables symmetric. Run mode still applies
WaitFor(project); publish mode skips it. The public Foundry WithReference
overload is left untouched.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 17680Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 17680" |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a publish/deploy mismatch in the Foundry AsHostedAgent(project) path where the Foundry project reference (and thus its connection environment variables) wasn’t being injected into the deployed target compute resource in publish mode, leading to missing project endpoint/config at deploy time. The change also ensures connection names are encoded so generated environment variable keys pass hosted-agent deploy-time validation.
Changes:
- Route both run and publish mode through a shared helper that injects the Foundry project reference onto the compute resource, preserving run/publish parity (while only run mode applies
WaitFor(project)). - Encode the project connection name before emitting
ConnectionStrings__{name}-style variables so keys are deploy-safe (e.g.,my-project→my_project). - Add/extend unit tests covering publish-mode env var resolution and the encoded connection-name behavior.
Show a summary per file
| File | Description |
|---|---|
| tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs | Adds a publish-mode regression test verifying the target resource resolves Foundry project connection env vars. |
| tests/Aspire.Hosting.Azure.Tests/FoundryExtensionsTests.cs | Adds a test asserting encoded connection keys pass deploy validation; refactors repeated provisioning simulation into a helper. |
| src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs | Centralizes project reference injection for run/publish, adds encoding for connection name, and injects the reference in publish mode onto the compute resource. |
| src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj | Links in the shared EnvironmentVariableNameEncoder implementation for use by the Foundry hosting package. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 0
davidfowl
reviewed
May 29, 2026
Replace the bespoke AddProjectReference helper (which re-implemented the IResourceWithWaitSupport cast + WaitFor) with a shared internal WithProjectReference helper in ProjectBuilderExtension.cs. The public WithReference(project) overload now delegates to it, so the cast lives in exactly one place. AsHostedAgent calls the shared helper with an encoded connection name in both run and publish mode, preserving deploy-safe env var names and run/publish symmetry while keeping run-mode WaitFor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a deploy-time failure when using the new Foundry
AsHostedAgent(project)API. The agent runs locally but fails on deploy withValueError: Azure AI project endpoint is required.Root cause
In publish mode,
AsHostedAgentnever injected the Foundry project reference into the target compute resource (the resource actually deployed). Run mode did inject it, but publish mode only attached the reference to the wrapperAzureHostedAgentResource, so the deployed agent's environment lacked the project connection (endpoint / connection string). The deployed agent therefore had no way to reach the project.Fix
Inject the project reference into the target builder in publish mode so the deployed agent gets the project connection environment variables. This is the core fix for the reported error.
Encode the connection name so the emitted env var key is deploy-safe. Hosted agents validate deployed environment variable names against
^[A-Za-z0-9_]+$at deploy time. A project name containing-— including the auto-generated default{name}-proj— would otherwise emitConnectionStrings__{name}-projand fail deployment.Both run and publish mode now route through a shared
AddProjectReferencehelper that injects the reference with an encoded connection name, keeping run and deploy environment variables symmetric. Run mode still appliesWaitFor(project); publish mode skips it (waiting has no meaning there). The public FoundryWithReference(project)overload is left untouched, so there is no behavior change for other callers. Encoding is a no-op for already-valid (dash-free) names.Testing
Aspire.Hosting.Foundry.Tests: 93/93 pass, including the run-mode dependency (WaitAnnotation) test.Aspire.Hosting.Azure.TestsFoundryExtensionsTests: 22/22 pass, including a new publish-mode test asserting the resolved env var key isConnectionStrings__my_project(not the dashed form) and that all resolved keys satisfy the deploy-time validation regex.Checklist