VS Code with environment vars

Im using the vscode extension for my platformio project, and using build_flags with environment variables. Each time I build, it starts a new terminal, so all the environment variables I set are wiped away. Without setting a global var (eg bash_profile or bash_rc) how do I set the env var using vscode?

Please see Environment variables — PlatformIO latest documentation

To set build flags via environment variables, you need to use PLATFORMIO_BUILD_FLAGS

My question is more specific to VSCode than how to set an environment variable. Using VSCode, on each build, it will start a fresh terminal session, so no variables are present - unless they are set globally. There’s gotta be a better solution?

Like my answer to environment variables used by PlatformIO.

Perhaps I misunderstood you.
Can you give me an example that shows what you want to achieve?

I was able to solve it using this pattern: # Convention for compile time configuration of PlatformIO projects | Arik Yavilevich's blog

Basically a pre build script that sets the environment variables