Vscode-code-runner: How to I run code with WSL?

Created on 18 Jun 2018  路  9Comments  路  Source: formulahendry/vscode-code-runner

I setup WSL Ubuntu but I don't know how to connect Code-Runner with WSL.
How to I run code with WSL using Code-Runner?

question

Most helpful comment

316 answers how to send commands to wsl, but not how to deal with the file path type problem, which deserves some attention.

The problem

WSL, of course, uses posix paths (/mnt/c/Users/name/projects) but when Code Runner is used in Windows, all of it's file path variables are windows style (C:\Users\name\projects).

Solutions

The lastest WSL insider's release has native (except no -h) support for the wslpath command (more here) for converting path types. It can be used with code-runner.executorMap like this:

"code-runner.executorMap": {
    "powershell": "powershell.exe $(wslpath -w $fullFileName)",
    "bat": "cmd.exe $(wslpath -w $fullFileName)"
}

And to run, for example, a python file in Windows python instead of WSL python:

"code-runner.executorMap": {
    "python": "powershell.exe -command \"python $(wslpath -w $fullFileName)\""
}

For now, this works.

In the long term, I expect some more extensible best practices to develop.

But until then, a "wsl": true switch for Code Runner would go a long way. See the Save and Run with wsl plugin.

All 9 comments

316 answers how to send commands to wsl, but not how to deal with the file path type problem, which deserves some attention.

The problem

WSL, of course, uses posix paths (/mnt/c/Users/name/projects) but when Code Runner is used in Windows, all of it's file path variables are windows style (C:\Users\name\projects).

Solutions

The lastest WSL insider's release has native (except no -h) support for the wslpath command (more here) for converting path types. It can be used with code-runner.executorMap like this:

"code-runner.executorMap": {
    "powershell": "powershell.exe $(wslpath -w $fullFileName)",
    "bat": "cmd.exe $(wslpath -w $fullFileName)"
}

And to run, for example, a python file in Windows python instead of WSL python:

"code-runner.executorMap": {
    "python": "powershell.exe -command \"python $(wslpath -w $fullFileName)\""
}

For now, this works.

In the long term, I expect some more extensible best practices to develop.

But until then, a "wsl": true switch for Code Runner would go a long way. See the Save and Run with wsl plugin.

@garytyler Did you set WSL for the VS Code terminal? If yes, the Code Runner would automatically change the file path. Refer to #106

@formulahendry Yes, I have. I will have a look at #106 and see if I missed anything. Thanks!

@formulahendry, sorry for the delay. It鈥檚 working well now.

Good to know it is working.

@garytyler How did you get it to work? I already have bash set up as my default shell in vscode settings but code runner still tries to run a windows version of node for me

Nvm got it working. Just had to also add:

"code-runner.runInTerminal": true

in addition to

"code-runner.terminalRoot": "/mnt/",

I also setup WSL Ubuntu but I can't run with WSL in right the path.
How to I run code with WSL in the right path?

This is my settings.json
{ "code-runner.runInTerminal": true, "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe", "code-runner.executorMap": { "c": "cd $(wslpath $dir) & gcc $fileName -o $fileNameWithoutExt & ./$fileNameWithoutExt", "cpp": "g++ $fileName -o $fileNameWithoutExt -std=c++11 & ./$fileNameWithoutExt"}, "python": "python3 -u ./$fileName", }

When I run my code with "ctrl+alt+N"
The output became:
jamesqian@J:/mnt/c/Users/Jamesqian/Desktop/tmp$ cd $(wslpath "c:\Users\Jamesqian\Desktop\tmp\") & gcc test.c -o test & ./test

wslpath is https://github.com/laurent22/wslpath

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rickmed picture rickmed  路  4Comments

w3adventures picture w3adventures  路  3Comments

mjaniec2013 picture mjaniec2013  路  5Comments

sstorey-nephila picture sstorey-nephila  路  5Comments

ElektroStudios picture ElektroStudios  路  4Comments