-
Notifications
You must be signed in to change notification settings - Fork 280
fix: azd down returns error instead of prompting init steps when no environment exists #6850
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51d38b9
Initial plan
Copilot 75e6317
Fix azd down prompting init steps when no .azure directory exists
Copilot 8996252
fix: azd down returns error instead of prompting init steps when no e…
Copilot 06fa2aa
Address review feedback: remove lazy[provisioning.Manager], use Servi…
Copilot 9c092ff
Revert unrelated go.mod/go.sum changes in extensions
Copilot 7ec387e
Fix eager env resolution: change HooksMiddleware to use lazy.Lazy[*en…
Copilot d8c9ec5
Fix: change provisioning.Manager to use lazy env, remove List() check…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/alpha" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/cloud" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/config" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/lazy" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/project" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks/mockenv" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func newTestProvisionManager( | ||
| mockContext *mocks.MockContext, | ||
| lazyEnv *lazy.Lazy[*environment.Environment], | ||
| envManager environment.Manager, | ||
| ) *provisioning.Manager { | ||
| alphaManager := alpha.NewFeaturesManagerWithConfig(config.NewEmptyConfig()) | ||
| return provisioning.NewManager( | ||
| mockContext.Container, | ||
| func() (provisioning.ProviderKind, error) { return provisioning.Bicep, nil }, | ||
| envManager, | ||
| lazyEnv, | ||
| mockContext.Console, | ||
| alphaManager, | ||
| nil, | ||
| cloud.AzurePublic(), | ||
| ) | ||
| } | ||
|
|
||
| func newTestDownAction( | ||
| t *testing.T, | ||
| mockContext *mocks.MockContext, | ||
| envManager *mockenv.MockEnvManager, | ||
| lazyEnv *lazy.Lazy[*environment.Environment], | ||
| provisionManager *provisioning.Manager, | ||
| ) *downAction { | ||
| t.Helper() | ||
| alphaManager := alpha.NewFeaturesManagerWithConfig(config.NewEmptyConfig()) | ||
| action := newDownAction( | ||
| []string{}, | ||
| &downFlags{}, | ||
| provisionManager, | ||
| lazyEnv, | ||
| envManager, | ||
| &project.ProjectConfig{}, | ||
| mockContext.Console, | ||
| alphaManager, | ||
| project.NewImportManager(nil), | ||
| ) | ||
| return action.(*downAction) | ||
| } | ||
|
|
||
| func Test_DownAction_NoEnvironments_ReturnsError(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| envManager := &mockenv.MockEnvManager{} | ||
|
|
||
| // lazyEnv must NOT be called when no env exists and it returns ErrNameNotSpecified | ||
| lazyEnv := lazy.NewLazy(func() (*environment.Environment, error) { | ||
| return nil, environment.ErrNameNotSpecified | ||
| }) | ||
| provisionManager := newTestProvisionManager(mockContext, lazyEnv, envManager) | ||
|
|
||
| action := newTestDownAction(t, mockContext, envManager, lazyEnv, provisionManager) | ||
|
|
||
| _, err := action.Run(*mockContext.Context) | ||
| require.Error(t, err) | ||
|
|
||
| var suggestionErr *internal.ErrorWithSuggestion | ||
| require.True(t, errors.As(err, &suggestionErr)) | ||
| require.Contains(t, suggestionErr.Error(), "no environment selected") | ||
| require.Contains(t, suggestionErr.Suggestion, "azd env new") | ||
| } | ||
|
|
||
| func Test_DownAction_EnvironmentNotFound_ReturnsError(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| envManager := &mockenv.MockEnvManager{} | ||
|
|
||
| // Simulate -e flag pointing to a missing environment | ||
| lazyEnv := lazy.NewLazy(func() (*environment.Environment, error) { | ||
| return nil, fmt.Errorf("'missing-env': %w", environment.ErrNotFound) | ||
| }) | ||
| provisionManager := newTestProvisionManager(mockContext, lazyEnv, envManager) | ||
|
|
||
| action := newTestDownAction(t, mockContext, envManager, lazyEnv, provisionManager) | ||
|
|
||
| _, err := action.Run(*mockContext.Context) | ||
| require.Error(t, err) | ||
|
|
||
| var suggestionErr *internal.ErrorWithSuggestion | ||
| require.True(t, errors.As(err, &suggestionErr)) | ||
| require.Contains(t, suggestionErr.Error(), "environment not found") | ||
| require.Contains(t, suggestionErr.Suggestion, "azd env list") | ||
| } | ||
|
|
||
| func Test_DownAction_NoDefaultEnvironment_ReturnsError(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| envManager := &mockenv.MockEnvManager{} | ||
|
|
||
| // No -e flag and no default environment set | ||
| lazyEnv := lazy.NewLazy(func() (*environment.Environment, error) { | ||
| return nil, environment.ErrNameNotSpecified | ||
| }) | ||
| provisionManager := newTestProvisionManager(mockContext, lazyEnv, envManager) | ||
|
|
||
| action := newTestDownAction(t, mockContext, envManager, lazyEnv, provisionManager) | ||
|
|
||
| _, err := action.Run(*mockContext.Context) | ||
| require.Error(t, err) | ||
|
|
||
| var suggestionErr *internal.ErrorWithSuggestion | ||
| require.True(t, errors.As(err, &suggestionErr)) | ||
| require.Contains(t, suggestionErr.Error(), "no environment selected") | ||
| require.Contains(t, suggestionErr.Suggestion, "azd env select") | ||
| } | ||
|
|
||
| func Test_DownAction_EnvironmentExists_ProceedsToProvisioning(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| envManager := &mockenv.MockEnvManager{} | ||
| envManager.On("InvalidateEnvCache", mock.Anything, mock.Anything).Return(nil) | ||
|
|
||
| env := environment.NewWithValues("test-env", nil) | ||
| lazyEnv := lazy.From(env) | ||
| provisionManager := newTestProvisionManager(mockContext, lazyEnv, envManager) | ||
|
|
||
| action := newTestDownAction(t, mockContext, envManager, lazyEnv, provisionManager) | ||
|
|
||
| _, err := action.Run(*mockContext.Context) | ||
| // The action must get past the env check and reach provisioning. | ||
| // It will fail on Initialize (no IaC provider in mock container), which is expected. | ||
| // The key assertion is: the error is NOT an env-check error. | ||
| require.Error(t, err) | ||
| var suggestionErr *internal.ErrorWithSuggestion | ||
| require.False(t, errors.As(err, &suggestionErr), "Expected a provisioning error, not an env-check error") | ||
| } | ||
vhvb1989 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,14 @@ import ( | |
| "github.com/azure/azure-dev/cli/azd/pkg/ext" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/input" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/ioc" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/lazy" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/output/ux" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/project" | ||
| ) | ||
|
|
||
| type HooksMiddleware struct { | ||
| envManager environment.Manager | ||
| env *environment.Environment | ||
| lazyEnv *lazy.Lazy[*environment.Environment] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make sure this doesn't regress #3920? This area has gone through a lot of churn recently:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| projectConfig *project.ProjectConfig | ||
| importManager *project.ImportManager | ||
| commandRunner exec.CommandRunner | ||
|
|
@@ -32,7 +33,7 @@ type HooksMiddleware struct { | |
| // Creates a new instance of the Hooks middleware | ||
| func NewHooksMiddleware( | ||
| envManager environment.Manager, | ||
| env *environment.Environment, | ||
| lazyEnv *lazy.Lazy[*environment.Environment], | ||
| projectConfig *project.ProjectConfig, | ||
| importManager *project.ImportManager, | ||
| commandRunner exec.CommandRunner, | ||
|
|
@@ -42,7 +43,7 @@ func NewHooksMiddleware( | |
| ) Middleware { | ||
| return &HooksMiddleware{ | ||
| envManager: envManager, | ||
| env: env, | ||
| lazyEnv: lazyEnv, | ||
| projectConfig: projectConfig, | ||
| importManager: importManager, | ||
| commandRunner: commandRunner, | ||
|
|
@@ -81,6 +82,11 @@ func (m *HooksMiddleware) registerCommandHooks( | |
| return next(ctx) | ||
| } | ||
|
|
||
| env, err := m.lazyEnv.GetValue() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| hooksManager := ext.NewHooksManager(m.projectConfig.Path, m.commandRunner) | ||
| hooksRunner := ext.NewHooksRunner( | ||
| hooksManager, | ||
|
|
@@ -89,7 +95,7 @@ func (m *HooksMiddleware) registerCommandHooks( | |
| m.console, | ||
| m.projectConfig.Path, | ||
| m.projectConfig.Hooks, | ||
| m.env, | ||
| env, | ||
| m.serviceLocator, | ||
| ) | ||
|
|
||
|
|
@@ -98,7 +104,7 @@ func (m *HooksMiddleware) registerCommandHooks( | |
| commandNames := []string{m.options.CommandPath} | ||
| commandNames = append(commandNames, m.options.Aliases...) | ||
|
|
||
| err := hooksRunner.Invoke(ctx, commandNames, func() error { | ||
| err = hooksRunner.Invoke(ctx, commandNames, func() error { | ||
| result, err := next(ctx) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -131,6 +137,11 @@ func (m *HooksMiddleware) registerServiceHooks(ctx context.Context) error { | |
| continue | ||
| } | ||
|
|
||
| env, err := m.lazyEnv.GetValue() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| serviceHooksManager := ext.NewHooksManager(service.Path(), m.commandRunner) | ||
| serviceHooksRunner := ext.NewHooksRunner( | ||
| serviceHooksManager, | ||
|
|
@@ -139,7 +150,7 @@ func (m *HooksMiddleware) registerServiceHooks(ctx context.Context) error { | |
| m.console, | ||
| service.Path(), | ||
| service.Hooks, | ||
| m.env, | ||
| env, | ||
| m.serviceLocator, | ||
| ) | ||
|
|
||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.

Uh oh!
There was an error while loading. Please reload this page.