Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,12 +1151,19 @@ func (ef *envRefreshAction) Run(ctx context.Context) (*actions.ActionResult, err
Title: fmt.Sprintf("Refreshing environment %s (azd env refresh)", ef.env.Name()),
})

// Initialize services, skipping any with unsupported hosts (e.g., extension-provided hosts
// that are not currently loaded). Env refresh only needs infrastructure outputs from Azure;
// services with unsupported hosts simply won't receive the environment-updated event.
if err := ef.projectManager.Initialize(ctx, ef.projectConfig); err != nil {
return nil, err
if !hasUnsupportedHostError(err) {
return nil, err
}
}

if err := ef.projectManager.EnsureAllTools(ctx, ef.projectConfig, nil); err != nil {
return nil, err
if !hasUnsupportedHostError(err) {
return nil, err
}
}
Comment thread
jongio marked this conversation as resolved.

infra, err := ef.importManager.ProjectInfrastructure(ctx, ef.projectConfig)
Expand Down Expand Up @@ -1253,6 +1260,14 @@ func (ef *envRefreshAction) Run(ctx context.Context) (*actions.ActionResult, err
}, nil
}

// hasUnsupportedHostError reports whether err (or any error in its chain) is caused by
// an unsupported service host. This lets env refresh continue when extension-provided hosts
// (e.g., azure.ai.agent) are not loaded.
func hasUnsupportedHostError(err error) bool {
_, ok := errors.AsType[*project.UnsupportedServiceHostError](err)
return ok
}

func newEnvGetValuesFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *envGetValuesFlags {
flags := &envGetValuesFlags{}
flags.Bind(cmd.Flags(), global)
Expand Down
59 changes: 59 additions & 0 deletions cli/azd/cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package cmd

import (
"fmt"
"testing"

"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -170,3 +173,59 @@ func TestNewEnvConfigUnsetCmd(t *testing.T) {
require.Equal(t, "unset <path>", cmd.Use)
require.NotEmpty(t, cmd.Short)
}

func TestHasUnsupportedHostError(t *testing.T) {
t.Parallel()

tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "unrelated error",
err: assert.AnError,
expected: false,
},
{
name: "direct UnsupportedServiceHostError",
err: &project.UnsupportedServiceHostError{
Host: "azure.ai.agent",
ServiceName: "agent",
},
expected: true,
},
{
name: "wrapped UnsupportedServiceHostError",
err: fmt.Errorf("initializing service 'agent', %w", &project.UnsupportedServiceHostError{
Host: "azure.ai.agent",
ServiceName: "agent",
}),
expected: true,
},
{
name: "wrapped in ErrorWithSuggestion",
err: fmt.Errorf("getting service target: %w", &internal.ErrorWithSuggestion{
Err: &project.UnsupportedServiceHostError{
Host: "azure.ai.agent",
ServiceName: "agent",
},
Suggestion: "install an extension",
}),
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := hasUnsupportedHostError(tt.err)
assert.Equal(t, tt.expected, got)
})
}
}
Loading