Refactor container helper to avoid environment confusion bugs#6649
Refactor container helper to avoid environment confusion bugs#6649weikanglim merged 1 commit intoAzure:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors ContainerHelper to be stateless by requiring callers to pass the active *environment.Environment explicitly, preventing bugs caused by implicitly resolving the default environment.
Changes:
- Remove implicit environment resolution from
ContainerHelper(and its constructor) and thread*environment.Environmentthrough helper method calls. - Update service targets (Container Apps, AKS, Docker framework, .NET Container App flows) to pass their current environment explicitly.
- Update gRPC container service and unit tests to use the new explicit-environment APIs.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| cli/azd/pkg/project/container_helper.go | Removes internal env lookup and updates helper APIs to accept *environment.Environment. |
| cli/azd/pkg/project/container_helper_test.go | Updates tests for new helper signatures and explicit env passing. |
| cli/azd/pkg/project/framework_service_docker.go | Passes p.env into ContainerHelper.Build/Package. |
| cli/azd/pkg/project/framework_service_docker_test.go | Updates helper construction for new constructor signature. |
| cli/azd/pkg/project/service_target_containerapp.go | Passes at.env into ContainerHelper.Publish. |
| cli/azd/pkg/project/service_target_containerapp_test.go | Updates helper construction for new constructor signature. |
| cli/azd/pkg/project/service_target_aks.go | Passes t.env into ContainerHelper.Publish. |
| cli/azd/pkg/project/service_target_aks_test.go | Updates helper construction for new constructor signature. |
| cli/azd/pkg/project/service_target_dotnet_containerapp.go | Passes at.env through Build/Package/Publish/Credentials/DefaultImageName calls. |
| cli/azd/internal/grpcserver/container_service.go | Lazily resolves environment and passes it into container helper operations. |
Comments suppressed due to low confidence (3)
cli/azd/pkg/project/container_helper_test.go:1182
envManagerandazdCtxare still being set up in this test case, butContainerHelperno longer resolves the current environment internally. SincePublishis now passedenvexplicitly, this mockenvManager/azdCtxsetup is unused and can be removed to avoid confusion about what drives the behavior under test.
envManager := &mockenv.MockEnvManager{}
envManager.On("Get", mock.Anything, "dev").Return(env, nil)
envManager.On("Save", *mockContext.Context, env).Return(nil)
azdCtx := azdcontext.NewAzdContextWithDirectory(t.TempDir())
err := azdCtx.SetProjectState(azdcontext.ProjectState{
DefaultEnvironment: "dev",
})
require.NoError(t, err)
mockContainerRegistryService := &mockContainerRegistryService{}
setupContainerRegistryMocks(mockContext, &mockContainerRegistryService.Mock)
containerHelper := NewContainerHelper(
clock.NewMock(),
mockContainerRegistryService,
nil,
mockContext.CommandRunner,
dockerCli,
dotnetCli,
mockContext.Console,
cloud.AzurePublic(),
)
cli/azd/pkg/project/service_target_containerapp_test.go:271
azdCtxis initialized and has project state set above, but it is no longer passed to (or used by)NewContainerHelper. Unless another dependency here relies on that side effect, thisazdCtxsetup is now redundant and can be removed to keep the test setup minimal and aligned with the new explicit-environment approach.
containerHelper := NewContainerHelper(
clock.NewMock(),
containerRegistryService,
remoteBuildManager,
nil,
dockerCli,
dotnetCli,
mockContext.Console,
cloud.AzurePublic(),
)
cli/azd/pkg/project/service_target_aks_test.go:1015
NewContainerHelperno longer takesazdCtx, butcreateAksServiceTargetstill accepts anazdCtxparameter (now unused in the function body). Consider removing that parameter and updating call sites so the helper signature and test setup reflect the new explicit-environment design and avoid misleading future readers.
containerHelper := NewContainerHelper(
clock.NewMock(),
containerRegistryService,
remoteBuildManager,
nil,
dockerCli,
dotnetCli,
mockContext.Console,
cloud.AzurePublic(),
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… environment confusion bugs
b298ecb to
d219a84
Compare
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash: pwsh: WindowsPowerShell install MSI install Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|
Refactored
ContainerHelperto accept*environment.Environmentexplicitly in method signatures instead of implicitly resolving it viagetCurrentEnvironment(). This fixes a bug where the helper was always fetching the default environment, makes it stateless and reusable as library code.The issue in #6019 is not regressed since
container_service.gocontinues to observe the environment updates using*lazy.Lazy[*environment.Environment].Fixes a concern raised in #6641