Skip to content

When VS Code is launched from a terminal with an activated environment, select it #18178

Closed as not planned

Description

For both virtual and conda environments, if you launch VS Code from an activated environment, we should pick that up and automatically select that environment for the user if no environment had been previously selected.

One open question, though, is what to do if you had an environment previously selected? Do we:

  1. Ignore the one you launched VS Code under?
  2. Automatically select the activated one?
  3. Prompt to see whether you want to keep your previously selected environment or switch to the activated one?

Activity

  1. added
    area-environmentsFeatures relating to handling interpreter environments
    and removed
    triage-neededNeeds assignment to the proper sub-team
    on Dec 15, 2021
  2. paulacamargo25 commented on Dec 15, 2021

    @paulacamargo25

    The team discuss this and we decided that we will select the activated environment if there is no environment explicitly selected by User.

  3. removed this from the April 2022 milestone on Apr 26, 2022
  4. 5 remaining items

  5. karrtikr commented on Apr 29, 2022

    @karrtikr

    if you have installed conda you might always have a base environment activated

    Another situation: A common workaround for conda issues today is to use the Anaconda prompt to launch VSCode which already has base activated. In this case they would still expect to change their conda env to any new ones they create.

  6. greazer commented on Apr 29, 2022

    @greazer

    For sure the code . scenarios need to be addressed, though I think they can be handled in a reasonable way (notifications, errors, if necessary) if we went this direction. Example. Let's say that the user had FolderA opened with the A environment active. If the user launches code from the B environment, then a few things could happen.

    • Tell the user that FolderA was already opened with a different active environment and abort.
    • Tell the user and offer to reload FolderA with the B environment.
    • Support having separate instances of FolderA being using the environment as a differentiator.

    For no. 3, yes, users could have a base environment activated and so they would be limited to just that base environment. That's exactly what I'm implying. This doesn't seem to be a big problem as they may want to work in the base environment OR if not they'd just activate the environment they want before launching. Alternatively, this could be considered a special case for anaconda provided we can detect that they are in the base environment. As long as the user stays within the anaconda environment list, allow them to choose any of them. I would guess that for users who use an anaconda base environment, it's unlikely they'd wander outside of anaconda.

    Another idea would be to provide a flag allowing the user to enforce ignoring the activated environment (and operate like we do today). Though I think the fact that a user has started code from within an activated environment at all, especially an anaconda environment, it may not be possible to completely ignore it and has been the source of problems in the past.

  7. brettcannon commented on May 2, 2022

    @brettcannon
    MemberAuthor

    Not only do I think that we should do this (select the activated environment), I'd like you to consider a couple more options...

    1. Ensure that the activated environment overrides any selected environment the user has set in settings.

    I plan to test this out in WWBD.

    1. Ensure that the activated environment is only applicable during the lifetime of that VS Code session (across reloads of course)

    I don't quite follow here. You say the chosen environment "is only applicable during the lifetime of that ... session", but you always say that it should persist "across reloads of course". To me that seems contradictory. Are you suggesting that the selection of the activated environment is ephemeral and forgotten once VS Code is quit?

    1. This is the kicker... Don't allow the user to change the active environment at all in this mode.

    I don't think this can be a mode; it's all or nothing. So either we say you must launch VS Code from a shell with an activated environment (which isn't possible for those that launch from a shortcut on their desktop), or you have the option to potentially undue your mistake of using the wrong environment in your shell for your workspace. It also isn't applicable across workloads to lock you into what you launched with: if I have multiple environments set up for different Python versions I shouldn't have to quit VS Code just to change my environment.

    How do you detect this condition? I launch VS code from a command line all the time. The environment in this situation is my global python. Do you skip this logic for a global python?

    Both venv and conda set environment variables when an environment is activated. I personally plan to check for those environment variables during activation, and if they are set then forcibly set the environment via WWBD.

  8. DonJayamanne commented on May 3, 2022

    @DonJayamanne

    Both venv and conda set environment variables when an environment is activated. I personally plan to check for those environment variables during activation, a
    nd if they are set then forcibly set the environment via WWBD.

    Is it possible to add some telemetry to check these so we know how many users activate and launch vscode.
    At one point we (Jupyter ext) were supposed to do this to figure out how often users do this, which could in turn be an indication of why kernels fail to work.

  9. brettcannon commented on May 3, 2022

    @brettcannon
    MemberAuthor

    Is it possible to add some telemetry to check these so we know how many users activate and launch vscode.

    Probably could, but I honestly don't see any reason not to do this; doing it in WWBD just helps get it out faster and test it without breaking people's workflow. I would expect we would do this as an experiment in the core extension rather than worry about telemetry of potential use since this shouldn't be too expensive to do.

  10. greazer commented on May 3, 2022

    @greazer

    Woot! Love the idea of trying it out in WWBD.

    Ensure that the activated environment is only applicable during the lifetime of that VS Code session (across reloads of course)

    I don't quite follow here. You say the chosen environment "is only applicable during the lifetime of that ... session", but you always say that it should persist "across reloads of course". To me that seems contradictory. Are you suggesting that the selection of the activated environment is ephemeral and forgotten once VS Code is quit?

    Maybe this isn't an issue. My thought was that if the user starts VS Code from an activated environment, it should always be that selected Python environment. However, it doesn't "stick" such that if they launch VS Code from a shortcut on the desktop that that last chosen environment will be used.

    This is the kicker... Don't allow the user to change the active environment at all in this mode.

    I don't think this can be a mode; it's all or nothing. So either we say you must launch VS Code from a shell with an activated environment (which isn't possible for those that launch from a shortcut on their desktop), or you have the option to potentially undue your mistake of using the wrong environment in your shell for your workspace. It also isn't applicable across workloads to lock you into what you launched with: if I have multiple environments set up for different Python versions I shouldn't have to quit VS Code just to change my environment.

    I'm not totally following here. I think what you're saying is that we cannot lock the user's environment if they started from that environment because they have the ability to change the selected environment once VS Code is started. Right. And that's my point. We can know if a user started with an activated environment (other than "base" possibly), and if they did, then disable/hide/error out the commands that allow the user to change it. Is this at least technically possible to pull off? If not, then the discussion can end. But it seems possible and is at least an idea to play around with. :) Assuming it's possible, if a user wants to use another environment for a VS Code instance started this way, then they'd have to shut down that instance. I know it sounds crazy, but this is exactly how Jupyter works today. I'm not saying we just have to copy Jupyter. What I'm saying is that it can provide a simplified and intuitive experience for many.

    For users that start from a shortcut, the limitation wouldn't exist. They'd be free to choose an environment just how they do today.

  11. brettcannon commented on May 4, 2022

    @brettcannon
    MemberAuthor

    My thought was that if the user starts VS Code from an activated environment, it should always be that selected Python environment. However, it doesn't "stick" such that if they launch VS Code from a shortcut on the desktop that that last chosen environment will be used.

    If WWBD does the setting, then that selection will persist for that workspace until it's changed.

    I think what you're saying is that we cannot lock the user's environment if they started from that environment because they have the ability to change the selected environment once VS Code is started. Right.

    Not "because they have the ability" as in "it's functionality we already provide," but more that it's a valid use-case to want to change your environment. For instance, I know library developers who need to test against multiple Python versions and so have multiple environments set up. If they happen to launch from an activated environment for Python 3.10, run e.g. Nox to test against all the Python versions installed, and get a failure for Python 3.7, I don't want to force them to quit VS Code just to switch to the 3.7 environment to try and fix their issue.

    We can know if a user started with an activated environment (other than "base" possibly), and if they did, then disable/hide/error out the commands that allow the user to change it. Is this at least technically possible to pull off?

    With enough blood, sweat, and tears, sure. 😉 We would have to store some global state upon execution as to how the current environment was selected and then do something to either prevent or error out when trying to change the environment.

    is at least an idea to play around with. :) Assuming it's possible

    Do we have telemetry showing that this is a concern? It's going to take away a use-case, so I would be hesitant to do a potentially deep change to guard all potential triggers of the quick-pick for environments unless this was data-driven.

    I know it sounds crazy, but this is exactly how Jupyter works today.

    Jupyter also doesn't launch VS Code today. 😉

    In all seriousness, this does get a bit philosophical very quickly as to whether we want to match the typical Jupyter workflow to this extent? I think the issue here is while you could argue like Jim is that Jupyter users may like this footgun-avoidance feature, that's the only group who may want this. So this would then become a conda-only situation, but it's really a "conda user using Jupyter" situation which is even narrower and harder to identify based on intent until they launch the Jupyter extension.

    But maybe that's the potential feature if we choose to pursue this? If you launch the Jupyter extension we lock you out from changing your environment. But I would definitely want to drop Jupyter as a soft dependency first in that case to avoid accidental triggering of this feature.

  12. removed this from the June 2022 milestone on Jun 21, 2022
  13. karrtikr commented on Mar 3, 2023

    @karrtikr

    It's done. Closing as in favor of #10668.

  14. locked as resolved and limited conversation to collaborators on Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-environmentsFeatures relating to handling interpreter environmentsfeature-requestRequest for new features or functionalityneeds PRReady to be worked on

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions