12

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?

1
  • 2
    Getting the command input type to work has also stumped me. The pickString example from code.visualstudio.com/docs/editor/… worked like a charm, but there is no example for command and every logical attempt has so far failed...does this even work? Working with 1.39.2 Visual Studio Code on Ubuntu 18.04. Commented Nov 8, 2019 at 14:14

3 Answers 3

6

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)

Sign up to request clarification or add additional context in comments.

Comments

1

In your launch.json, try replacing

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

with

"args": "bar"

2 Comments

I think that the value for args should be an array, cause you could have multiple arguments. In this case though, if it's just one argument, the omitting the [] should still work
Works for me with a string, doesn't work for me with a list. There are other commands that work with a list, but not this one. You can request a change at github.com/microsoft/vscode/issues - I agree that consistency would be nice.
1

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

1 Comment

This worked great. Though note the insane fileName capitalisation. This tripped me up for ages, and it doesn't have good error checking so the only error you get is ENOENT.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.