Environment Variable Resolution in VSCode

· 177 words · 1 minute read

Problem

Linux environment variables declared in ~/.bashrc aren't pulled into VSCode when it starts.

Specifically, I declared a variable export foo\'bar'= in ~/.bashrc. I launch VSCode using its desktop file (via the rofi launcher). Then when I execute a dotnet build process I find that the foo variable is unset in any of the shells that VSCode launches.

Solution

This issue arises because of how VSCode initializes in different contexts. If I launched VSCode from an interactive shell, I would have the variable I exported in ~/.bashrc. VSCode does make some effort to bring in environment variables from its host system [0]. For some reason this doesn't work on my computer.

My solution was to modify VSCode's default .desktop file to initialize from an interactive shell, like so:

  [Desktop Entry]
  ...
  Exec=/bin/bash -ic "/usr/bin/code --unity-launch %F"
  ...

The -i command line flag tells bash to launch an interactive shell. -c tells bash to evaluate the following string as a command.

This is a kludge, but it works rather seamlessly.