How to pass input variable from launch.json to tasks.json in vscode

Viewed 1827

I can use input variables from launch.json in launch.json.

"configurations": [
  {
    ...
    "args": [${input:file_no}]
    "preLanuchTask": "runPreTasks"
    ...
  }
],
"inputs": [
  {
    "id": "file_no",
    "type": "promptString"
  }
]

Now, I want to get access to the same variable without entering input a second time in tasks.json.

{
  "version": "2.0.0",
  "tasks":[
    {
      "label": "runPreTasks",
      "type": "shell",
      "command": sh,
      "args": [
        "/path2script/scriptName.sh",
        "${input:file_no}"    // This does not work, without defining input again
      ]
    }
  ]
}

Is there a way to pass input variables from launch.json to tasks.json in vscode?

2 Answers

You can use the extension Command Variable v1.21.0

It has a command extension.commandvariable.promptStringRemember that behaves the same as an ${input:name} promptString variable. By adding a key property the result is saved under this key and you can retreive it with the extension.commandvariable.rememberPick command.

The extension.commandvariable.rememberPick command can be used in a different task/launch than the extension.commandvariable.promptStringRemember command

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Task 1",
      "type": "shell",
      "command": "dostuff1",
      "args": ["-p", "${input:promptPath}"]
    },
    {
      "label": "Task 2",
      "type": "shell",
      "command": "dostuff2",
      "args": ["-p", "${input:rememberPath}"]
    },
    {
      "label": "Do Task 1 and 2",
      "dependsOrder": "sequence",
      "dependsOn": ["Task 1", "Task 2"],
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "promptPath",
      "type": "command",
      "command": "extension.commandvariable.promptStringRemember",
      "args": {
        "key": "path",
        "description": "Enter a path"
      }
    },
    {
      "id": "rememberPath",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "path" }
    }
  ]
}

Following @rioV8 answer, I edited my json files as shown below:

launch.json:

"configurations": [
  {
    ...
    "args": [${input:file_no}]
    "preLanuchTask": "runPreTasks"
    ...
  }
],
"inputs": [
  {
    "id": "file_no",
    "type": "command",
    "command": "extension.commandvariable.promptStringRemember",
    "args": {
      "key": "lastnumber",
      "description": "Enter the number"
    }
  }
]

tasks.json:

{
  "version": "2.0.0",
  "tasks":[
    {
      "label": "runPreTasks",
      "type": "shell",
      "command": sh,
      "args": [
        "/path2script/scriptName.sh",
        "${input:file_no}"    
      ]
    }
  ]
  "inputs": [
    {
      "id": "file_no",
      "type": "command",
      "command": "extension.commandvariable.rememberPick",
      "args": { "key": "lastnumber" }
    }
  ]
}
Related