VS Code tasks.json — 任务单独工作,但不能组合使用

huangapple go评论84阅读模式
英文:

VS Code tasks.json -- Tasks work individually, but not combined

问题

这让我很烦恼(发疯!)。构建/运行文件是正确的,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}"
]
}
]
}

构建时出现的错误:

无法加载包:包构建:在以下任何位置都找不到包“build”:
D:\dev\Go\src\build(来自$GOROOT)
D:\dev\Gopher\src\build(来自$GOPATH)
无法加载包:包-o:在以下任何位置都找不到包“-o”:
D:\dev\Go\src-o(来自$GOROOT)
D:\dev\Gopher\src-o(来自$GOPATH)
无法加载包:包d:/dev/Gopher/src/myproject.exe:在以下任何位置都找不到包“d:/dev/Gopher/src/myproject.exe”:
D:\dev\Go\src\d:\dev\Gopher\src\myproject.exe(来自$GOROOT)
D:\dev\Gopher\src\d:\dev\Gopher\src\myproject.exe(来自$GOPATH)

我似乎无法理解为什么一种方式可以工作,而另一种方式不行。第二种方法(合并任务)在这里有详细说明:https://stackoverflow.com/questions/30046411/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}"
]
}
]
}

英文:

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: https://stackoverflow.com/questions/30046411/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}"
        ]
    }
]
}

答案1

得分: 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}"
        ]
    }
]
}
英文:

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

得分: 0

你应该添加属性 suppressTaskName

解决方案中去除多余的 build 参数显然是有效的,然而,VSCode 的文档中已经涵盖了这个例子:

> 我们将 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"]
        }
    ]
}
英文:

You should add the attribute suppressTaskName.

OPs solution to remove superfluous build parameter obviously works, however, VSCode's documentation covers this very example:

> We set suppressTaskName to true as by default the task name is also passed to the command which would result in "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

得分: 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
}
]
}

英文:

My favorite build task is:

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

huangapple
  • 本文由 发表于 2016年2月11日 23:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/35343143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定