microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

Setting python.terminal.activateEnvironment: true results in loading env variables from the .env file #23865

Open lks-dev opened 4 months ago

lks-dev commented 4 months ago

The expected behavior, as stated by the online docs, is that the terminal is not influenced by the contents of the python.envFile:

An environment variable definitions file is a text file containing key-value pairs in the form of environment_variable=value, with # used for comments. Multiline values aren't supported, but references to previously defined environment variables are allowed. Environment variable definitions files can be used for scenarios such as debugging and tool execution (including linters, formatters, IntelliSense, and testing tools), but aren't applied to the terminal.

The documentation of the python.terminal.activateEnvironment setting, also does not reference the .env file as part of the activation:

Indicates whether to automatically activate the environment you select using the Python: Select Interpreter command when a new terminal is created. For example, when this setting is true, and you select a virtual environment, the extension automatically runs the environment's activate command when creating a new terminal (source env/bin/activate on macOS/Linux; env\scripts\activate on Windows).

However, when using an .env file in conjunction with the python.terminal.activateEnvironment setting, the contents of the .env file are applied to the terminal, which is, to my knowledge, not explicitly documented, but probably should be?


The behavior was replicated by me on Linux (bash) and windows (cmd) with pip .venvs and vscode-python v2024.10.0. Taking a look at the environment contributions shows environment variables set in the .env file, see second to last line VAR.

# Terminal Environment Changes

## Extension: vscode.git

[...]

## Extension: ms-python.python

Activated environment for `./.venv/bin/python`

- `VIRTUAL_ENV=/path/to/.venv`
- `VAR=I am defined in the issue.env file which is set as python.envFile in the vscode workspace`
- `PATH=~/.vscode/extensions/ms-python.python-2024.10.0-linux-x64/python_files/deactivate/bash:/path/to/.venv/bin:${env:PATH}`
luabud commented 4 months ago

hey @lks-dev, thanks for bringing this up! We probably need to revisit this and document the right behaviour if applicable. We'll look into this when we can!