microsoft / vscode-python

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

Environment variables persist in terminal after Python extension deactivation and ignore updates to `.env` file and comments within it #23280

Closed andylizf closed 3 months ago

andylizf commented 4 months ago

Type: Bug

Behaviour

When the Python extension for Visual Studio Code is activated by opening a Python file, it reads environment variables from the workspace's .env file and applies them to the Terminal Environment. These variables persist even after the extension is deactivated and do not reflect updates made to the .env file. Additionally, comments within the .env file are not properly parsed, causing incorrect environment variable values to persist. The VS Code documentation states:

An environment variable definitions file is a text file containing key-value pairs in the form of environment_variable=value, with # used for comments. 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.

Steps to reproduce:

  1. Create a .env file in the root of a workspace with the following content:
    MESSAGE="Hello World" # This is a comment
  2. Open a Python file (e.g., main.py) to activate the Python extension.
  3. Open a new terminal session and observe the MESSAGE environment variable, noting that the comment is incorrectly appended to the value.

    Output for "Show environment contributions" in the terminal session

    ``` # Terminal Environment Changes ## Extension: vscode.git Enables the following features: git auth provider - `GIT_ASKPASS=c:\Users\andyl\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\git\dist\askpass.sh` - `VSCODE_GIT_ASKPASS_NODE=C:\Users\andyl\AppData\Local\Programs\Microsoft VS Code\Code.exe` - `VSCODE_GIT_ASKPASS_EXTRA_ARGS=` - `VSCODE_GIT_ASKPASS_MAIN=c:\Users\andyl\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\git\dist\askpass-main.js` - `VSCODE_GIT_IPC_HANDLE=\\.\pipe\vscode-git-93778db71e-sock` ## Extension: ms-python.python Activated environment for `~\miniconda3\python.exe` - `CONDA_DEFAULT_ENV=base` - `CONDA_PREFIX=C:\Users\andyl\miniconda3` - `CONDA_PROMPT_MODIFIER=(base) ` - `CONDA_PYTHON_EXE=C:\Users\andyl\miniconda3\python.exe` - `CONDA_SHLVL=1` - `MESSAGE="Hello World" # Say hello to the world` - `PATH=C:\Users\andyl\miniconda3;C:\Users\andyl\miniconda3\Library\mingw-w64\bin;C:\Users\andyl\miniconda3\Library\usr\bin;C:\Users\andyl\miniconda3\Library\bin;C:\Users\andyl\miniconda3\Scripts;C:\Users\andyl\miniconda3\bin;C:\Users\andyl\miniconda3\condabin;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.4.2.0_x64__8wekyb3d8bbwe;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\vcpkg;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\xmake;C:\Program Files\Git\cmd;C:\Program Files (x86)\Incredibuild;C:\Program Files\dotnet;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\nodejs;C:\Program Files\Pandoc;C:\Program Files\GitHub CLI;C:\Users\andyl\miniconda3;C:\Users\andyl\miniconda3\Library\mingw-w64\bin;C:\Users\andyl\miniconda3\Library\usr\bin;C:\Users\andyl\miniconda3\Library\bin;C:\Users\andyl\miniconda3\Scripts;C:\Program Files\MySQL\MySQL Shell 8.2\bin;;C:\Users\andyl\.cargo\bin;C:\Users\andyl\AppData\Local\Microsoft\WindowsApps;C:\Users\andyl\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files (x86)\Nmap;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Alist.Alist_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Rclone.Rclone_Microsoft.Winget.Source_8wekyb3d8bbwe\rclone-v1.62.2-windows-amd64;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\aria2.aria2_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Programs\oh-my-posh\bin;C:\Program Files\Graphviz\bin;C:\Users\andyl\AppData\Local\Programs\MiKTeX\miktex\bin\x64;C:\Program Files (x86)\Prince\engine\bin;D:\aria2;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\GNU.Wget2_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-7.0-full_build\bin;C:\Users\andyl\AppData\Roaming\npm;${env:PATH}` - `POSH_PID=10292` - `PROMPT=(base) $P$G` - `SSL_CERT_FILE=C:\Users\andyl\miniconda3\Library\ssl\cacert.pem` - `_CONDA_OLD_CHCP=65001` - `CONDA_ROOT=C:\Users\andyl\miniconda3` - `__CONDA_OPENSLL_CERT_FILE_SET="1"` ```

  1. Close all Python files and exit VS Code to deactivate the Python extension.
  2. Restart VS Code and modify the .env file to:
    MESSAGE="Goodbye, World!" # Update the message
  3. Open a new terminal session and check the MESSAGE environment variable, which still shows the initial value with the comment.
  4. Attempt to report this issue via the VS Code issue reporter, noting that opening the reporter activates the Python extension, which may reactivate terminals and apply up-to-date environment variables.

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` 2024-04-23 19:01:48.859 [info] Experiment 'pythonRecommendTensorboardExt' is active 2024-04-23 19:01:48.859 [info] Experiment 'pythonREPLSmartSend' is active 2024-04-23 19:01:48.859 [info] Experiment 'pythonTerminalEnvVarActivation' is active 2024-04-23 19:01:48.859 [info] Experiment 'pythonTestAdapter' is active 2024-04-23 19:01:48.859 [info] Default formatter is set to null for workspace c:\Users\andyl\Documents\bug-python-ext 2024-04-23 19:01:48.859 [info] Test server listening. 2024-04-23 19:01:48.859 [error] Failed to initialize source map support in extension 2024-04-23 19:01:48.859 [info] Python interpreter path: ~\miniconda3\python.exe 2024-04-23 19:01:48.939 [info] > hatch env show --json 2024-04-23 19:01:48.939 [info] cwd: . 2024-04-23 19:01:48.965 [info] > conda info --json 2024-04-23 19:01:51.139 [info] > ~\miniconda3\python.exe -I ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\get_output_via_markers.py ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\interpreterInfo.py 2024-04-23 19:01:51.288 [info] > pyenv which python 2024-04-23 19:01:51.288 [info] cwd: . 2024-04-23 19:01:51.433 [info] > conda info --json 2024-04-23 19:01:51.433 [info] shell: powershellCore 2024-04-23 19:01:52.996 [info] Starting Pylance language server. 2024-04-23 19:01:56.675 [info] > conda run -n base --no-capture-output python ~\.vscode\extensions\ms-python.python-2024.4.1\python_files\get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.4.1/python_files/printEnvVariables.py 2024-04-23 19:01:56.675 [info] shell: powershellCore 2024-04-23 19:02:00.195 [info] > python ~/.vscode/extensions/ms-python.python-2024.4.1/python_files/printEnvVariables.py 2024-04-23 19:02:00.195 [info] shell: powershellCore 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_DEFAULT_ENV in collection to base {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_PREFIX in collection to C:\Users\andyl\miniconda3 {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_PROMPT_MODIFIER in collection to (base) {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_PYTHON_EXE in collection to C:\Users\andyl\miniconda3\python.exe {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_SHLVL in collection to 1 {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable MESSAGE in collection to "Hello, World!" # This is a comment {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Prepending environment variable PATH in collection to C:\Users\andyl\miniconda3;C:\Users\andyl\miniconda3\Library\mingw-w64\bin;C:\Users\andyl\miniconda3\Library\usr\bin;C:\Users\andyl\miniconda3\Library\bin;C:\Users\andyl\miniconda3\Scripts;C:\Users\andyl\miniconda3\bin;C:\Users\andyl\miniconda3\condabin;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.4.2.0_x64__8wekyb3d8bbwe;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\vcpkg;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\xmake;C:\Program Files\Git\cmd;C:\Program Files (x86)\Incredibuild;C:\Program Files\dotnet;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\nodejs;C:\Program Files\Pandoc;C:\Program Files\GitHub CLI;C:\Users\andyl\miniconda3;C:\Users\andyl\miniconda3\Library\mingw-w64\bin;C:\Users\andyl\miniconda3\Library\usr\bin;C:\Users\andyl\miniconda3\Library\bin;C:\Users\andyl\miniconda3\Scripts;C:\Program Files\MySQL\MySQL Shell 8.2\bin;;C:\Users\andyl\.cargo\bin;C:\Users\andyl\AppData\Local\Microsoft\WindowsApps;C:\Users\andyl\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files (x86)\Nmap;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Alist.Alist_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Rclone.Rclone_Microsoft.Winget.Source_8wekyb3d8bbwe\rclone-v1.62.2-windows-amd64;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\aria2.aria2_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Programs\oh-my-posh\bin;C:\Program Files\Graphviz\bin;C:\Users\andyl\AppData\Local\Programs\MiKTeX\miktex\bin\x64;C:\Program Files (x86)\Prince\engine\bin;D:\aria2;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\GNU.Wget2_Microsoft.Winget.Source_8wekyb3d8bbwe;C:\Users\andyl\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-7.0-full_build\bin;C:\Users\andyl\AppData\Roaming\npm; {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable POSH_PID in collection to 2964 {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable PROMPT in collection to (base) $P$G {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable SSL_CERT_FILE in collection to C:\Users\andyl\miniconda3\Library\ssl\cacert.pem {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable _CONDA_OLD_CHCP in collection to 65001 {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable CONDA_ROOT in collection to C:\Users\andyl\miniconda3 {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Setting environment variable __CONDA_OPENSLL_CERT_FILE_SET in collection to "1" {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-04-23 19:02:02.450 [info] Send text to terminal: & python c:\Users\andyl\.vscode\extensions\ms-python.python-2024.4.1\python_files\printEnvVariablesToFile.py c:\Users\andyl\.vscode\extensions\ms-python.python-2024.4.1\python_files\deactivate\powershell\envVars.txt ```

Extension version: 2024.4.1 VS Code version: Code 1.88.1 (e170252f762678dec6ca2cc69aba1570769a5d39, 2024-04-10T17:41:02.734Z) OS version: Windows_NT x64 10.0.22631 Modes:

User Settings

``` languageServer: "Pylance" ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |.NET Install Tool|ms-dotnettools.vscode-dotnet-runtime|2.0.3| |Azure Repos|ms-vscode.azure-repos|0.38.0| |Bash Beautify|shakram02.bash-beautify|0.1.1| |Better Comments|aaron-bond.better-comments|3.0.2| |Bookmarks|alefragnani.Bookmarks|13.5.0| |C/C++|ms-vscode.cpptools|1.19.9| |C/C++ Themes|ms-vscode.cpptools-themes|2.0.0| |Catppuccin for VSCode|Catppuccin.catppuccin-vsc|3.13.0| |Catppuccin Icons for VSCode|Catppuccin.catppuccin-vsc-icons|1.11.0| |clangd|llvm-vs-code-extensions.vscode-clangd|0.1.28| |CMake Tools|ms-vscode.cmake-tools|1.17.17| |Code Runner|formulahendry.code-runner|0.12.2| |CodeLLDB|vadimcn.vscode-lldb|1.10.0| |CodeQL|GitHub.vscode-codeql|1.12.5| |CodeSnap|adpyke.codesnap|1.3.4| |Data Wrangler (Preview)|ms-toolsai.datawrangler|0.28.0| |Dev Containers|ms-vscode-remote.remote-containers|0.354.0| |Docker|ms-azuretools.vscode-docker|1.29.0| |ESLint|dbaeumer.vscode-eslint|2.4.4| |Even Better TOML|tamasfe.even-better-toml|0.19.2| |Gather|ms-python.gather|2022.3.2| |GitHub Actions|github.vscode-github-actions|0.26.2| |GitHub Codespaces|GitHub.codespaces|1.16.19| |GitHub Copilot|GitHub.copilot|1.181.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.14.1| |GitHub Pull Requests|GitHub.vscode-pull-request-github|0.86.1| |GitHub Repositories|GitHub.remotehub|0.62.0| |GitHub Theme|GitHub.github-vscode-theme|6.3.4| |GitLens — Git supercharged|eamodio.gitlens|14.9.0| |Hex Editor|ms-vscode.hexeditor|1.9.14| |IntelliCode|VisualStudioExptTeam.vscodeintellicode|1.3.1| |isort|ms-python.isort|2023.10.1| |JavaScript Debugger|ms-vscode.js-debug|1.88.0| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.2| |Jest|Orta.vscode-jest|6.2.4| |Jupyter|ms-toolsai.jupyter|2024.3.1| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.17| |Jupyter PowerToys|ms-toolsai.vscode-jupyter-powertoys|0.1.1| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |JupyterHub|ms-toolsai.jupyter-hub|2023.10.1003012305| |Lex|luniclynx.lex|0.1.0| |Makefile Tools|ms-vscode.makefile-tools|0.9.10| |Markdown Preview Enhanced|shd101wyy.markdown-preview-enhanced|0.8.13| |markdownlint|DavidAnson.vscode-markdownlint|0.55.0| |Marp for VS Code|marp-team.marp-vscode|2.8.0| |Microsoft Edge Tools for VS Code|ms-edgedevtools.vscode-edge-devtools|2.1.5| |Nsight Visual Studio Code Edition|nvidia.nsight-vscode-edition|2023.2.32964508| |One Dark Pro|zhuangtongfa.material-theme|3.17.0| |OpenAPI (Swagger) Editor|42Crunch.vscode-openapi|4.25.2| |Peacock|johnpapa.vscode-peacock|4.2.2| |PostgreSQL|ms-ossdata.vscode-postgresql|0.3.0| |Postman|Postman.postman-for-vscode|1.0.1| |PowerShell|ms-vscode.powershell|2024.2.1| |Prettier - Code formatter|esbenp.prettier-vscode|10.4.0| |Print|pdconsec.vscode-print|0.11.19| |Prisma Import|ajmnz.prisma-import|5.12.1| |Project Manager|alefragnani.project-manager|12.8.0| |Pylance|ms-python.vscode-pylance|2024.4.1| |Pylint|ms-python.pylint|2023.10.1| |Python|ms-python.python|2024.4.1| |Python Debugger|ms-python.debugpy|2024.4.0| |Rainbow CSV|mechatroner.rainbow-csv|3.11.0| |Remote - SSH|ms-vscode-remote.remote-ssh|0.110.1| |Remote - SSH: Editing Configuration Files|ms-vscode-remote.remote-ssh-edit|0.86.0| |Remote - Tunnels|ms-vscode.remote-server|1.5.1| |Remote Development|ms-vscode-remote.vscode-remote-extensionpack|0.25.0| |Remote Explorer|ms-vscode.remote-explorer|0.4.3| |Remote Repositories|ms-vscode.remote-repositories|0.40.0| |rust-analyzer|rust-lang.rust-analyzer|0.3.1932| |SQLite|alexcvzz.vscode-sqlite|0.14.1| |SQLTools|mtxr.sqltools|0.28.3| |Surround|yatki.vscode-surround|1.5.0| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.8| |Test Adapter Converter|ms-vscode.test-adapter-converter|0.1.9| |Test Explorer UI|hbenl.vscode-test-explorer|2.21.1| |Tokyo Night|enkia.tokyo-night|1.0.6| |VS Code Speech|ms-vscode.vscode-speech|0.8.0| |vscode-icons|vscode-icons-team.vscode-icons|12.7.0| |vscode-openapi-viewer|AndrewButson.vscode-openapi-viewer|1.1.2| |vscode-pdf|tomoki1207.pdf|1.2.2| |WSL|ms-vscode-remote.remote-wsl|0.88.0| |x86 and x86_64 Assembly|13xforever.language-x86-64-assembly|3.1.4| |XMake|tboox.xmake-vscode|2.3.3| |YAML|redhat.vscode-yaml|1.14.0| |YOJ|HuanCheng65.vscode-yoj|0.1.1|
System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (12 x 2592)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|31.91GB (12.46GB free)| |Process Argv|--crash-reporter-id e5f6c82f-4975-44ec-b7f1-fd21deba716e| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805cf:30301675 binariesv615:30325510 vsaa593:30376534 py29gd2263:31024239 vscaac:30438847 c4g48928:30535728 azure-dev_surveyone:30548225 vscrpc:30673769 962ge761:30959799 pythongtdpath:30769146 welcomedialogc:30910334 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 h48ei257:31000450 pythontbext0:30879054 accentitlementst:30995554 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfnew:31000557 d34g3935:30971562 fegfb526:30981948 bg6jg535:30979843 ccp2r3:30993541 dsvsc020:30976470 pythonait:31006305 gee8j676:31009558 dsvsc021:30996838 01bff139:31013167 pythoncenvpt:31022790 ```
karthiknadig commented 4 months ago

Open a new terminal session and check the MESSAGE environment variable

This is a VS Code behavior where it persists the environment variables used to create the terminal. This is independent of the python extension. If you can create a separate feature request for the .env part (see below), I can move this issue to VS Code.

Detecting updates to .env and comments in it

This is a feature request. Please file a separate issue for that.

andylizf commented 4 months ago

@karthiknadig The current behavior where environment variables from .env files are automatically applied to terminal sessions seems inconsistent with VS Code documentation, which does not indicate such behavior. Additionally, the handling of comments (being included in variable values) contradicts the documented use of # for comments. These issues seem like bugs rather than feature requests and might benefit from being addressed to align with the documentation’s guidelines.

karthiknadig commented 4 months ago

@andylizf I think the parsing comments part is a bug, but updating environment variables as a part of .env file modification is a feature request.

@luabud We might need to update our documentation on the .env files. We now include the env variables from .env files when we create terminals.

github-actions[bot] commented 3 months ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

Happy Coding!