Run go (golang) templ from VSCode task

3 weeks ago 22
ARTICLE AD BOX

I want to add Go Templ command as a VSCode task, so I can run it with a hotkey

I added .vscode/tasks.json to my project's folder that looks like this:

{ "version": "2.0.0", "tasks": [ { "label": "Templ Build", "type": "shell", "command": "go", "args": [ "tool", "templ", "generate" ], "problemMatcher": [] } ] }

When I run try to run the task, on hover it shows the command it will run, and it contains an extra "build" that I didn't add:

Hover in VSCode shows extra "build"

When ran, the terminal in VSCode shows this:

* Executing task: C:\Program Files\Go\bin\go.exe build tool templ generate package templ is not in std (C:\Program Files\Go\src\templ) package generate is not in std (C:\Program Files\Go\src\generate) * The terminal process "C:\Program Files\Go\bin\go.exe 'build', 'tool', 'templ', 'generate'" terminated with exit code: 1.

It makes an attempt to build "templ" and "generate", instead of running the "templ" tool.
When I type the command "go tool templ generate" in the terminal, it compiles the templates correctly.

Q: What is the correct way to configure this task?

Read Entire Article