Skip to content
| Marketplace
Sign in
Visual Studio Code>Debuggers>CMake Launch EnvironmentNew to Visual Studio Code? Get it now.
CMake Launch Environment

CMake Launch Environment

sebastian bleeker

|
95 installs
| (1) | Free
Run your environment setup script and inject its environment into every CMake launch.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

CMake Launch Environment

Pick your environment setup script once, and every CMake launch gets its environment.

You probably already have a script that prepares your environment: the one you source in a terminal before running anything by hand. This extension runs that script, captures the environment it produces, and passes it to everything VS Code launches. Debug and Run work the same as your terminal does.

You don't edit launch.json or copy variables into settings.


Why

Many programs only run with the right environment: libraries on the search path, plugin or resource directories set, SDK variables exported. In a terminal you source a script first and everything works. From the editor's Debug button the program starts without that environment. It fails, often with an error that says nothing about the actual cause.

Usually you'd copy the variables into launch.json by hand, once per configuration, and keep them in sync with the script from then on. This extension runs the script itself, so there is nothing to copy or keep in sync.


Getting started

  1. Open a CMake project. A status bar item shows the active build type, e.g. Debug: no script selected.
  2. Click it. A picker lists the environment scripts in your workspace.
  3. Pick one. It runs immediately, and the status bar shows a check mark and the script's name.
  4. Press CMake Tools' Debug or Run Without Debugging. Your program starts with everything the script exported.

That's all the setup there is. Each build type has its own script, so Debug and Release can use different ones.

  • Outside the workspace? Choose Browse… in the picker. A file inside the workspace is stored as ${workspaceFolder}/…, so the assignment still works if the project moves.
  • Want to stop? Click the status bar item and choose Clear assignment.

What it covers

Every way VS Code can start your program:

Launch path Covered
CMake Tools Debug button ✅
CMake Tools Run Without Debugging ✅
Hand-written launch.json (cppdbg / cppvsdbg) ✅
Plain F5 ✅
Any terminal you open ✅

If a launch configuration already has an environment entry, the script's value wins for each variable the script defines. Your other entries are left alone.

Supported scripts

Platform Scripts
macOS, Linux .sh, .bash, .zsh, .ksh
Windows .ps1, .cmd, .bat

The picker only offers scripts your machine can run, so you won't see a .bat on macOS or a .sh on Windows.

Requirements

  • VS Code 1.82 or later
  • CMake Tools, which reports the active build type
  • The C/C++ extension, for debugging (cppdbg / cppvsdbg)

Day to day

Editing a script re-runs it

The extension watches the assigned script and every script it pulls in. When you save one of them, the environment is captured again within about a second, and the status bar briefly names the file that changed. Files outside the workspace are watched too, such as an SDK's own setup script.

A file counts as pulled in when the script:

  • sources it with source or . (sh, bash and similar)
  • runs it with call (.cmd, .bat)
  • dot-sources it, runs it with &, or loads it with Import-Module (PowerShell)
  • mentions any other path ending in a script extension

This applies recursively, so a file sourced by a sourced file is watched as well.

The extension finds these files by reading the script, not by running it. It understands paths built from:

  • the usual "folder this script is in" idioms: $(dirname "${BASH_SOURCE[0]}"), ${BASH_SOURCE%/*}, $PSScriptRoot, %~dp0
  • variables the script set earlier
  • environment variables

A path computed any other way isn't watched.

The status bar shows what is in effect

The status bar shows what is actually being injected, not just what you picked:

It shows It means
Debug: no script selected No script is assigned. Click to pick one.
Debug: env.sh with a check mark The script ran and its environment is injected.
Debug: env.sh with a spinning icon The script is running. Nothing is injected until it finishes.
Debug: env.sh — capture failed The script failed. Nothing is injected. Hover to see why.
CMake Env: no build type CMake Tools hasn't reported a build type yet. Configure the project first.
CMake Env: CMake Tools unavailable CMake Tools isn't installed or didn't start.

A script is shown as active only if its capture succeeded. A broken script shows up in the status bar right away. Your program never launches with a stale or partial environment while everything looks fine.

Hover to see what the script did

Hover over the status bar item to see the environment in effect:

  • Set by this script: the variables the script added or changed, with their values. Usually only a few, and they're the ones you care about.
  • Unchanged from your environment: the names of the other captured variables.
  • Paths are clickable. A file opens in the editor; a directory opens in Finder or Explorer. PATH-style lists are split into their entries.
  • Paths that don't exist are flagged. A search path pointing at a missing directory is a common reason a program fails to start with an unrelated-looking error.

To see every variable, click Show all N variables in the hover or run Show Captured Environment. Either one opens a document you can scroll, search and copy from.


Commands

All of these are in the Command Palette under CMake Launch Environment.

Command What it does
Select Environment Script for Active Build Type Opens the picker. Picking the current script again re-runs it.
Clear Environment Script for Active Build Type Removes the assignment and stops injecting.
Show Captured Environment for Active Build Type Opens the full captured environment in an editor.

Settings

Setting Default What it does
cmakeLaunchEnv.watchScripts true Re-run the script when it, or a script it pulls in, changes on disk.

How the environment is captured

The script runs in its own interpreter (bash, pwsh/powershell, or cmd). The resulting environment is then read back in a structured format: NUL-delimited on macOS and Linux, JSON on Windows. It is never parsed line by line.

Line-by-line parsing breaks on things real scripts do all the time. An exported bash function, or a value that contains a newline, corrupts every variable that comes after it. CMake Tools' own environmentSetupScript has this problem (#2686). A silently truncated environment is worse than none, because the failure shows up somewhere else entirely.

Here, values containing = or several lines come through intact, as do shells that export functions. Whatever the script prints with echo doesn't affect the capture either.

What is remembered

Assignments are saved per workspace and restored when you reopen the project. They live in VS Code's workspace state, so they stay on your machine and aren't committed to the repository.

The captured environment is never saved, only which script you picked. When you reopen the project, the script runs again in the background. Replaying the previous session's variables could inject a stale environment if the script changed in between. Until the run finishes, nothing is injected.

If the script was deleted or moved in the meantime, its assignment is dropped.

Known limitations

  • Switching build type doesn't carry the script over. Each build type needs its own assignment.
  • Scripts run on assignment, on startup and when a watched file changes, not on every launch. If the script depends on something that isn't a watched file, such as a computed include path or another tool's output, re-pick it to run it again.
  • Terminals that were already open don't get the variables. VS Code applies environment changes when it creates a terminal, so open a new one.
  • On Windows, a .ps1 with non-ASCII characters needs a UTF-8 BOM. Without one, Windows PowerShell 5.1 reads the file in the system code page and mangles those characters, exactly as it would in your own terminal. Most editors save PowerShell scripts with a BOM already.

Feedback

Bugs and suggestions: github.com/sblkr/vscode-launch-environment/issues

To build it yourself, or to read why it works the way it does, see CONTRIBUTING.md and DECISIONS.md.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft