6

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.

7
  • Linting and Source Control are two very distinct questions. Is Git installed on your system, with GitHub Desktop or similar? Commented Oct 9, 2017 at 23:00
  • Is git installed on WSL? Commented Oct 9, 2017 at 23:04
  • @ifconfig Yes it is but it is not installed on Windows. Commented Oct 10, 2017 at 0:00
  • Try installing GitHub Desktop. Is the folder you are in already configured as a git repo? Commented Oct 10, 2017 at 0:09
  • @ifconfig The problem didn't occur when I downloaded the Git on Windows. I'll just stick with the PowerShell for now. Thank you for the help. Commented Oct 10, 2017 at 3:46

3 Answers 3

3

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 ;-)

Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

You need to have Git installed on the host OS, (Windows) as VS Code calls git from cmd, not the Integrated Terminal.

A solution to this issue is to install git for Windows. GitHub Desktop is a good option for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.