Skip to content

Use 'conda run' when debugging user code #8422

Description

Work item associated to the spike #8421

Activity

  1. added this to the FY20Q2 milestone on Nov 19, 2019
  2. karrtikr commented on Nov 25, 2019

    @karrtikr

    To make this work,

    • LaunchRequestArguments.pythonPath is deprecated in the debugger. Remove all occurences of "pythonPath" is the extension.
    • Check conda version to make sure conda run command is supported.
    • If the current interpreter is conda, fetch two things.
      • <path to conda.exe> : Use condaService.getCondaFile() to do that.
      • Name and path of the conda environment : Use condaService.getCondaEnvironment() to fetch that.
        Now add this to the launch configuration in the debug resolver,
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "python": ["<path to conda.exe>", "run", "-n","<name of environment>", "python"] // <--- This is what you will have to add if conda environment has a name
    }
    

    OR

    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "python": ["<path to conda.exe>", "run", "-p","<path to environment>", "python"] // <--- This is what you will have to add if conda environment has does not have name
    }
    
    • If the current interpreter is not conda, you have to add "python": <path to the selected python interpreter> in the launch resolver. This is because debugger expects 'python' must have at least 1 element.
    • In earlier versions, one could use double dashes in conda run command to separate CLI flags for clarity. Something like,
    conda run -n base -- python <...>`
    

    But it seems support for that has been deprecated in conda. However one could still use

    conda run -n base python -- <...>`
    

    as python is able to parse double dashes.

    • Edit package.json to support intellisense for "python" in launch.json. Also make sure to remove intellisense for "pythonPath".
  3. DonJayamanne commented on Nov 25, 2019

    @DonJayamanne

    If the current interpreter is conda, fetch two things

    Please ensure we re-use existing code instead of hardcoding logic in other places.
    As it is, we have 3 rules (if cond, if env name, if env path) in this place and we have missed one crucial rule (checking version of conda). Hence the need to re-use code.
    Kim-Adeline Miguel (@kimadeline) Karthik Nadig (@karthiknadig) /cc

  4. int19h commented on Nov 25, 2019

    @int19h

    If the current interpreter is not Conda, the extension should provide the full path to the corresponding Python binary in "python", just like it did before via "pythonPath".

    In the common case of a single value, it can be specified directly - i.e. "python": "foo" is the same as "python": ["foo"]

    But note that "python" is a ptvsd 5 thing, so none of this should kick in if the older version is in use - it should continue to use "pythonPath".

  5. karrtikr commented on Nov 25, 2019

    @karrtikr

    Pavel Minaev (@int19h) Oh right. Edited the issue accordingly.
    Don Jayamanne (@DonJayamanne) I added that we need to check conda version only to check if conda run is supported.

  6. DonJayamanne commented on Nov 25, 2019

    @DonJayamanne

    Still unsure why we're documenting an existing logic. what if another is missed again.
    Also this solution implies we write this code again, instead of re-using... Anyways, thats my opinion .

  7. karrtikr commented on Nov 26, 2019

    @karrtikr

    Oh I see what you're saying.
    Was just documenting to make sure we don't forget about it. We'll definitely try to re-use existing logic where we can.

  8. karthiknadig commented on Dec 3, 2019

    @karthiknadig
    Member

    Make sure that when we test this, we also test it with auto-activate terminal setting turned on.

  9. DonJayamanne commented on Dec 3, 2019

    @DonJayamanne

    Shouldn't we use conda activate then run python code, instead of conda run?
    I think conda run would work when not running in a terminal, but when in a terminal conda activate would be better

    Else user output won't be displayed in terminal, input will not work, etc..

  10. luabud commented on Dec 5, 2019

    @luabud
    Member

    Don Jayamanne (@DonJayamanne) this is for calling the debugger, not running python code

  11. DonJayamanne commented on Dec 5, 2019

    @DonJayamanne

    calling the debugger, not running python code

    But with the way debug adapter is designed, why do we even need to run the adapter with conda run!
    The adapter doesn't run any user code? So it's not necessary at all.
    It's the process that's launched by the adapter that needs conda environment

    Pavel Minaev (@int19h) Karthik Nadig (@karthiknadig) /cc

  12. int19h commented on Dec 5, 2019

    @int19h

    That's exactly what the proposal does: "python" is a property that gets parsed by the adapter; or rather by the launcher, which is spawned by the adapter via "runInTerminal" request to VSC. The launcher then applies it when spawning the debuggee. Neither the adapter nor the launcher run in the activated environment.

    Are you saying that the debuggee needs to be spawned using conda activate rather than conda run, due to conda/conda#8386? It looks like they have fixed the issue with redirection recently, so I don't know if we have to worry about that anymore.

    If we do, I'm not sure how we can pull that off. We'd need to issue conda activate first as a separate "runInTerminal" request, but the problem with those is that VSC sends the response to it as soon as the command starts running - there's no way for them to tell when it actually completes, though. So if we just send two requests sequentially, I don't think that'll do the right thing.

  13. 6 remaining items

  14. int19h commented on Dec 5, 2019

    @int19h
  15. DonJayamanne commented on Dec 5, 2019

    @DonJayamanne

    To clarify, what I'd like to avoid is having to re-implement all of this:

    I agree, there's more to this (keeping track of terminals, etc). And that's something we wanted to avoid in core extension team (back when I was there). Brett Cannon (@brettcannon) is aware of these discussions.

  16. DonJayamanne commented on Dec 5, 2019

    @DonJayamanne

    Pavel Minaev (@int19h) Had a chat with Karthik Nadig (@karthiknadig) about an alternative. Creating a terminal with the right environment variables. This might help https://github.com/microsoft/vscode-python/issues/8928

  17. removed this from the FY20Q2 milestone on Feb 14, 2023
  18. karrtikr commented on Mar 3, 2023

    @karrtikr

    Based on chats with the team:

    • The gist of what we need to do is set the python field in the launch configuration to the

      [ "conda" , "run", "-n", "env_name", "--no-capture-output", "python"]
      

      and set the adapterPython to whatever python executable we want.

    • This means it'll also use this command to run the launcher, unless we specify "debugLauncherPython", so it will show up as "conda run" in the terminal, which is what the users might expect. However note it does mean that we'll spawn "conda run" from inside another "conda run", which is very recently fixed Running using conda run inside already activated environment should be the same as running it outside conda/conda#11305.

    • So we are waiting on conda to let us know their support timelines based on we can start using this approach.

    With #20651 we're now using conda run to get environment variables in cases where some other python is explicitly specified by user. We should remove this with this issue.

  19. removed their assignment
    on Mar 3, 2023
  20. karrtikr commented on Sep 12, 2023

    @karrtikr

    We're currently getting env variables using conda run and applying to debugging or executing #20651, which also has been working well. Hence closing as not required for now.

  21. brettcannon commented on Sep 13, 2023

    @brettcannon
    Member

    We're currently getting env variables using conda run and applying to debugging or executing #20651, which also has been working well. Hence closing as not required for now.

    And I assume this won't be a concern when debugging is launched from the Python Debugger extension or if people turn off the terminal activation feature?

  22. karrtikr commented on Sep 13, 2023

    @karrtikr

    That's right, we intercept and add the env variables independently of the experiment.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions