Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh integrated terminal with .env variables after changing .env file #24815

Open
ScottWilliamAnderson opened this issue Feb 14, 2025 · 4 comments
Assignees
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug needs spike Label for issues that need investigation before they can be worked on.

Comments

@ScottWilliamAnderson
Copy link

ScottWilliamAnderson commented Feb 14, 2025

Type: Bug

Behaviour

I've found that there is no way for me to update the living integrated terminal with the updated .env variables, is that right? The only way is to kill the terminal and start a new one. Relaunch terminal does not relaunch it, and does not update environment variables

Steps to reproduce:

create a .env file in a python project folder, if you make any changes to it, your terminal will show an orange warning triangle and suggest either Relaunching the terminal or Show environment contributions.
Your newly added environment variables from the .env will never arrive to already launched terminals.

Diagnostic data

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

2025-02-14 09:27:52.099 [info] > & ./.venv/Scripts/Activate.ps1 ; echo 'e8b39361-0157-4923-80e1-22d70d46dee6' ; python ~/.vscode/extensions/ms-python.python-2025.0.0-win32-x64/python_files/printEnvVariables.py
2025-02-14 09:27:52.099 [info] shell: powershellCore
2025-02-14 09:27:54.847 [info] Setting environment variable COMPOSE_FILE in collection to docker-compose-dev.yml {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable CONDA_PROMPT_MODIFIER in collection to False {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable DB_USER in collection to readonly-django-user {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable DISABLE_API_AUTH in collection to True {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable DISABLE_API_CACHE in collection to True {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable OT_API_KEY in collection to da2-x7u2ucifubbttnaz5ezhzhckmy {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable OT_API_URL in collection to https://gql.open-trade.io {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Prepending environment variable PATH in collection to c:\Users\username\.vscode\extensions\ms-python.python-2025.0.0-win32-x64\python_files\deactivate\powershell;C:\Users\username\Source\my-project\.venv/Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\PostgreSQL\16\bin;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_211\bin;C:\Hadoop\hadoop-3.3.6\bin;C:\Program Files\WireGuard\;C:\Program Files\WezTerm;C:\Program Files (x86)\oh-my-posh\bin;C:\Program Files\PowerShell\7\;C:\Program Files\GitHub CLI\;C:\Users\username\.local\bin;C:\Users\username\scoop\shims;C:\Users\username\AppData\Local\Microsoft\WindowsApps;C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\username\AppData\Roaming\npm;C:\Users\username\AppData\Local\Programs\Microsoft VS Code Insiders\bin;;C:\Users\username\AppData\Local\spicetify; {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable POSH_SESSION_ID in collection to c13863ce-f399-4200-9e93-365e4a93f9db {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable POSH_SHELL_VERSION in collection to 7.5.0 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable POSH_THEME in collection to C:\Users\username\Source\scripts\oh-my-posh\plenty-of-info.omp.json {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable POWERLINE_COMMAND in collection to oh-my-posh {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable PSMODULEPATH in collection to C:\Users\username\OneDrive - FiveSigma\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable PYTHONIOENCODING in collection to utf-8 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable PYTHONUNBUFFERED in collection to 1 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable SECRET_KEY in collection to j#mba3)n^__zfwuz2k(ph*1*q1ik@s^iaeo#v$cxwy!_r%+!$( {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable TEMP in collection to C:\Users\username\AppData\Local\Temp {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable TMP in collection to C:\Users\username\AppData\Local\Temp {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable USE_DISKCACHE in collection to 0 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable VIRTUAL_ENV in collection to C:\Users\username\Source\my-project\.venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2025-02-14 09:27:54.847 [info] Setting environment variable VIRTUAL_ENV_PROMPT in collection to my-project {"applyAtShellIntegration":true,"applyAtProcessCreation":true}

Extension version: 2025.0.0
VS Code version: Code 1.97.1 (e249dada235c2083c83813bd65b7f4707fb97b76, 2025-02-10T13:13:58.153Z)
OS version: Windows_NT x64 10.0.26100
Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.8
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Pylance
User Settings


languageServer: "Pylance"

Installed Extensions
Extension Name Extension Id Version
All-Autocomplete Ati 0.0.26
atlascode atl 3.4.5
autodocstring njp 0.6.1
autopep8 ms- 2024.2.0
better-comments aar 3.0.2
Bookmarks ale 13.5.0
code-runner for 0.12.2
code-spell-checker str 4.0.34
codebucket pst 0.2.0
codestream Cod 15.21.0
codium Cod 0.14.7
control-snippets svi 1.9.1
copilot Git 1.271.0
copilot-chat Git 0.23.2024102903
copilot-workspace git 0.5.4
cursor-charcode but 0.9.2
datawrangler ms- 1.18.2
debug-visualizer hed 2.4.0
debugpy ms- 2025.0.1
docxreader Sha 1.1.3
dotenv mik 1.0.1
easy-snippet inu 0.7.5
enhancedcodachi van 0.0.6
esbonio swy 0.11.0
explorer-exclude Pet 1.3.2
export-your-extensions Bha 1.0.2
gc-excelviewer Gra 4.2.63
geo-data-viewer Ran 2.6.0
gistfs vsl 0.6.0
git-graph mhu 1.30.0
git-tree-compare let 1.17.0
gitignore cod 0.9.0
gitignore mic 1.0.1
gitlens eam 16.3.0
gremlins nho 0.26.0
html-preview-vscode geo 0.2.5
hungry-delete jas 1.7.0
indent-one-space use 1.0.0
intellicode-api-usage-examples Vis 0.2.9
isort ms- 2023.10.1
js-debug ms- 1.97.1
js-debug-companion ms- 1.1.3
js-unicode-preview adp 1.1.4
jsoncrack-vscode Ayk 3.0.0
jupyter ms- 2024.11.0
jupyter-keymap ms- 1.1.2
jupyter-renderers ms- 1.1.0
local-history xyz 1.8.1
macros ctf 1.1.1
makefile-tools ms- 0.11.13
markdown-all-in-one yzh 3.6.2
markdown-checkbox bie 0.4.0
metago met 4.3.3
MetaJump met 1.4.4
MetaWord met 1.3.2
output-colorizer IBM 0.1.2
parquet-explorer Ada 1.2.1
partial-diff ryu 1.4.3
pdf tom 1.2.2
polacode pnp 0.3.4
project-manager ale 12.8.0
python ms- 2025.0.0
python-environment-manager don 1.2.7
rainbow-csv mec 3.17.0
remote-containers ms- 0.398.0
remote-wsl ms- 0.88.5
restructuredtext lex 190.4.5
rewrap stk 1.16.3
search-editor-apply-changes jak 0.1.1
selected-character-count mou 0.0.1
simple-rst tro 1.5.4
snakeviz til 0.0.2
todo-tree Gru 0.0.226
vscode-checkpoints mic 1.3.3
vscode-docker ms- 1.29.4
vscode-edge-devtools ms- 2.1.8
vscode-essentials jab 1.6.0
vscode-format-context-menu lac 1.0.4
vscode-github-actions git 0.27.1
vscode-js-profile-table ms- 1.0.10
vscode-jupyter-cell-tags ms- 0.1.9
vscode-jupyter-slideshow ms- 0.1.6
vscode-multiclip sle 0.1.5
vscode-open san 0.3.0
vscode-peacock joh 4.2.2
vscode-power-mode hoo 3.0.2
vscode-pull-request-github Git 0.104.1
vscode-pylance ms- 2025.2.1
vscode-settings-cycler hoo 1.0.1
vscode-spotify shy 3.2.1
vscode-sqlite ale 0.14.1
vscode-text-pastry jkj 1.3.1
vscode-yaml red 1.16.0
vscodeintellicode Vis 1.3.2
vsliveshare ms- 1.0.5948
A/B Experiments
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyt551cf:31179979
vscod805:30301674
binariesv615:30325510
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
a9j8j154:30646983
962ge761:30959799
2e7ec940:31000449
pythontbext0:30879054
cppperfnew:31000557
dwnewjupyter:31046869
nativerepl1:31139838
pythonrstrctxt:31112756
nativeloc1:31192215
iacca1:31171482
5fd0e150:31155592
dwcopilot:31170013
stablechunks:31184530
6074i472:31201624
dwoutputscf:31238124
customenabled:31237564
5a9hg449:31230677
9064b325:31222308
copilot_t_ci:31222730
jda6j935:31233686

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Feb 14, 2025
@ScottWilliamAnderson
Copy link
Author

Is there any additional data I can provide to help with this?

@anthonykim1 anthonykim1 self-assigned this Feb 18, 2025
@anthonykim1 anthonykim1 added area-terminal bug Issue identified by VS Code Team member as probable bug needs spike Label for issues that need investigation before they can be worked on. and removed triage-needed Needs assignment to the proper sub-team labels Feb 18, 2025
@anthonykim1
Copy link

anthonykim1 commented Feb 24, 2025

Hello @ScottWilliamAnderson Thanks for filing this issue.

Few things to clarify before I dig into this further.

Q1. Are you able to get the terminal with updated variables from your .env when you kill your current terminal and launch a "new", fresh terminal?

Relaunch terminal does not relaunch it, and does not update environment variables

Q2. Do you mean you can launch the terminal with the "relaunch terminal" button, but just not have your updated variables from your updated .env file?

I did see

create a .env file in a python project folder, if you make any changes to it, your terminal will show an orange warning triangle and suggest either Relaunching the terminal or Show environment contributions.
Your newly added environment variables from the .env will never arrive to already launched terminals.

but just wanted to triple check.

Q3. Which shell are you using on your Windows machine?

Thanks again.

@anthonykim1
Copy link

Also, another important one is:

Are you able to repro this outside of Python experience?
Since .env files are not really specific to Python, I wonder if you suffer from same problem when you are outside of Python experience in VS Code, if so we should get this moved to VS Code itself.

@ScottWilliamAnderson
Copy link
Author

Hi @anthonykim1, thank you for taking a look at this.

Q1. Are you able to get the terminal with updated variables from your .env when you kill your current terminal and launch a "new", fresh terminal?

Yes, in a Python workspace, the newly launched terminals respect the latest saved .env variables.

Q3. Which shell are you using on your Windows machine?

I am using Powershell 7.5.0

Q2. Do you mean you can launch the terminal with the "relaunch terminal" button, but just not have your updated variables from your updated .env file?

I came across something strange digging further into this:

  • I made an empty folder, and opened in VSCode. I added a .env file with an environment variable.
  • Opening the integrated terminal (I use Powershell 7 and run gci env:* | sort-object name) the variable is not present. I Believe this is expected behaviour.
  • If I add a "script.py" file to this folder with something short like import antigravity, launch a new integrate terminal, and run the same gci env:* | sort-object name command, the env variable is still not present. I also think this is expected behaviour.
  • If I create a python virtual environment, set it as the default interpreter and run script.py, then the hover information for the integrated terminal with the orange triangle warning icon appears. It prompts to relaunch the terminal. Clicking on the blue "Relaunch Terminal" link in the hover information reloads the terminal as expected. The env variable is then present. Also any newly opened terminals respect the latest .env at the time of creation.
  • For some reason, in my larger, existing python repositories, the Blue hyperlink "Relaunch Terminal" in the hover does not do anything. This is a mild nuisance, but I have found a simple workaround: I've set a keyboard shortcut for the "Terminal: Relaunch Active Terminal" Command:

Image

I find this actually easier than having to navigate my mouse within the hover on the terminal name. Using the Keyboard shortcut or invoking the same command via f1, works as expected.

I could improve slightly my setup by debugging this and pinning down the cause of the failed relaunch using the hover link, but I don't think it's worth the time now that I have the keyboard shortcut.

I suppose my original curiosity still stands,

I've found that there is no way for me to update the living integrated terminal with the updated .env variables, is that right?

So e.g. if you are running a server on your integrated terminal, the only way for it to update its environment variables with new .env data is to shut it down and relaunch it, given you have to relaunch the terminal its living in.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug needs spike Label for issues that need investigation before they can be worked on.
Projects
None yet
Development

No branches or pull requests

2 participants