How to Evaluate VS Code Environment Variables in Extension

Viewed 411

I asked a similar question to this a couple weeks ago, but I think because it was so specific and lengthy, I did not get an answer. See original post. I'll try and ask a more direct question this time--

I'm currently writing a VS Code extension. Is there a function built into the VS Code API that when given a string as input, allows for evaluation of environment variables, like those used in the tasks.json file? Full list of variables can be found here: https://code.visualstudio.com/docs/editor/variables-reference.

Such a function would take in a string containing environment variables and information about the workspace, then evaluate the environment variables in it. Example: "${workspaceFolder}_${fileBasenameNoExt}.txt""myWokspaceFolderName_myFileName.txt

I know VS Code has the functionality built in (as it is used in tasks) but I don't know how they do it and/or if I am able to use that functionality without having to rebuild it for my extension. What's the best way to implement this functionality in my extension?

1 Answers

One easy way to check a variable's runtime value is to create a VS Code task to output the variable value to the console. For example, to see the resolved value for ${workspaceFolder}, you can create and run (Terminal > Run Task) the following simple 'echo' task in tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo",
      "type": "shell",
      "command": "echo ${workspaceFolder}"
    }
  ]
}
Related