6

I know this question might sound similar to this one: How do I add environment variables to launch.json in VSCode

But what I really want is to use the variables from my .env file inside the actual launch.json file, instead of using them in the program.

So my setup is something like this:

project-root/
  |-- .env
  |-- .vscode/
        |-- launch.json
  |-- src/
        |-- my-plugin/
        |-- my-theme/
  |-- wordpress/
  |-- data/
  |-- docker-compose.yml

In my .env file I have this:

PLUGIN_SLUG=my-plugin
THEME_SLUG=my-theme

Now, in my launch.json file, I would really like to be able to use the ${THEME_SLUG} and ${PLUGIN_SLUG} variables like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9000,
      "pathMappings": {
        "/var/www/html/wp-content/plugins/${PLUGIN_SLUG}": "${workspaceRoot}/src/${PLUGIN_SLUG}",
        "/var/www/html/wp-content/themes/${THEME_SLUG}": "${workspaceRoot}/src/${THEME_SLUG}",
        "/var/www/html": "${workspaceRoot}/wordpress",
      },
    }
  ],
}

Any idea how to achieve this?

::EDIT::

After digging some further, I realized that when I set the variables globally inside /etc/profile.d/temp.sh like this:

export PLUGIN_SLUG=codeable-plugin
export THEME_SLUG=codeable-theme

After logging out of my system and back in, I'm able to use these variables anywhere, including in my launch.json file like this:

   "/var/www/html/wp-content/plugins/${env:PLUGIN_SLUG}": "${workspaceRoot}/src/${env:PLUGIN_SLUG}",
   "/var/www/html/wp-content/themes/${env:THEME_SLUG}": "${workspaceRoot}/src/${env:THEME_SLUG}",

While this is a step closer to what I want, it's not really workable, to update these variables manually in my global OS config each time I switch projects, and then log out and in again.

6
  • What extension are you using? php is not a valid type for launch configs. Ultimately, I think you will be at the mercy of extension support on this one sadly. I think your question is specific to php and using an .env file not just 'using custom env variables', because, for example, using envFile is already supported with node launch configs
    – soulshined
    Commented Aug 2, 2020 at 1:29
  • @soulshined PHP Debug by Felix Becker. Do you have any reference to how it could be done for a simple node launch config? (without PHP) Commented Aug 6, 2020 at 5:51
  • Sure. You just use the built in node configurations and call the envFile property, enter the file name and to reference the variables you do it like you showed in your example:${env:}
    – soulshined
    Commented Aug 6, 2020 at 12:22
  • I'm sure I tried this with a default node project and I'm pretty sure VSC does not load the .env file automatically. I believe ${env:} is only available for global system variables. I could be wrong though. Did you find any documentation about this? Commented Aug 6, 2020 at 13:37
  • It does not load an .env file automatically, correct. That’s what the envFile property is for. github.com/microsoft/vscode-js-debug/blob/master/OPTIONS.md
    – soulshined
    Commented Aug 6, 2020 at 13:40

0