If working with different tool chains, SDKs, and vendors, then one must use different environments.
With VS Code, this can end up in counter-intuitive situation. When I start a new Visual Studio Code instance, it will open a new window. But if there is already an instance running, it actually will re-using that environment. This can cause lots of subtle problems, including failed builds.

So how to start a new Visual Studio Code Window, with a new instance?
Outline
The problem is that if I start a new VS Code, it re-uses the environment of an existing instance.
This is explained best with an example. I’m using Espressif for this. But the issue is not Espressif related, it is a general VS Code behavior.
Let’s start with a system (Windows in my case) where VS Code is not running yet.
For Espressif and ESP32, I create a new environment using a script/batch file. That script file checks the necessary tools and sets a local environment variables in a console.
Inside that console, I then start VS Code to use that environment:
code .

That environment has set some variables:

That started VS Code instance is using the same environment and variables:

So far so good.
Launching new VS Code
If I new start a new VS Code, and there is a running instance, then it will re-use that environment.

That is logical and OK if:
- Start VS Code from the same console/environment
- Using the menu File > New Window in VS Code
But it re-uses that existing environment as well if:
- Start VS Code with ‘code’ from a new console/shell/environment
- Start VS Code with the Windows shortcut
It seems that VS Code is always checking if an existing instance is running, and re-uses that environment. This is usually OK and can make sense, but if using local environments with shell scrips this will cause problems.
Solution
The solution I have found is to start VS Code with a special command line parameter. One can get a list with
code --help
This reveals an interesting option:
-n --new-window Force to open a new window.
The description is maybe not that clear, but this opens a new VS Code with new environment. So it does *not* re-use an existing window environment:

With this, I have a VS Code window which does not inherit the environment of an already running instance.
Summary
If there is already a VS Code window or instance running, a new VS Code window will use the existing window. In this case, the new window will reuse the current environment.
The solution to start VS code with a new or different environment is to use the -n option. This can be added on the command line or to the shortcut starting VS code.
Happy coding 🙂
Links
- Visual Studio Code:https://code.visualstudio.com/
- https://github.com/microsoft/vscode/issues/48
- Articles around VS Code: https://mcuoneclipse.com/2021/05/01/visual-studio-code-for-c-c-with-arm-cortex-m-part-1/
Simple, but very useful!
LikeLike