diff --git a/internal/stack/shellinit.go b/internal/stack/shellinit.go index 2e22e1a693..a3db128a6e 100644 --- a/internal/stack/shellinit.go +++ b/internal/stack/shellinit.go @@ -109,8 +109,16 @@ func helpText(shell string) string { func getShellName(exe string) string { shell := filepath.Base(exe) - // NOTE: remove .exe extension from executable names present in Windows - shell = strings.TrimSuffix(shell, ".exe") + cleanSuffixes := []string{ + // Remove .exe extension from executable names present in Windows. + ".exe", + // Remove " (deleted)", that can appear here if the shell process has been + // replaced by an upgrade in Linux while the terminal was open. + " (deleted)", + } + for _, suffix := range cleanSuffixes { + shell = strings.TrimSuffix(shell, suffix) + } return shell } diff --git a/internal/stack/shellinit_internal_test.go b/internal/stack/shellinit_internal_test.go index 80c9114cad..cedc3a8b25 100644 --- a/internal/stack/shellinit_internal_test.go +++ b/internal/stack/shellinit_internal_test.go @@ -5,6 +5,7 @@ package stack import ( + "path/filepath" "strings" "testing" @@ -44,13 +45,24 @@ func Test_getShellName(t *testing.T) { type args struct { exe string } - tests := []struct { + type testCase struct { name string args args want string - }{ - {"linux exec", args{exe: "bash"}, "bash"}, - {"windows exec", args{exe: "cmd.exe"}, "cmd"}, + } + var tests []testCase + if filepath.Separator == '\\' { + tests = []testCase{ + {"windows relative path", args{exe: `cmd.exe`}, "cmd"}, // Not sure if this case is real. + {"windows exec", args{exe: `C:\Windows\System32\cmd.exe`}, "cmd"}, + } + } else { + tests = []testCase{ + {"linux exec", args{exe: "/usr/bin/bash"}, "bash"}, + {"linux upgraded exec", args{exe: "/usr/bin/bash (deleted)"}, "bash"}, + {"windows relative path", args{exe: `cmd.exe`}, "cmd"}, // Not sure if this case is real. + {"windows exec", args{exe: `C:/Windows/System32/cmd.exe`}, "cmd"}, + } } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {