1

Is there a way to set a variable in a launch configuration, that comes from a file, without an extension?

I've seen the following:

  • default vscode variables, e.g. ${file}
  • vscode input prompt or picker
  • vscode extension command

But neither seems to allow me to use a variable of my own, from some external data. In my case, i just want to load a port from a json configuration, so i don't need to copy it into the launch configuration: "url": "http://something:${coolPeoplePort}". The server uses the port from that same file.

I've found this overly workflow specific way. This could potentially be abused, by running some program, that puts the variable on stdout. Why this feature is so specific to this application, i don't understand. It might work, because it seems to be precisely and only for putting ports into chrome debug URIs. Unlike the intention, the node program wouldn't be a webserver, and it would feel like i am using "happy hippo special case #5643", which is typically neither clean, future-proof, nor robust.

There are extensions that provide a multitude of ways to deal with this, but i don't want to install one, as it is a rather minor use case (i just don't want to copy some data to two places).

Doofus
  • 952
  • 4
  • 19

1 Answers1

1

In extension Command Variable v1.10.0 I have added the possibility to extract a value from a JSON file.

Example config.json file

{
  "log": "foobar.log",
  "server1": {
    "port": 5011
  },
  "server2": {
    "port": 5023
  }
}

In your task.json you want to use the server1 port value:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo Server1Port",
      "type": "shell",
      "command": "echo",
      "args": [
        "${input:configServer1Port}"
      ],
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "configServer1Port",
      "type": "command",
      "command": "extension.commandvariable.file.content",
      "args": {
        "fileName": "${workspaceFolder}/config.json",
        "json": "content.server1.port",
        "default": "4321"
      }
    }
  ]
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Originally, i didn't want to add another extension for this, but it seems to be the only way. Thanks, i'll consider it. – Doofus Dec 30 '20 at 21:51
  • @Doofus Much functionality of VSC is from extensions, many are part of the default install – rioV8 Dec 30 '20 at 23:05
  • Yes, but third-party extensions always come with the typical problems: what, if they stop being maintained? What, if they are incompatible with another? Are they buggy? Are they secure/safe? It's often not that big of a problem, and i might just install it, hence why i accepted your answer. Still, i always try to use existing features first. When those possibilities are exhausted, i think about how dearly i need the feature, and then potentially get an extension, if possible. – Doofus Dec 30 '20 at 23:11