Repository: xonsh/xonsh

Issue:
git completion on windows broken
Git completions on windows has stopped working:
```
snail@sea ~\Documents\xonsh master $ $BASH_COMPLETIONS
['C:\\Program Files\\Git\\usr\\share\\bash-completion\\bash_completion', 
'C:\\Program Files\\Git\\mingw64\\share\\git\\completion\\git-completion.bash']
```

I guess it could be some of the recent fixes to bash completion scripts. Do we require bash completion 2.0? 

On windows we use the git bash completions that come bundled with 'Git for Windows'. I don't think that 2.0



Discussion:
How are you getting the completions on windows? I am on conda's git and it doesn't seem to come with the completions
Also! Thanks for reporting!
'git for Windows' comes with MSys which has bash completion. Mostly for the Git bash shell, but xonsh is configured to detect if it is installed and then use it git complete script. Although now it seems to not work anymore. 

Unless anyone has some quick insights, then I will dig into this. That was one of my favorite features 
I think v1 is supported still, as per http://xon.sh/envvars.html#bash-completions.  This is definitely something we shouldn't leave broken.
There was a bug report about bash completions recently and some discussion about v1 vs v2 support.
This is a mixture of several bugs, and me having the 'Windows Subsystem for Linux (WSL)' installed. WSL bash can't be used for anything in this regard.

Bash from WSL is in `C:\Windows\system32\bash.exe` and is always picked by the subprocess even though I have the 'Git for Windows' bash first on my xonsh path. It seems like subprocess doesn't respect the environment that is passed in here: (https://github.com/xonsh/xonsh/blob/master/xonsh/completers/bash.py#L92). 

I found this (https://bugs.python.org/issue15451) that states that subprocess doesn't use the passed in `env` when searching the for the executable on Windows. It seems to do that on Linux. That was surprising to me. 

This means that we should use the `locate_binary` instead in `xonsh.platform.bash_command()` when determining if bash is on the path: https://github.com/xonsh/xonsh/blob/master/xonsh/platform.py#L289

And maybe some code to prevent us from picking up the WSL bash.exe if it is installed. 

I have a PR incoming.



Resolving patch:
diff --git a/xonsh/platform.py b/xonsh/platform.py
--- a/xonsh/platform.py
+++ b/xonsh/platform.py
@@ -6,6 +6,7 @@
 import sys
 import signal
 import pathlib
+import builtins
 import platform
 import functools
 import subprocess
@@ -285,16 +286,22 @@ def windows_bash_command():
     # Check that bash is on path otherwise try the default directory
     # used by Git for windows
     wbc = 'bash'
-    try:
-        subprocess.check_call([wbc, '--version'],
-                              stdout=subprocess.PIPE,
-                              stderr=subprocess.PIPE)
-    except (FileNotFoundError, subprocess.CalledProcessError):
-        gfwp = git_for_windows_path()
-        if gfwp:
-            bashcmd = os.path.join(gfwp, 'bin\\bash.exe')
-            if os.path.isfile(bashcmd):
-                wbc = bashcmd
+    bash_on_path = builtins.__xonsh_commands_cache__.lazy_locate_binary('bash',
+                                                                        ignore_alias=True)
+    if bash_on_path:
+        # Check if Bash is from the "Windows Subsystem for Linux" (WSL)
+        # which can't be used by xonsh foreign-shell/completer
+        out = subprocess.check_output([bash_on_path, '--version'],
+                                      stderr=subprocess.PIPE,
+                                      universal_newlines=True)
+        if 'pc-linux-gnu' in out.splitlines()[0]:
+            gfwp = git_for_windows_path()
+            if gfwp:
+                bashcmd = os.path.join(gfwp, 'bin\\bash.exe')
+                if os.path.isfile(bashcmd):
+                    wbc = bashcmd
+        else:
+            wbc = bash_on_path
     return wbc
 
 #


Test patch: