VScode使WSL退出并在任何构建任务上出错

问题描述 投票:0回答:1

我在vscode中有一个构建任务,它在我的默认终端中运行“make”,我将其设置为WSL。问题是,无论我使用什么命令运行任务,终端总是会立即退出并出现错误/bin/bash: - : invalid option。我在互联网上的其他地方找不到任何关于这种情况的例子。

我已经尝试将我的行结尾设置为\ n,以及来自here的各种内容,但似乎没有任何效果。

我的任务是

{
    "version": "2.0.0",
    "type": "shell",
    "tasks": [
        {
            "label": "build",
            "command": "make",
        }
    ]
}

我究竟做错了什么?

visual-studio-code windows-subsystem-for-linux
1个回答
0
投票

我可以使用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文件以通过“options”字段完全控制构建终端命令的方式,则解决该问题,如下所示:

{
    // 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输出和查找源代码中的错误变得很方便。

因此,在您的情况下,您需要:

  1. 将默认vscode终端设置为命令提示符
  2. 添加到您的tasks.json文件选项: "options": { "shell": { "executable": "C:\\WINDOWS\\System3\\bash.exe", "args":["-c"] }

结合原始的tasks.json文件,上面的结果应该会导致在WSL工作文件夹中发出格式正确的“make”命令。

© www.soinside.com 2019 - 2024. All rights reserved.