【问题标题】:VScode makes WSL exit with error on any build taskVScode 在任何构建任务上都使 WSL 退出并出现错误
【发布时间】:2019-02-16 17:57:39
【问题描述】:

我在 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】:

    我可以使用 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 输出和定位源代码中的错误变得很方便。

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

    1. 将默认的 vscode 终端设置为命令提示符

    2. 将选项添加到您的 tasks.json 文件中:

          "options": {
              "shell": {
                  "executable": "C:\\WINDOWS\\System3\\bash.exe",
                  "args":["-c"]
              }
      

    结合您的原始 tasks.json 文件,以上内容应导致在您的 WSL 工作文件夹中发出格式良好的“make”命令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 2015-08-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多