【问题标题】:VS Code tasks.json -- Tasks work individually, but not combinedVS Code tasks.json - 任务单独工作,但不组合
【发布时间】:2016-05-22 10:53:57
【问题描述】:

这让我发疯(发疯!)。构建/运行文件正确且 fmt 命令正确。但是,如果我尝试合并到一个任务文件中,它就会停止工作。

这两个单独工作很好,并且按照我想要的方式行事:

tasks.json

{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"taskName": "build",
"args": [
    "build",
    "-o",
    "${workspaceRoot}.exe",
    "&&",
    "${workspaceRoot}.exe"
],
"isBuildCommand": true
}

tasks.json

{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"taskName": "fmt",
"args": [
    "fmt",
    "${file}"
],
"isBuildCommand": true
}

但是合并成一个文件,就不行了:

tasks.json

{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"tasks": [
    {
        "taskName": "build",
        "args": [
            "build",
            "-o",
            "${workspaceRoot}.exe",
            "&&",
            "${workspaceRoot}.exe"
        ],
        "isBuildCommand": true
    },
    {
        "taskName": "fmt",
        "args": [
            "fmt",
            "${file}"
        ]
    }
]
}

构建时出现错误:

can't load package: package build: cannot find package "build" in any of:
    D:\dev\Go\src\build (from $GOROOT)
    D:\dev\Gopher\src\build (from $GOPATH)
can't load package: package -o: cannot find package "-o" in any of:
    D:\dev\Go\src\-o (from $GOROOT)
    D:\dev\Gopher\src\-o (from $GOPATH)
can't load package: package d:/dev/Gopher/src/myproject.exe: cannot find package "d:/dev/Gopher/src/myproject.exe" in any of:
    D:\dev\Go\src\d:\dev\Gopher\src\myproject.exe (from $GOROOT)
    D:\dev\Gopher\src\d:\dev\Gopher\src\myproject.exe (from $GOPATH)

我似乎无法理解为什么它以一种方式起作用,而另一种则不起作用。此处概述了第二种方法(用于组合任务):Define multiple tasks in VSCode


Answer: 问题在于添加“build”或“fmt”作为 args,而它已经被列为任务名。我不知道 taskname 是这样工作的。最终的工作产品,允许用户开发而不用担心愚蠢的 Windows 防火墙:

tasks.json(感谢@not-a-golfer 的最终和工作)

{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"echoCommand": true ,
"tasks": [
    {
        "taskName": "build",
        "args": [
            "-o",
            "${workspaceRoot}.exe",
            "&&",
            "${workspaceRoot}.exe"
        ],
        "isBuildCommand": true
    },
    {
        "taskName": "fmt",
        "args": [
            "${file}"
        ]
    }
]
}

【问题讨论】:

    标签: json go visual-studio-code vscode-tasks


    【解决方案1】:

    以下似乎有效,但您似乎无法使用&& 链接运行:

    {
    "version": "0.1.0",
    "isShellCommand": true,
    "showOutput": "always",
    "command": "go",
    "echoCommand": true ,
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "-x",
                "-o",
                "${workspaceRoot}.exe"
            ],
            "isBuildCommand": true
        },
        {
            "taskName": "fmt",
            "args": [
                "${file}"
            ]
        }
    ]
    }
    

    【讨论】:

    • 添加 "&&","${workspaceRoot}.exe" 对我有用。我猜这个问题与我再次在 args 中列出任务名有关。谢谢!
    • @victoroux 这让我了解了我不知道的 vscode 中的这个功能,所以谢谢 :)
    【解决方案2】:

    你应该添加属性suppressTaskName

    删除多余的build 参数的 OPs 解决方案显然有效,但是,VSCode's documentation 涵盖了这个例子:

    我们将 suppressTaskName 设置为 true,因为默认情况下,任务名称也会传递给会导致“echo hello Hello World”的命令。

    {
        "version": "0.1.0",
        "command": "echo",
        "isShellCommand": true,
        "args": [],
        "showOutput": "always",
        "echoCommand": true,
        "suppressTaskName": true,
        "tasks": [
            { 
                "taskName": "hello",
                "args": ["Hello World"]
            },
            { 
                "taskName": "bye",
                "args": ["Good Bye"]
            }
        ]
    }
    

    【讨论】:

      【解决方案3】:

      我最喜欢的构建任务是:

      {
      "version": "0.1.0",
      "isShellCommand": true,
      "showOutput": "always",
      "command": "go",
      "echoCommand": true ,
      "options": {
          "cwd": "${fileDirname}"
      },
      "tasks": [
          {
              "taskName": "build",
              "args": [
                  "build",
                  "-x"
              ],
              "isBuildCommand": true,
              "suppressTaskName": true
          }
      ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 2017-09-25
        • 2016-05-24
        • 1970-01-01
        相关资源
        最近更新 更多