Start New VS Code Instances with Unique Environments

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.

VS Code re-using instance environment

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 .
Starting VS Code in Environment

That environment has set some variables:

Environment Variables

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

VS Code with Espressif Environment

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.

VS Code new Instance

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:

VS Code started with -n option

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

1 thought on “Start New VS Code Instances with Unique Environments

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.