Repository navigation
Spike: Investigate approach to use environment variables for environment activation #20492
Copy link
Copy link
Closed
Closed
Copy link
Labels
area-debuggingarea-environmentsFeatures relating to handling interpreter environmentsFeatures relating to handling interpreter environmentsarea-terminalfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds spikeLabel for issues that need investigation before they can be worked on.Label for issues that need investigation before they can be worked on.verifiedVerification succeededVerification succeeded
Milestone
Description
For #11039
Getting activated environment variables from conda
Get activated environment variables for each shell.
- Variables for each shell might be different. For eg. the prompt variable.
- Shell path to conda for each shell might be different. For eg. cmd might be
conda initalready but powershell might not be.
Diff is another option. Can we use diff for cmd in powershell, and add the prompt variable ourselves?
Use environment variable collection to activate terminals
Blockers: microsoft/vscode#99878 on macOS
- Handling the debugger
pythonconfig scenario- Which one takes precedence: environment var collection or env vars run in terminal request or envs supplied when creating terminal
- Kind of like internal console, except for rc and profile scripts?
- createTerminal(env) overrides environment variable collection and env in runinterminal request also affects the terminal.
- Cleanup collection prompt

Also there are some meaningless changes: liketrueto1.
Instead of this would it make more sense to user to just say: Terminal needed to be restarted to apply results of<conda command>: Allow to provide description to environment collection API vscode#171108
Some other alternatives:
What about multiroot workspaces?
Environment variable collection for a workspace?
Clarify what:
Note that an extension can only make a single change to any one variable, so this will overwrite any previous calls to replace, append or prepend.
means.
Relaunch terminal doesn't show up always.
on did change default shell event? microsoft/vscode#160694 (comment)
onDidOpenTerminal and vscode.env.shell: microsoft/vscode#171164
- Progress notification and activating terminals when an interpreter is changed.
- Check with Conda team if relying on environment vars is sufficient. For eg. scripts could potentially set shell aliases as well.
- It probably doesn't make sense to have
python.terminal.activateinterminlsettings if we aren't sending commands. - on did change default shell event?
vscode.env.shellmight return empty string to extension if fetched very soon after startup vscode#160694 (comment) - Follow up with VSCode regarding Provide
onDidChangeShellevent which is fired when default terminal profile changes vscode#171164 (comment) - Disable it for multiroot workspaces
- Add it behind experiment
- Get conda binary for a different shell than cmd.
- Create exp on control tower
- Handle debugger python scenario
- cmd to powershell conda.ts bug
- Only use cmd as a fallback if powershell fails
- Look into git bash in windows
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-debuggingarea-environmentsFeatures relating to handling interpreter environmentsFeatures relating to handling interpreter environmentsarea-terminalfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds spikeLabel for issues that need investigation before they can be worked on.Label for issues that need investigation before they can be worked on.verifiedVerification succeededVerification succeeded

Activity
karrtikr commentedon Jan 11, 2023
Multiroot scenarios:
Each workspace can have a different environment selected. How to decide which one applies to the collection? Options:
Active editor decides the Python which appears in the status bar:

Every time active editor changes, the environment collection is updated to contain fresh environment variables.
Problems:
Active terminal decides which Python to activate using environment collection:

If cwd of first workspace is active, the environment of first workspace is applied.
Problems:
Potential requests from VSCode:
In the second case, if
envoption interminal.createTerminalAPI overrides the environment set by collection, we can use the right environment. The prompt to relaunch terminal is also not expected to show in this case.To make the first case work would require a more convoluted solution from VSCode, for eg. environment collection only applies to a particular workspace.
For simplicity it would also work if we could disable prompts altogether in multiroot workspaces:

Tyriar commentedon Jan 11, 2023
Kartik Raj (@karrtikr) am I understanding this right that the environment variable collection is applied to the workspace, not the individual folder in the workspace? Would allowing to apply a particular workspace to the
EnvironmentVariableMutatorsolve that issue?The idea of
envoverriding seems like a scary breaking change.karrtikr commentedon Jan 12, 2023
Yes
I assume you also mean existing APIs like
replace,appendetc. will have to be extended to be workspace folder specific as well, to indicate which workspace folder the changes are applied to. If so then yes this would be great.karrtikr commentedon Jan 12, 2023
Fish shell scenario:
Fish shell does not have a prompt variable which we could set like cmd to indicate which environment is active:
Potential requests from VSCode:
One idea is to use the
terminal.titleproperty to indicate the environment is activated: Allow to provide description to environment collection API vscode#171108Another idea could be to use shell integration script to insert an environment variable which could influence the prompt, inspiration taken from official
activate.fishscript:If the above script is amended to support something similar to:
Variable names would have to unique enough, but this way we can directly edit the
VSCODE_PS1variable to influence the prompt.karrtikr commentedon Jan 12, 2023
Debugger scenario:
We provide a way to debug a python file using its own debugger python that maybe different than what is currently selected:
However the collection is activated using the selected environment, so when debugging the file the incorrect activated shell is used (
juliais selected environment):Solution:
Add

envproperty to send environment variables of the debugger python environment:This is passed down using
RunInTerminalRequestto VSCode, so the user code is debugged using the expected environment variables. The downside is that if the terminal remains after the debug session completes, user might try to interact with it, and be confused because the shell therein is incorectly activated.Potential VSCode request:
One solution could be to for VSCode to use the env provided by
RunInTerminalRequestto also apply to thePython Debug consoleterminal that remains after debugging is done, instead of just using it to debug user code. Variables can be set when spawning the shell itself, but it is expected to override environment collection in this case.Treat terminal as single use:
In this approach, the lifetime of the shell in the terminal is tied to the lifetime of the debug session. We can use “wait on exit” debugger feature, which causes the debug session to present a “Press any key to continue…” prompt at the end, and session is not considered terminated until the prompt is answered.
Tyriar commentedon Jan 12, 2023
The trick would be coming up with a way to do it in a backwards compatible way so we don't break the API. Haven't thought about it too much but for example:
Tracked in microsoft/vscode#171173
Tyriar commentedon Jan 12, 2023
What if we allowed an extension to control a variable that is fed into both the title and description settings?
Tracked in microsoft/vscode#171175
karrtikr commentedon Jan 12, 2023
Seems reasonable to me, we can come back to it based on user feedback if need be:
karrtikr commentedon Mar 8, 2023
Created action item based on spike: #20822