Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch specific task from input variable in VS Code?

I'm trying to create a launch configuration where an environment variable is dynamically determined by a shell script. Even though a command variable can start a task via workbench.action.tasks.runTask, it doesn't seem to be possible to specify which task to run. Input variables seem to be a little more flexible in that regard, but I can't seem to get it to work. Here is what I got:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {
                "XXX": "${input:foo}"
            },
            "args": []
        }
    ],
    "inputs": [
        {
            "type": "command",
            "id": "foo",
            "command": "workbench.action.tasks.runTask",
            "args": {
                "args": "bar",
            }
        }
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bar",
            "type": "shell",
            "command": "find /dev -name 'myspecialdevice*' -maxdepth 1"
        }
    ]
}

The issue is that the user is still queried for which task to run. I'm most insecure about the inputs.args section of the launch.json. I don't really know what the key value should be. Perhaps the implementation helps to figure this out?

like image 607
sven Avatar asked Feb 18 '19 10:02

sven


3 Answers

This answer not really relates to make use of a vscode task, but your introducting sentence offers the motivation/what is intended to be solved.

I was faced to the same question and was wondering about vscode's input type:command. It offers a way to embed a (custom) vscode command -- which looks like a powerfull mechanism to embed a (custom) extension here. But I didn't found a builtin command that simply executes a shell script and returns it stdout. Thus an extension to capture the output of a shell command is imho required todo the trick.

E.g. https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input provides a command shellCommand.execute doing exactly this.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {
                "XXX": "${input:foo}"
            },
            "args": []
        }
    ],
    "inputs": [
        {
            "id": "foo",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "find /dev -name 'myspecialdevice*' -maxdepth 1",
                "cwd": "${workspaceFolder}",
                /* To prevent user from selecting output (if there is just
                   one line printed by command), un-comment next line */
                //"useSingleResult": true
            }
        }
    ]
}

(Inspired by https://stackoverflow.com/a/58930746/1903441)

like image 87
550 Avatar answered Nov 07 '22 10:11

550


In your launch.json, try replacing

"args": {
    "args": "bar",
}

with

"args": "bar"
like image 1
Nicolas Kadis Avatar answered Nov 07 '22 08:11

Nicolas Kadis


It seems that in vscode, you cannot pass a return or even an environment variable from task.json to launch.json. But you can use a file as a intermediate.

For example, in task.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bar",
            "type": "shell",
            "command": "find /dev -name 'myspecialdevice*' -maxdepth 1 > ${workspaceFolder}/.vscode/temp"
        }
    ]
}

In launch.json you set bar as preLaunchTask, and later access the file using inputs, like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {
                "XXX": "${input:foo}"
            },
            "args": [],
            "preLaunchTask": "bar",
        }
    ],
    "inputs": [
        {
            "id": "foo",
            "type": "command",
            "command": "extension.commandvariable.file.content",
            "args": {
                "fileName": "${workspaceFolder}/.vscode/temp",
            }
        }
    ]
}

To get the comment working, just install this extension: https://marketplace.visualstudio.com/items?itemName=rioj7.command-variable

like image 1
William Surya Permana Avatar answered Nov 07 '22 09:11

William Surya Permana