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

问题描述 投票:3回答:3

这让我疯了(坚果!)。构建/运行文件是正确的,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}"
        ]
    }
]
}
json go visual-studio-code vscode-tasks
3个回答
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}"
        ]
    }
]
}

0
投票

您应该添加属性suppressTaskName

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

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
    }
]
}
© www.soinside.com 2019 - 2024. All rights reserved.