我可以使用 vscode 1.31.1(用户设置)和 Microsoft Windows 10.0.17134.619(运行 Ubuntu 18.04)附带的 WSL 重现相同的问题。
当 vscode 中的默认 shell 配置为 WSL Bash 时,以下 tasks.json 文件将失败并报上述错误:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g", "helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true,
}
}
]
}
上面的 tasks.json 文件是最新的 vscode 文档中建议的文件(请参阅https://code.visualstudio.com/docs/languages/cpp),但实际上并不能正常工作。看起来,无论出于何种原因,vscode 向终端发出的最终命令格式不正确。这应该看起来像“bash -c g++ ...”。如果可以在某处回显此命令,那将非常有帮助,这样错误就会很明显。
如果默认 shell 配置为保留 Windows 命令提示符,并且修改 tasks.json 文件以完全控制通过“选项”字段形成的构建终端命令的方式,问题就解决了,如下所示:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build hello world",
"type": "shell",
"command": "",
"args": [
"g++", "-g", "helloworld.cpp"
],
"problemMatcher":"$gcc",
"group": {
"kind": "build",
"isDefault": true,
},
"options": {
"shell": {
"executable": "C:\\WINDOWS\\System3\\bash.exe",
"args":["-c"]
}
}
}
]
}
上述工作tasks.json 文件使用https://code.visualstudio.com/docs/editor/tasks 中描述的选项。请注意,我添加了“problemMatcher”选项。它与此处描述的问题无关,但它对于解析 gcc 输出和定位源代码中的错误变得很方便。
因此,在您的情况下,您需要:
将默认的 vscode 终端设置为命令提示符
-
将选项添加到您的 tasks.json 文件中:
"options": {
"shell": {
"executable": "C:\\WINDOWS\\System3\\bash.exe",
"args":["-c"]
}
结合您的原始 tasks.json 文件,以上内容应导致在您的 WSL 工作文件夹中发出格式良好的“make”命令。