diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 2d87378568b..0acce4eb0ff 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -116,7 +116,7 @@ func (h *HooksRunner) GetScript(hookConfig *HookConfig) (tools.Script, error) { return nil, err } - switch hookConfig.Shell { + switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) { case ShellTypeBash: return bash.NewBashScript(h.commandRunner, h.cwd, h.env.Environ()), nil case ShellTypePowershell: @@ -158,6 +158,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti options.StdOut = previewer defer h.console.StopPreviewer(ctx, false) } + options.UserPwsh = string(hookConfig.Shell) log.Printf("Executing script '%s'\n", hookConfig.path) res, err := script.Execute(ctx, hookConfig.path, *options) diff --git a/cli/azd/pkg/ext/models.go b/cli/azd/pkg/ext/models.go index 1100c6a5ee6..c3439bdafe8 100644 --- a/cli/azd/pkg/ext/models.go +++ b/cli/azd/pkg/ext/models.go @@ -166,7 +166,7 @@ func createTempScript(hookConfig *HookConfig) (string, error) { scriptHeader := []string{} scriptFooter := []string{} - switch hookConfig.Shell { + switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) { case ShellTypeBash: ext = "sh" scriptHeader = []string{ diff --git a/cli/azd/pkg/tools/powershell/powershell.go b/cli/azd/pkg/tools/powershell/powershell.go index ca9fad66290..78bbcc98700 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -25,7 +25,7 @@ type powershellScript struct { // Executes the specified powershell script // When interactive is true will attach to stdin, stdout & stderr func (bs *powershellScript) Execute(ctx context.Context, path string, options tools.ExecOptions) (exec.RunResult, error) { - runArgs := exec.NewRunArgs("pwsh", path). + runArgs := exec.NewRunArgs(options.UserPwsh, path). WithCwd(bs.cwd). WithEnv(bs.envVars). WithShell(true) diff --git a/cli/azd/pkg/tools/powershell/powershell_test.go b/cli/azd/pkg/tools/powershell/powershell_test.go index 2a41c707913..ec8c9fd1633 100644 --- a/cli/azd/pkg/tools/powershell/powershell_test.go +++ b/cli/azd/pkg/tools/powershell/powershell_test.go @@ -23,10 +23,12 @@ func Test_Powershell_Execute(t *testing.T) { t.Run("Success", func(t *testing.T) { mockContext := mocks.NewMockContext(context.Background()) + // #nosec G101 + userPwsh := "pwsh -NoProfile" mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool { return true }).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) { - require.Equal(t, "pwsh", args.Cmd) + require.Equal(t, userPwsh, args.Cmd) require.Equal(t, workingDir, args.Cwd) require.Equal(t, scriptPath, args.Args[0]) require.Equal(t, env, args.Env) @@ -38,7 +40,7 @@ func Test_Powershell_Execute(t *testing.T) { runResult, err := PowershellScript.Execute( *mockContext.Context, scriptPath, - tools.ExecOptions{Interactive: to.Ptr(true)}, + tools.ExecOptions{UserPwsh: userPwsh, Interactive: to.Ptr(true)}, ) require.NotNil(t, runResult) diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index f111db99400..28216b14fc1 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -11,6 +11,7 @@ import ( type ExecOptions struct { Interactive *bool StdOut io.Writer + UserPwsh string } // Utility to easily execute a bash script across platforms