VS Code:使用WSL/Bash运行任务

6

WSL使得获取gcc及其他Linux工具变得非常容易。你可以使用一个脚本快速配置WSL开发环境,如果你搞砸了什么东西,你可以快速销毁它并以最小的代价重新开始。因此,我想在WSL上运行任务而不是在Windows上运行。

例如,在Sublime3中,我可以通过创建以下代码的新构建系统来使用WSL安装的GCC构建C代码:

{
  "cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
  "shell": true,
  "working_dir": "${file_path}",
}

我一直在尝试在VS Code中复制这个操作,但到目前为止没有成功。看起来至少有一个人已经设法使此功能正常 运行,因此应该是可行的。
似乎正确的方法是配置一个tasks.json文件。相关行似乎是:
"command": "bash.exe",
    "args": ["-c", "'insert code here'"]

似乎这个命令告诉PowerShell运行bash -c '插入代码'。有人能提供正确的命令吗?
一些背景信息: ${file}是一个变量,表示VSCode中活动文件。以下是所有预定义变量的列表。 wslpath是Windows 1803中新增的一个bash命令,它接受一个Windows路径并返回wsl路径,例如:
$ wslpath "c:\Users\Evan\Google Drive\Development\Programming Languages\file.c"
/mnt/c/Users/Evan/Google Drive/Development/Programming Languages/file.c

我的尝试:

我尝试编写一个脚本来完成这个任务,但是遇到了问题。调用myscript的tasks.json语法应该像这样(请注意,我们将文件名传递给脚本,以便脚本可以将其传递给GCC)。

"args": ["-c", "'myscript.sh \"${file}\"'"]

或者按照这个答案的说法

"command": "/path/to/script.sh"
"args": ["\"${file}\""]

myscript可能看起来像这样:

#!/bin/bash
echo "File Path is here $1"
x=$(wslpath "$1")
gcc "$x" -o a.out
./a.out

VS Code文档

6个回答

4

这可以在不使用脚本的情况下完成:

 "command": "gcc '$(wslpath '${file}')' && ./a.out"

${file}在bash解析命令之前先被解析。


这个不行。它被路径名中的空格搞混了。我收到以下错误信息:`> 执行任务: gcc $(wslpath 'c:\Users\Evan\Google Drive\Development\Programming Languages\C\sandbox.c') <gcc: error: /mnt/c/Users/Evan/Google: 没有那个文件或目录gcc: error: Drive/Development/Programming: 没有那个文件或目录gcc: error: Languages/C/sandbox.c: 没有那个文件或目录gcc: fatal error: no input filescompilation terminated.The terminal process terminated with exit code: 1` - Evan Rosica
我已经想通了,你只需要添加一组额外的转义引号 \" 来处理这种情况:"command": "gcc \"$(wslpath '${file}')\" && echo done" - Evan Rosica
然后要运行该文件,只需执行以下命令: "gcc \"$(wslpath '${file}')\" -o a.out &&./a.out - Evan Rosica
没有额外的引号对我有效。在 vs-code 1.23.1 和 windows 1803 上工作。 添加了运行可执行命令的答案。 - idanp
你的路径名中有空格吗? - Evan Rosica

2

以下是使用bash命令在vscode的task.json文件中解决问题的另一种快速方法。您几乎可以采用此方法,并插入所需的命令。如果需要,您还可以链接和嵌套命令。

{
    "version": "2.0.0",
    "tasks": [
        {
            // link current file. Uses one or more object.o files
            "label": "link_only_cpp_17",
            "type": "shell",
            "command": "bash", // call bash
            "args": [
                "-c", // read commands from string
                "\"g++ -g -o a $(ls | grep -F .o | tr '\\n' ' ')\"" // link all files ending with .o
            ],
            "problemMatcher": []
        }
    ]
}

这是我的task.json文件的一个示例。它表明您可以使用bash命令并将任意代码传递给它。为此,您必须使用-c标志(从字符串中读取代码),然后将代码作为字符串传递(下一个参数)。在此示例中,g++链接包含.o的所有文件。这些文件是通过$(ls | grep -F .o | tr '\\n' ' ')来查找的。请注意,您必须使用$(),否则您的代码将不会被评估。

据我所知,您无法在传递给bash的命令中使用vscode宏(例如${file}),因为这是一个字符串文字。这就是为什么我使用a而不是文件名的原因。如果您知道如何做到这一点,请让我知道:)

适用于vscode版本1.38.1。


1
一个简单的解决方法:在命令前加上wsl截图

1
为避免更改默认shell,使其在Powershell中工作并避免使用过长的转义序列,最好使用单独的文件。
启动器:
{
            "label": "command name",
            "type": "shell",
            "command": ".\\command_name.bat",
            "problemMatcher": []
}

批处理文件:

bash.exe -c "ssh -T bastion \"for proxy in bunch of different hosts ; do echo \\\$proxy ; ssh \\\$proxy -T -oBatchMode=yes -oStrictHostKeyChecking=no -oConnectTimeout=3 \\\"command --with --parameters\\\" ; done\""

0

我想通了。首先,确保您的用户设置已配置为使用bash

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

请注意,"C:\\Windows\\System32\\bash.exe" 看起来也可以正常工作。

接下来,你的 tasks.json 文件应该包含以下代码:

"type": "shell",                                        //Uses the default shee (make sure this is set to bash)
"command": "'./path to/your/script.sh'",                //Unix Style Path to the Script (remember, bash is your shell reading this).
"args": ["\"${file}\""],                                //Pass the path to the source code as an arg 
"useWSL": true,                                         //This was suggested somewhere. The script runs fine without it though.... Not sure what this does. 

最后,这是脚本。请务必将您的行结尾更改为LF,否则脚本将无法正常工作。

#!/bin/bash
echo "Running Script"
echo "Windows File Path $1"           #The Windows file path we passed in as an argument
wslpath=$(wslpath "$1")               #Convert the Windows path to a Unix Style Path
echo "wslpath is $wslpath"
gcc "$wslpath"                        #Compile the file
echo "done" 
./a.out                               #execute the compiled file

然后打开任何C文件,按下Ctrl + Shift + B并从构建任务菜单中选择脚本名称,将编译C文件到当前目录中的a.out。


0
我试图使用任务来运行一些npm脚本,但是遇到了"zsh:1: command not found: npm"的错误。
但是我通过默认的"vscode/tasks.json"设置解决了问题。
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "serve api",
      "type": "npm",
      "script": "serve-api", // one of the "scripts" entry in my package.json
    },

  ]
}

诀窍是首先打开你的WSL终端,然后从那里启动VSCode: code ~/path/to/my/vscode-project


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接