1

I will split the question into 2 posts because of the complexity. I'll create a second post after I have an answer for the first part.

Background: we are using a python virtual environment with

  • conan to manage dependencies (compiler)
  • invoke to simplify use, and vscode as development environment.
  1. We activate the virtual environment in the shell
  2. we call invoke, which adds the conan packages to the path
  3. then starts vscode

I want to be able to press F7 in vscode and compile my project using cmake tools, but i can't get the extension to find my compiler since it is a conan package and not in the windows path.

First part of the question: how to pass my shell path to vscode?

Currently we have:

def _run_in_venv(ctx, folder, command, **kwargs):
    with ctx.cd(folder):
        with ctx.prefix("activate_build.bat"):
            with ctx.prefix("activate.bat"):
                ctx.run(command, **kwargs)

and

    env = {"BUILD_DIR": f"{folder}", "PATH": f"{os.getenv('PATH')}"}
    _run_in_venv(ctx, folder, "code" + workspace_arg, env=env, echo=True)

I see that the compiler is in os.getenv('PATH'), but when i check the terminal in vscode (echo $env:path), it is completely different. BUILD_DIR is passed ok.

If i put the os.getenv('PATH') into a different variable TEMP, it is passed correctly (echo $env:temp).

I hope, that if i have the compiler in $env:path, cmake tools will recognize it. If it won't i'll create a second question regarding the cmake tools configuration.

1 Answer 1

1

1. Do not pass PATH manually

You are capturing os.getenv('PATH') inside Python before the .bat files execute in the shell. By passing this stale PATH in the env dictionary, you are explicitly overwriting the environment updates made by activate_build.bat.

Modify your Python script to let the shell command chain handle the propagation:

# ONLY pass the new variables. Remove "PATH".
env = {"BUILD_DIR": f"{folder}"} 
# invoke's ctx.run inherits the environment, and the prefix .bat files 
# will modify the PATH immediately before 'code' launches.
_run_in_venv(ctx, folder, "code " + workspace_arg, env=env, echo=True)

2. The "Singleton" Issue

VS Code ignores environment variables from a new launch command if a VS Code instance is already running. It will open a new window attached to the existing main process (which has the old PATH), while creating BUILD_DIR as a process-specific override.

Completely close VS Code before running invoke to force a "Cold Start."

3. Verification

The Integrated Terminal often resets the PATH via your PowerShell profile, hiding the fact that VS Code actually did receive the correct environment.

  • Open VS Code Dev Tools

  • In the Console, type: process.env['PATH']

  • If your compiler is listed here but not in the terminal, VS Code has the path (CMake Tools will work), and your Terminal settings are simply resetting it for display.

New contributor
NullPointerDepressiveDisorder is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.