VS Code tasks.json - 任务单独工作,但未合并

时间:2016-02-11 15:18:24

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

这让我疯了(疯了!)。构建/运行文件是正确的,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

答案:问题在于添加" 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}"
        ]
    }
]
}

3 个答案:

答案 0 :(得分:3)

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

{
"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}"
        ]
    }
]
}

答案 1 :(得分:0)

您应该添加属性suppressTaskName

用于删除多余build参数的OP解决方案显然有效,但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"]
        }
    ]
}

答案 2 :(得分:0)

我最喜欢的构建任务是:

{
"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
    }
]
}
相关问题