diff --git a/cli/azd/pkg/ai/python_bridge.go b/cli/azd/pkg/ai/python_bridge.go index 02a858082cb..2e8da158b4b 100644 --- a/cli/azd/pkg/ai/python_bridge.go +++ b/cli/azd/pkg/ai/python_bridge.go @@ -7,7 +7,7 @@ import ( "os" "path/filepath" - "github.com/azure/azure-dev/cli/azd/pkg/environment" + "github.com/azure/azure-dev/cli/azd/pkg/config" "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext" "github.com/azure/azure-dev/cli/azd/pkg/exec" "github.com/azure/azure-dev/cli/azd/pkg/osutil" @@ -36,7 +36,6 @@ type PythonBridge interface { // pythonBridge is a bridge to execute python components from the embedded AI resources project type pythonBridge struct { azdCtx *azdcontext.AzdContext - env *environment.Environment pythonCli *python.PythonCli workingDir string initialized bool @@ -45,12 +44,10 @@ type pythonBridge struct { // NewPythonBridge creates a new PythonBridge instance func NewPythonBridge( azdCtx *azdcontext.AzdContext, - env *environment.Environment, pythonCli *python.PythonCli, ) PythonBridge { return &pythonBridge{ azdCtx: azdCtx, - env: env, pythonCli: pythonCli, } } @@ -84,8 +81,12 @@ func (b *pythonBridge) Run(ctx context.Context, scriptName ScriptPath, args ...s // initPython initializes the Python environment func (b *pythonBridge) initPython(ctx context.Context) error { - envDir := b.azdCtx.EnvironmentRoot(b.env.Name()) - targetDir := filepath.Join(envDir, "ai") + configDir, err := config.GetUserConfigDir() + if err != nil { + return err + } + + targetDir := filepath.Join(configDir, "bin", "ai") if _, err := os.Stat(targetDir); os.IsNotExist(err) { if err := os.MkdirAll(targetDir, osutil.PermissionDirectory); err != nil { return err diff --git a/cli/azd/pkg/ai/python_bridge_test.go b/cli/azd/pkg/ai/python_bridge_test.go index 517d1357bb9..fe19193cbbe 100644 --- a/cli/azd/pkg/ai/python_bridge_test.go +++ b/cli/azd/pkg/ai/python_bridge_test.go @@ -2,11 +2,12 @@ package ai import ( "context" + "os" "path/filepath" "strings" "testing" - "github.com/azure/azure-dev/cli/azd/pkg/environment" + "github.com/azure/azure-dev/cli/azd/pkg/config" "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext" "github.com/azure/azure-dev/cli/azd/pkg/exec" "github.com/azure/azure-dev/cli/azd/pkg/tools/python" @@ -15,10 +16,13 @@ import ( ) func Test_PythonBridge_Init(t *testing.T) { + tempDir := t.TempDir() mockContext := mocks.NewMockContext(context.Background()) - env := environment.New("test") pythonCli := python.NewPythonCli(mockContext.CommandRunner) - azdCtx := azdcontext.NewAzdContextWithDirectory(t.TempDir()) + azdCtx := azdcontext.NewAzdContextWithDirectory(tempDir) + + azdConfigDir := filepath.Join(tempDir, ".azd") + os.Setenv("AZD_CONFIG_DIR", azdConfigDir) createdVirtualEnv := false installedDependencies := false @@ -37,10 +41,13 @@ func Test_PythonBridge_Init(t *testing.T) { return exec.NewRunResult(0, "", ""), nil }) - aiDir := filepath.Join(azdCtx.EnvironmentRoot(env.Name()), "ai") + userConfigDir, err := config.GetUserConfigDir() + require.NoError(t, err) + + aiDir := filepath.Join(userConfigDir, "bin", "ai") - bridge := NewPythonBridge(azdCtx, env, pythonCli) - err := bridge.Initialize(*mockContext.Context) + bridge := NewPythonBridge(azdCtx, pythonCli) + err = bridge.Initialize(*mockContext.Context) require.NoError(t, err) require.DirExists(t, aiDir) require.True(t, createdVirtualEnv) @@ -49,7 +56,6 @@ func Test_PythonBridge_Init(t *testing.T) { func Test_PythonBridge_Run(t *testing.T) { mockContext := mocks.NewMockContext(context.Background()) - env := environment.New("test") pythonCli := python.NewPythonCli(mockContext.CommandRunner) azdCtx := azdcontext.NewAzdContextWithDirectory(t.TempDir()) @@ -61,7 +67,7 @@ func Test_PythonBridge_Run(t *testing.T) { return exec.NewRunResult(0, "result", ""), nil }) - bridge := NewPythonBridge(azdCtx, env, pythonCli) + bridge := NewPythonBridge(azdCtx, pythonCli) result, err := bridge.Run(*mockContext.Context, "script.py", "arg1", "arg2") require.NoError(t, err) require.NotNil(t, result)