-
Couldn't load subscription status.
- Fork 712
Fix CommandLineArgsCallbackContext ExecutionContext in AzureResourcePreparer and prevent WithVSCodeDebugSupport execution in publish mode #11783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…reparer Set the ExecutionContext property when creating CommandLineArgsCallbackContext in AzureResourcePreparer.GetAzureReferences() to match how EnvironmentCallbackContext is created. This ensures callbacks receive the correct execution context (Publish vs Run mode), preventing WithVSCodeDebugSupport from incorrectly attempting to access ServiceProvider during manifest generation. Co-authored-by: davidfowl <[email protected]>
|
@copilot Also change |
Add check at method level to only register WithArgs callback when builder.ApplicationBuilder.ExecutionContext.IsRunMode is true. This prevents the callback from being registered during publish mode, avoiding unnecessary callback evaluation during manifest generation. Co-authored-by: davidfowl <[email protected]>
Done in 1348d79. Added check at method level to only register the WithArgs callback when |
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 11783Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 11783" |
|
@copilot add a test for WithVSCodeDebugSupport not applying in publish mode (there should be no annotation) |
There was a problem hiding this 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 pull request fixes a regression in Aspire 9.5.0 where using AddPythonApp with AddAzureContainerAppEnvironment causes manifest generation to fail with an "IServiceProvider is not available" error. The issue stems from incorrect execution context handling in command line argument callbacks during publish mode.
Key changes:
- Sets the correct ExecutionContext in
AzureResourcePreparer.GetAzureReferences()when creating CommandLineArgsCallbackContext - Prevents
WithVSCodeDebugSupportfrom registering callbacks during publish mode by checking execution context at the method level - Adds a regression test to verify command line argument callbacks receive the correct execution context
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Aspire.Hosting.Azure/AzureResourcePreparer.cs | Sets ExecutionContext property when creating CommandLineArgsCallbackContext to match the actual execution mode |
| src/Aspire.Hosting/ResourceBuilderExtensions.cs | Moves execution context check to method level to prevent callback registration during publish mode |
| tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs | Adds regression test verifying callbacks receive correct execution context during publish operations |
Added tests to verify that WithVSCodeDebugSupport does not add the SupportsDebuggingAnnotation in publish mode, only in run mode. This ensures the debugging annotation is only added when appropriate. Co-authored-by: davidfowl <[email protected]>
| builder.WithAnnotation(new SupportsDebuggingAnnotation(projectPath, debugAdapterId, requiredExtensionId)); | ||
|
|
||
| return builder; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| builder.WithAnnotation(new SupportsDebuggingAnnotation(projectPath, debugAdapterId, requiredExtensionId)); | |
| return builder; | |
| return builder.WithAnnotation(new SupportsDebuggingAnnotation(projectPath, debugAdapterId, requiredExtensionId)); |
(nit) this can be reverted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d421906. Reverted the formatting change.
| // This simulates WithVSCodeDebugSupport behavior - checking IsRunMode before accessing ServiceProvider | ||
| if (!context.ExecutionContext.IsRunMode) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // This should not be reached during publish mode, but if the ExecutionContext is not set correctly, | ||
| // it will default to Run mode and then try to access ServiceProvider which will throw | ||
| _ = context.ExecutionContext.ServiceProvider; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- we don't need the IsRunMode check. That can be removed.
- You can set the
IServiceProviderto a variable outside of the callback on line 243, and then Assert that it is not null at the end of the test on line 267.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d421906. Removed the IsRunMode check and updated the test to capture the ExecutionContext and assert it's not null and in Publish mode.
|
@copilot implement PR feedback. |
- Reverted formatting change in ResourceBuilderExtensions.cs (return statement on one line) - Improved CommandLineArgsCallbackContextHasCorrectExecutionContextDuringPublish test to: - Remove unnecessary IsRunMode check in callback - Capture ExecutionContext to verify it's set correctly - Assert ExecutionContext is not null and in Publish mode Co-authored-by: davidfowl <[email protected]>
|
/backport to release/9.5 |
|
Started backporting to release/9.5: https://github.com/dotnet/aspire/actions/runs/18177473850 |
…reparer and prevent WithVSCodeDebugSupport execution in publish mode (#11783) * Initial plan * Fix CommandLineArgsCallbackContext ExecutionContext in AzureResourcePreparer Set the ExecutionContext property when creating CommandLineArgsCallbackContext in AzureResourcePreparer.GetAzureReferences() to match how EnvironmentCallbackContext is created. This ensures callbacks receive the correct execution context (Publish vs Run mode), preventing WithVSCodeDebugSupport from incorrectly attempting to access ServiceProvider during manifest generation. Co-authored-by: davidfowl <[email protected]> * Only execute WithVSCodeDebugSupport in run mode Add check at method level to only register WithArgs callback when builder.ApplicationBuilder.ExecutionContext.IsRunMode is true. This prevents the callback from being registered during publish mode, avoiding unnecessary callback evaluation during manifest generation. Co-authored-by: davidfowl <[email protected]> * Update src/Aspire.Hosting/ResourceBuilderExtensions.cs * Add tests for WithVSCodeDebugSupport behavior in publish mode Added tests to verify that WithVSCodeDebugSupport does not add the SupportsDebuggingAnnotation in publish mode, only in run mode. This ensures the debugging annotation is only added when appropriate. Co-authored-by: davidfowl <[email protected]> * Address PR feedback: revert formatting and improve test - Reverted formatting change in ResourceBuilderExtensions.cs (return statement on one line) - Improved CommandLineArgsCallbackContextHasCorrectExecutionContextDuringPublish test to: - Remove unnecessary IsRunMode check in callback - Capture ExecutionContext to verify it's set correctly - Assert ExecutionContext is not null and in Publish mode Co-authored-by: davidfowl <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: davidfowl <[email protected]> Co-authored-by: David Fowler <[email protected]>
Fix: WithVSCodeDebugSupport ServiceProvider Access Issue During Manifest Generation
Fixes a regression in Aspire 9.5.0 where using
AddPythonAppwithAddAzureContainerAppEnvironmentcauses manifest generation (azd infra gen) to fail with "IServiceProvider is not available" error.Root Cause
When
AzureResourcePreparer.GetAzureReferences()evaluates command line argument callbacks to discover Azure resource references, it was creatingCommandLineArgsCallbackContextwithout setting theExecutionContextproperty. This caused the context to default toRunmode instead of inheriting the actual execution context (Publishmode during manifest generation).The
WithVSCodeDebugSupportcallback (introduced in 9.5.0 for Python apps) checksctx.ExecutionContext.IsRunModebefore accessingctx.ExecutionContext.ServiceProvider. With the incorrect default context, this check failed, causing the callback to attempt accessingServiceProviderduring manifest generation where it's not available.Changes Made
Set ExecutionContext in AzureResourcePreparer - Set the
ExecutionContextproperty when creatingCommandLineArgsCallbackContextinAzureResourcePreparer.GetAzureReferences()to match the pattern already used forEnvironmentCallbackContextOnly execute WithVSCodeDebugSupport in run mode - Modified
WithVSCodeDebugSupportto return early when not in run mode, preventing both theWithArgscallback registration and theSupportsDebuggingAnnotationfrom being added during publish modeAdded tests - Added tests to verify:
WithVSCodeDebugSupportcorrectly adds the annotation in run modeWithVSCodeDebugSupportdoes not add the annotation in publish modeCommandLineArgsCallbackContexthas the correctExecutionContextset during publish operationsImpact
WithVSCodeDebugSupport) now work correctly with Azure Container Apps during manifest generationIsRunModevsIsPublishModedecisionsWithVSCodeDebugSupportcallbacks and debugging annotations are only registered when appropriate for the execution modeTesting
CommandLineArgsCallbackContextHasCorrectExecutionContextDuringPublishthat verifies callbacks receive the correct execution context during publish operationsWithVSCodeDebugSupportAddsAnnotationInRunModeto verify theSupportsDebuggingAnnotationis correctly added in run modeWithVSCodeDebugSupportDoesNotAddAnnotationInPublishModeto verify the annotation is not added in publish modeFixes #11781
Original prompt
This section details on the original issue you should resolve
<issue_title>WithVSCodeDebugSupport in Python 9.5.0 causes IServiceProvider error with AddAzureContainerAppEnvironment during manifest generation (azd infra gen)</issue_title>
<issue_description>## Description
When using
AddAzureContainerAppEnvironmenttogether withAddPythonAppin Aspire 9.5.0, runningazd infra gen(manifest generation) fails with anIServiceProvider is not availableerror. This is a regression in 9.5.0 - the same code works correctly in 9.4.2.Environment
azd)Steps to Reproduce
Create an AppHost project with Aspire 9.5.0
Add the following packages:
Configure AppHost.cs with both Azure Container App Environment and Python app:
Run
azd infra genExpected Behavior
Manifest generation should succeed and produce infrastructure files.
Actual Behavior
The command fails with the following error:
Root Cause Analysis
New Feature in 9.5.0:
WithVSCodeDebugSupportwas added toAddPythonAppin Aspire 9.5.0 to enable VS Code debugging support (see diff)The Conflict: When
AddAzureContainerAppEnvironmentis present, theAzureResourcePreparer.BeforeStartAsynclifecycle hook runs during manifest generation and callsGetAzureReferences(), which evaluates all args callbacks to discover Azure resource referencesThe Bug: The
WithVSCodeDebugSupportcallback attempts to accessctx.ExecutionContext.ServiceProvider, but during manifest generation, the execution context wasn't constructed withDistributedApplicationExecutionContextOptions, causing theServiceProviderproperty getter to throw an exception before theIsRunModecheck can executeWhy 9.4.2 Works: The Python package in 9.4.2 does not include
WithVSCodeDebugSupport, so this callback never existsImpact
This regression affects any Aspire 9.5.0 project that:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.