Search code examples
visual-studio-codevscode-tasks

How can I run a shell command in Visual Studio Code (attempted with launch.json)


I'm using a gRPC proto file in my project, so whenever I modify the file, I need to rebuild the .proto file. I'd like to ideally have a launch configuration, or some kind of button that I can hit from the VSCode interface to rebuild the proto files without launching the entire program, is this possible? The attempts I've made so far have not worked.

I've looked at 43836861 but this specifies that the task is launched before every launch of the program, which is not what I need.

I've tried setting up the following:

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "protoc",
            "type": "shell",
            "command": "protoc",
            "args": [
                "--go_out=${workspaceFolder}/backchannel",
                "--go_opt=paths=source_relative",
                "--go-grpc_out=${workspaceFolder}/backchannel",
                "--go-grpc_opt=paths=source_relative",
                "--proto_path=${workspaceFolder}/backchannel",
                "${workspaceFolder}/backchannel/backchannel.proto"
            ]
        }
    ]
}

And I've added this to my launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Rebuild Protoc",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "",
            "preLaunchTask": "protoc"
        }
    ]
}

This didn't work, and by asking around, I've tried switching "request" to "custom" as well as "type" to "shell" so I ended up with something like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Rebuild Protoc",
      "type": "shell",
      "command": "echo 'Rebuild Protoc task completed'",
      "preLaunchTask": "protoc"
    }
  ]
}

But I'll admit, I don't really know enough about launch.json to debug why it says that "shell" is not a supported type.

Can somebody explain this to me, and show me how to fix it?


Solution

  • You can run tasks without a launch config. Launch configs are for debugging programs. Tasks are for doing things that aren't debugging. One way to run a task is to use the Tasks: Run Task command. You can also bind that command to a keyboart shortcut if you so wish.

    If you set the group field of the task definition, you can use any existing default keybinding for running the task for that task group. (you can use the keyboard shortcut editor UI and search "tasks" to see what those default keybindings are).