{"record":{"id":"db4efeebd394f561","repo":"microsoft/aspire","slug":"unable-to-resolve-environment-variable","errorCode":null,"errorMessage":"Unable to resolve environment variable '{environmentVariableName}' for Foundry hosted agent '{hostedAgent.Name}' from target resource '{resource.Name}'. Endpoint '{endpointReference.EndpointName}' on resource '{endpointReference.Resource.Name}' cannot be used. Resource '{endpointReference.Resource.Name}' does not have a compute environment deployment target.","messagePattern":"Unable to resolve environment variable '(.+?)' for Foundry hosted agent '(.+?)' from target resource '(.+?)'\\. Endpoint '(.+?)' on resource '(.+?)' cannot be used\\. Resource '(.+?)' does not have a compute environment deployment target\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs","lineNumber":475,"sourceCode":"        EndpointReferenceExpression endpointReferenceExpression,\n        DistributedApplicationExecutionContext context,\n        AzureHostedAgentResource hostedAgent,\n        IResource resource,\n        string environmentVariableName,\n        CancellationToken cancellationToken)\n    {\n        var endpointReference = endpointReferenceExpression.Endpoint;\n        var endpoint = endpointReference.EndpointAnnotation;\n\n        if (endpointReference.Resource != hostedAgent.Target && !endpoint.IsExternal)\n        {\n            throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, $\"Endpoint '{endpoint.Name}' is internal. Foundry hosted agents can only reference externally exposed endpoints during publish.\");\n        }\n\n        if (!ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(endpointReference.Resource, out var computeEnvironment))\n        {\n            var reason = $\"Resource '{endpointReference.Resource.Name}' does not have a compute environment deployment target.\";\n            throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, reason);\n        }\n\n#pragma warning disable ASPIRECOMPUTE002\n        var expression = computeEnvironment.GetEndpointPropertyExpression(endpointReferenceExpression);\n#pragma warning restore ASPIRECOMPUTE002\n\n        return await expression.GetValueAsync(\n            new ValueProviderContext\n            {\n                ExecutionContext = context,\n                Caller = resource\n            },\n            cancellationToken).ConfigureAwait(false);\n    }\n\n    private static InvalidOperationException CreateEndpointResolutionException(\n        AzureHostedAgentResource hostedAgent,\n        IResource resource,","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs#L457-L493","documentation":"When publishing a Foundry hosted agent, each endpoint-referenced environment variable must resolve to a resource that has a compute environment deployment target, so Aspire can compute the published endpoint URL. This error means the referenced resource is not deployed into any compute environment, so the agent's environment variable cannot be resolved.","triggerScenarios":"ResolvePublishedEndpointAsync calls ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(endpointReference.Resource, ...) which returns false, causing CreateEndpointResolutionException during publish of the agent's environment variable.","commonSituations":"Referencing an endpoint of a resource that isn't deployed (local-only project, container, external process); forgetting to add the referenced project to the compute environment; referencing infrastructure resources via endpoint bindings.","solutions":["Add the referenced resource to a compute environment so it has a deployment target.","Reference a resource actually deployed into the same compute environment as the agent.","If the value is not a deployed endpoint, bind via a connection string or explicit URL instead.","Verify the endpoint's resource is associated with the compute environment before publish."],"exampleFix":"// before\nvar agent = builder.AddProject<Projects.Agent>(\"agent\")\n    .WithEnvironment(\"API_URL\", externalApi.GetEndpoint(\"https\")); // not in compute env\n\n// after\ncomputeEnvironment.AddProject(externalApi);\nvar agent = builder.AddProject<Projects.Agent>(\"agent\")\n    .WithEnvironment(\"API_URL\", externalApi.GetEndpoint(\"https\"));","handlingStrategy":"validation","validationCode":"// Only reference endpoints of resources deployed into a compute environment\nif (!IsDeployedInComputeEnvironment(endpointRef.Resource))\n    throw new InvalidOperationException(\n        $\"Resource '{endpointRef.Resource.Name}' has no compute environment; add it before publishing the agent.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add every endpoint-referenced project to the compute environment (e.g. env.AddProject(...)).","Reference connection strings or explicit URLs for resources that are not deployed endpoints.","Run a publish dry-run locally to catch unresolved environment variables before CI deploy."],"tags":["azure","foundry","publish","compute-environment","configuration"],"backgroundTag":"missing-required-config","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}