douweihui0178 2016-02-11 15:18
浏览 338
已采纳

VS Code task.json —任务可以单独工作,但不能合并

This is driving me nuts (go nuts!). Build / run file is proper and fmt command is proper. But if I try to combine into one tasks file, it stops working.

These two work fine on their own and behave the way I want:

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
}

But combined into one file, it will not work:

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

Error given on build:

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)

I can't seem to understand why it works one way, but not the other. The second method (for combined tasks) is outlined here: Define multiple tasks in VSCode


Answer: The problem lies with adding "build" or "fmt" as an args when it's already listed as a taskname. I did not know that's how taskname worked. Final working product which allows users to develop without worrying about stupid windows firewalls:

tasks.json (final & working thanks to @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条回答 默认 最新

  • dongzhang5006 2016-02-11 17:25
    关注

    The following seems to be working, but it appears that you can't chain the running with &&:

    {
    "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}"
            ]
        }
    ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?