Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Git with VS Code and Bash on Ubuntu on Windows (WSL)

I couldn't figure out how to integrate WSL with VS Code. I can open the integrated terminal using:

"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"

The integrated terminal works. However, I can't use source control or any of the linting features of VS Code. At the source control menu, it says "There are no active source control providers.".

The problem is probably caused by the path of git but I couldn't figure out how to solve the problem. I would appreciate any help. Thank you.

like image 339
batatop Avatar asked Oct 09 '17 20:10

batatop


People also ask

Can you use git in WSL?

Git can be installed on Windows AND on WSL The root of your file system / is the mount point of your root partition, or folder, in the case of WSL.

Can you run VS Code on WSL?

Once you've opened a folder in WSL, you can use VS Code's debugger in the same way you would when running the application locally. For example, if you select a launch configuration in launch.json and start debugging (F5), the application will start on remote host and attach the debugger to it.


2 Answers

According to this article you have to write a batch file

@echo off
bash.exe -c "git %*"

And tell VsCode git plugin to target this bat file. (With the terminal set to use bash as you did)

You can do that for all your linters / sniffers / helpers plugins.

Hope this can help ... and work ;-)

like image 157
pitrackster Avatar answered Oct 17 '22 00:10

pitrackster


None of these options worked for me, so I built my own!

You can find my full solution here: https://gist.github.com/onecrayon/de756538d5331f7592065507c03b1864

In short: you need to proxy through a batch file (as suggested by pitrackster), but their batch file will fail when used with VSC because it doesn't properly escape the proxied command and fails to convert paths to and from Windows and Unix.


For posterity, here's the scripts I linked above at the time of this posting, sans ReadMe:

git.bat

@echo off

:: %~dp0 is the directory of this batch file
set proxy_path=%~dp0_proxy_cmd.bat
:: %* is the full command passed to this batch file
%proxy_path% git %*

_proxy_cmd.bat

@echo off

:: Properly escape the command
:: Many thanks to wsl-alias for this logic: https://github.com/leongrdic/wsl-alias
set cmd=%*
set cmd=%cmd:\"=\\"%
set cmd=%cmd:\'=\\'%
set cmd=%cmd:\=/%
set cmd=%cmd://=\\%
set cmd=%cmd:"=\"%
set cmd=%cmd:'=\'%
set cmd=%cmd:(=\(%
set cmd=%cmd:)=\)%

:: Grab the path to our proxy Bash script (%~dp0 is the directory of this batch file)
set bash_proxy=%~dp0_proxy_cmd.sh
set bash_proxy=%bash_proxy:\=\\%

:: Pass things off to the Bash script
bash.exe -c "$(wslpath %bash_proxy%) %cmd%"

_proxy_cmd.sh

##
# Evaluates command, parsing paths at both ends (Windows => Unix => Windows)
#
# Benefits to doing this instead of directly invoking the command in the batch file:
# 
# + Easier to convert path arguments to Unix paths
# + sed regex does not require double escaping backslashes (not embedded in double quotes)
##

cmd=()
for arg in "$@"
do
    if [[ $arg =~ ^[a-zA-Z]:/ ]]; then
        cmd+=( $(wslpath "$arg") )
    else
        cmd+=("$arg")
    fi
done

# TODO: Look into ways to convert inline paths via `wslpath` instead of hard-coding `/mnt` search
# Kind of a tricky issue, because our output could be basically anything
eval "${cmd[@]}" | sed \
    -e 's|"/mnt/\([a-zA-Z]\)/\([^"]*\)"|"\1:/\2"|g' \
    -e "s|'/mnt/\\([a-zA-Z]\\)/\\([^']*\\)'|'\\1:/\\2'|g" \
    -e 's|/mnt/\([A-Za-z]\)/\([^ ]*\)|\1:/\2|g' \
    -e 's|/|\\|g'
like image 43
One Crayon Avatar answered Oct 16 '22 23:10

One Crayon