From ce29029c8d9be7440bae0d562a247ca48cd8e70e Mon Sep 17 00:00:00 2001 From: Yionse Date: Tue, 3 Dec 2024 14:15:25 +0800 Subject: [PATCH 1/6] :bug: fix issue 4592 --- cli/azd/cmd/hooks.go | 10 +++++++++- cli/azd/pkg/tools/powershell/powershell.go | 10 +++++++++- cli/azd/pkg/tools/script.go | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cli/azd/cmd/hooks.go b/cli/azd/cmd/hooks.go index 09ef37c6b2b..ca0964c76ed 100644 --- a/cli/azd/cmd/hooks.go +++ b/cli/azd/cmd/hooks.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "strings" "github.com/azure/azure-dev/cli/azd/cmd/actions" "github.com/azure/azure-dev/cli/azd/internal" @@ -235,7 +236,14 @@ func (hra *hooksRunAction) execHook( }) defer hra.console.StopPreviewer(ctx, false) - runOptions := &tools.ExecOptions{StdOut: previewer} + isRunProfile := true + if hook.Windows != nil { + if hook.Windows.Run != "" { + isRunProfile = !strings.Contains(strings.ToLower(hook.Windows.Run), "-noprofile") + } + } + runOptions := &tools.ExecOptions{StdOut: previewer, IsRunProfile: isRunProfile} + err := hooksRunner.RunHooks(ctx, hookType, runOptions, commandName) if err != nil { return err diff --git a/cli/azd/pkg/tools/powershell/powershell.go b/cli/azd/pkg/tools/powershell/powershell.go index ca9fad66290..312e03060d9 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -25,7 +25,15 @@ 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). + + cmdConfig := " -NoProfile" + normalCmd := "pwsh" + + if !options.IsRunProfile { + normalCmd += cmdConfig + } + + runArgs := exec.NewRunArgs(normalCmd, path). WithCwd(bs.cwd). WithEnv(bs.envVars). WithShell(true) diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index f111db99400..3499936cf59 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 + IsRunProfile bool } // Utility to easily execute a bash script across platforms From cc5ff74b12c3b8ccc4168590958fe42532a86804 Mon Sep 17 00:00:00 2001 From: Yionse Date: Wed, 4 Dec 2024 10:18:30 +0800 Subject: [PATCH 2/6] resolve ci error --- cli/azd/.vscode/cspell.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/azd/.vscode/cspell.yaml b/cli/azd/.vscode/cspell.yaml index 2c97ad56cf3..61ce97040d6 100644 --- a/cli/azd/.vscode/cspell.yaml +++ b/cli/azd/.vscode/cspell.yaml @@ -13,6 +13,7 @@ words: - usgovcloudapi - chinacloudapi - unmarshals + - noprofile languageSettings: - languageId: go ignoreRegExpList: From b24ef4814d596711c6e1d6e80acd8b70c5bcf0b5 Mon Sep 17 00:00:00 2001 From: Yionse Date: Wed, 4 Dec 2024 10:28:28 +0800 Subject: [PATCH 3/6] resolve gofmt error --- cli/azd/cmd/hooks.go | 2 +- cli/azd/pkg/tools/powershell/powershell.go | 4 ++-- cli/azd/pkg/tools/script.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/azd/cmd/hooks.go b/cli/azd/cmd/hooks.go index ca0964c76ed..c61ab2e363f 100644 --- a/cli/azd/cmd/hooks.go +++ b/cli/azd/cmd/hooks.go @@ -243,7 +243,7 @@ func (hra *hooksRunAction) execHook( } } runOptions := &tools.ExecOptions{StdOut: previewer, IsRunProfile: isRunProfile} - + err := hooksRunner.RunHooks(ctx, hookType, runOptions, commandName) if err != nil { return err diff --git a/cli/azd/pkg/tools/powershell/powershell.go b/cli/azd/pkg/tools/powershell/powershell.go index 312e03060d9..e6d6c3db8aa 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -25,14 +25,14 @@ 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) { - + cmdConfig := " -NoProfile" normalCmd := "pwsh" if !options.IsRunProfile { normalCmd += cmdConfig } - + runArgs := exec.NewRunArgs(normalCmd, path). WithCwd(bs.cwd). WithEnv(bs.envVars). diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index 3499936cf59..379f1734a5f 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -9,8 +9,8 @@ import ( // ExecOptions provide configuration for how scripts are executed type ExecOptions struct { - Interactive *bool - StdOut io.Writer + Interactive *bool + StdOut io.Writer IsRunProfile bool } From f91cc42fa04a56879026299a35d19253043619b1 Mon Sep 17 00:00:00 2001 From: Yionse Date: Tue, 24 Dec 2024 14:51:16 +0800 Subject: [PATCH 4/6] fix --- cli/azd/cmd/hooks.go | 9 +-------- cli/azd/pkg/ext/hooks_runner.go | 8 ++++++++ cli/azd/pkg/tools/powershell/powershell.go | 3 +-- cli/azd/pkg/tools/script.go | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cli/azd/cmd/hooks.go b/cli/azd/cmd/hooks.go index c61ab2e363f..0c57e11d7ea 100644 --- a/cli/azd/cmd/hooks.go +++ b/cli/azd/cmd/hooks.go @@ -3,7 +3,6 @@ package cmd import ( "context" "fmt" - "strings" "github.com/azure/azure-dev/cli/azd/cmd/actions" "github.com/azure/azure-dev/cli/azd/internal" @@ -236,13 +235,7 @@ func (hra *hooksRunAction) execHook( }) defer hra.console.StopPreviewer(ctx, false) - isRunProfile := true - if hook.Windows != nil { - if hook.Windows.Run != "" { - isRunProfile = !strings.Contains(strings.ToLower(hook.Windows.Run), "-noprofile") - } - } - runOptions := &tools.ExecOptions{StdOut: previewer, IsRunProfile: isRunProfile} + runOptions := &tools.ExecOptions{StdOut: previewer} err := hooksRunner.RunHooks(ctx, hookType, runOptions, commandName) if err != nil { diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 2d87378568b..8fba1088bf5 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -159,6 +159,14 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti defer h.console.StopPreviewer(ctx, false) } + isRunWithNoProfile := false + if hookConfig.Shell == "pwsh" { + if hookConfig.Run != "" { + isRunWithNoProfile = strings.Contains(strings.ToLower(hookConfig.Run), "-noprofile") + } + } + options.IsRunWithNoProfile = isRunWithNoProfile + log.Printf("Executing script '%s'\n", hookConfig.path) res, err := script.Execute(ctx, hookConfig.path, *options) if err != nil { diff --git a/cli/azd/pkg/tools/powershell/powershell.go b/cli/azd/pkg/tools/powershell/powershell.go index e6d6c3db8aa..645481b78ec 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -25,11 +25,10 @@ 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) { - cmdConfig := " -NoProfile" normalCmd := "pwsh" - if !options.IsRunProfile { + if options.IsRunWithNoProfile { normalCmd += cmdConfig } diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index 379f1734a5f..414f866b2b4 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -10,8 +10,8 @@ import ( // ExecOptions provide configuration for how scripts are executed type ExecOptions struct { Interactive *bool - StdOut io.Writer - IsRunProfile bool + StdOut io.Writer + IsRunWithNoProfile bool } // Utility to easily execute a bash script across platforms From 93a7252c4ca5f132071785773137fbf632f9496d Mon Sep 17 00:00:00 2001 From: Yionse Date: Tue, 24 Dec 2024 14:57:09 +0800 Subject: [PATCH 5/6] resolve gofmt --- cli/azd/pkg/ext/hooks_runner.go | 2 +- cli/azd/pkg/tools/script.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 8fba1088bf5..f1367497697 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -166,7 +166,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti } } options.IsRunWithNoProfile = isRunWithNoProfile - + log.Printf("Executing script '%s'\n", hookConfig.path) res, err := script.Execute(ctx, hookConfig.path, *options) if err != nil { diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index 414f866b2b4..adda683df4f 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -9,8 +9,8 @@ import ( // ExecOptions provide configuration for how scripts are executed type ExecOptions struct { - Interactive *bool - StdOut io.Writer + Interactive *bool + StdOut io.Writer IsRunWithNoProfile bool } From 94f6becb70981e3f369cf4bec6a53153d99f4431 Mon Sep 17 00:00:00 2001 From: Yionse Date: Wed, 25 Dec 2024 10:17:22 +0800 Subject: [PATCH 6/6] Changed variable naming --- cli/azd/pkg/ext/hooks_runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index f1367497697..ff93e38fc28 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -159,7 +159,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti defer h.console.StopPreviewer(ctx, false) } - isRunWithNoProfile := false + var isRunWithNoProfile bool if hookConfig.Shell == "pwsh" { if hookConfig.Run != "" { isRunWithNoProfile = strings.Contains(strings.ToLower(hookConfig.Run), "-noprofile")