@@ -88,49 +88,34 @@ type SubShell interface {
8888
8989// New returns the subshell relevant to the current process, but does not activate it
9090func New (cfg sscommon.Configurable ) SubShell {
91- binary := resolveBinaryPath (DetectShellBinary (cfg ))
92-
93- name := filepath .Base (binary )
94- name = strings .TrimSuffix (name , filepath .Ext (name ))
95- logging .Debug ("Detected SHELL: %s" , name )
96-
97- if runtime .GOOS == "windows" {
98- // For some reason Go or MSYS doesn't translate paths with spaces correctly, so we have to strip out the
99- // invalid escape characters for spaces
100- binary = strings .ReplaceAll (binary , `\ ` , ` ` )
101- }
91+ name , path := DetectShell (cfg )
10292
10393 var subs SubShell
10494 switch name {
105- case " bash" :
95+ case bash . Name :
10696 subs = & bash.SubShell {}
107- case " zsh" :
97+ case zsh . Name :
10898 subs = & zsh.SubShell {}
109- case " tcsh" :
99+ case tcsh . Name :
110100 subs = & tcsh.SubShell {}
111- case " fish" :
101+ case fish . Name :
112102 subs = & fish.SubShell {}
113- case " cmd" :
103+ case cmd . Name :
114104 subs = & cmd.SubShell {}
115105 default :
116- logging .Debug ("Unsupported shell: %s, defaulting to OS default." , name )
117- rollbar .Error ("Unsupported shell: %s" , name ) // we just want to know what this person is using
106+ rollbar .Error ("subshell.DetectShell did not return a known name: %s" , name )
118107 switch runtime .GOOS {
119108 case "windows" :
120- binary = "cmd.exe"
121109 subs = & cmd.SubShell {}
122110 case "darwin" :
123- binary = "zsh"
124111 subs = & zsh.SubShell {}
125112 default :
126- binary = "bash"
127113 subs = & bash.SubShell {}
128114 }
129- binary = resolveBinaryPath (binary )
130115 }
131116
132- logging .Debug ("Using binary: %s" , binary )
133- subs .SetBinary (binary )
117+ logging .Debug ("Using binary: %s" , path )
118+ subs .SetBinary (path )
134119
135120 env := funk .FilterString (os .Environ (), func (s string ) bool {
136121 return ! strings .HasPrefix (s , constants .ProjectEnvVarName )
@@ -182,8 +167,10 @@ func ConfigureAvailableShells(shell SubShell, cfg sscommon.Configurable, env map
182167 return nil
183168}
184169
185- func DetectShellBinary (cfg sscommon.Configurable ) (binary string ) {
170+ // DetectShell detects the shell relevant to the current process and returns its name and path.
171+ func DetectShell (cfg sscommon.Configurable ) (string , string ) {
186172 configured := cfg .GetString (ConfigKeyShell )
173+ var binary string
187174 defer func () {
188175 // do not re-write shell binary to config, if the value did not change.
189176 if configured == binary {
@@ -196,25 +183,56 @@ func DetectShellBinary(cfg sscommon.Configurable) (binary string) {
196183 }
197184 }()
198185
199- if binary := os .Getenv ("SHELL" ); binary != "" {
200- return binary
201- }
202-
203- if runtime .GOOS == "windows" {
186+ binary = os .Getenv ("SHELL" )
187+ if binary == "" && runtime .GOOS == "windows" {
204188 binary = os .Getenv ("ComSpec" )
205- if binary != "" {
206- return binary
207- }
208189 }
209190
210- fallback := configured
211- if fallback == "" {
191+ if binary == "" {
192+ binary = configured
193+ }
194+ if binary == "" {
212195 if runtime .GOOS == "windows" {
213- fallback = "cmd.exe"
196+ binary = "cmd.exe"
214197 } else {
215- fallback = "bash"
198+ binary = "bash"
199+ }
200+ }
201+
202+ path := resolveBinaryPath (binary )
203+
204+ name := filepath .Base (path )
205+ name = strings .TrimSuffix (name , filepath .Ext (name ))
206+ logging .Debug ("Detected SHELL: %s" , name )
207+
208+ if runtime .GOOS == "windows" {
209+ // For some reason Go or MSYS doesn't translate paths with spaces correctly, so we have to strip out the
210+ // invalid escape characters for spaces
211+ path = strings .ReplaceAll (path , `\ ` , ` ` )
212+ }
213+
214+ isKnownShell := false
215+ for _ , ssName := range []string {bash .Name , cmd .Name , fish .Name , tcsh .Name , zsh .Name } {
216+ if name == ssName {
217+ isKnownShell = true
218+ break
219+ }
220+ }
221+ if ! isKnownShell {
222+ logging .Debug ("Unsupported shell: %s, defaulting to OS default." , name )
223+ rollbar .Error ("Unsupported shell: %s" , name ) // we just want to know what this person is using
224+ switch runtime .GOOS {
225+ case "windows" :
226+ name = cmd .Name
227+ path = resolveBinaryPath ("cmd.exe" )
228+ case "darwin" :
229+ name = zsh .Name
230+ path = resolveBinaryPath ("zsh" )
231+ default :
232+ name = bash .Name
233+ path = resolveBinaryPath ("bash" )
216234 }
217235 }
218236
219- return fallback
237+ return name , path
220238}
0 commit comments