diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 37c3d5b70ff..7a78c5b3df3 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -124,7 +124,7 @@ func (h *HooksRunner) GetScript(hookConfig *HookConfig, envVars []string) (tools return nil, err } - switch hookConfig.Shell { + switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) { case ShellTypeBash: return bash.NewBashScript(h.commandRunner, h.cwd, envVars), nil case ShellTypePowershell: @@ -190,6 +190,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 500e371419e..531c4631314 100644 --- a/cli/azd/pkg/ext/models.go +++ b/cli/azd/pkg/ext/models.go @@ -172,7 +172,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 05ec99050ab..659dac724b9 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -28,7 +28,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 5c9eebe6113..d0c7d2ddc5a 100644 --- a/cli/azd/pkg/tools/powershell/powershell_test.go +++ b/cli/azd/pkg/tools/powershell/powershell_test.go @@ -26,10 +26,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) @@ -41,7 +43,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 21d6f497889..2526b78bdf3 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -14,6 +14,7 @@ import ( type ExecOptions struct { Interactive *bool StdOut io.Writer + UserPwsh string } // Utility to easily execute a bash script across platforms