From 5a0f269d85c3bc8a64aa6ee1f3cea4d072d070cb Mon Sep 17 00:00:00 2001 From: Nick Young Date: Thu, 5 Sep 2019 00:23:31 +1000 Subject: [PATCH] fix: Be more restrictive with bash init fallback (#278) This should improve compatibility with "Git Bash" with Git for Windows by using psub+source init for all bash shells v4.1 and newer. --- src/init.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/init.rs b/src/init.rs index 41aaac04..92390c75 100644 --- a/src/init.rs +++ b/src/init.rs @@ -32,12 +32,40 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> { let setup_stub = match shell_basename { Some("bash") => { - /* This *should* look like the zsh function, but bash 3.2 (MacOS default shell) - does not support using source with process substitution, so we use this - workaround from https://stackoverflow.com/a/32596626 */ + /* + * The standard bash bootstrap is: + * `source <(starship init bash --print-full-init)` + * + * Unfortunately there is an issue with bash 3.2 (the MacOS + * default) which prevents this from working. It does not support + * `source` with process substitution. + * + * There are more details here: https://stackoverflow.com/a/32596626 + * + * The workaround for MacOS is to use the `/dev/stdin` trick you + * see below. However, there are some systems with emulated POSIX + * environments which do not support `/dev/stdin`. For example, + * `Git Bash` within `Git for Windows and `Termux` on Android. + * + * Fortunately, these apps ship with recent-ish versions of bash. + * Git Bash is currently shipping bash 4.4 and Termux is shipping + * bash 5.0. + * + * Some testing has suggested that bash 4.0 is also incompatible + * with the standard bootstrap, whereas bash 4.1 appears to be + * consistently compatible. + * + * The upshot of all of this, is that we will use the standard + * bootstrap whenever the bash version is 4.1 or higher. Otherwise, + * we fall back to the `/dev/stdin` solution. + * + * More background can be found in these pull requests: + * https://github.com/starship/starship/pull/241 + * https://github.com/starship/starship/pull/278 + */ let script = { format!( - r#"if [ "${{BASH_VERSINFO[0]}}" -gt 4 ] + r#"if [ "${{BASH_VERSINFO[0]}}" -gt 4 ] || ([ "${{BASH_VERSINFO[0]}}" -eq 4 ] && [ "${{BASH_VERSINFO[1]}}" -ge 1 ]) then source <("{}" init bash --print-full-init) else