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

Jupyter cell debugging does not support "step into" the third party library code with "justmycode:false" #8146

Closed
ckxckx opened this issue Oct 28, 2021 · 48 comments · Fixed by #11058 or #11689
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality notebook-debugging on-testplan
Milestone

Comments

@ckxckx
Copy link

ckxckx commented Oct 28, 2021

Jupyter cell debugging does not support "step into" the third party library code with "justmycode:false" in launch.json.

Please fix it !

@Qiuzhuang
Copy link

can not step into lib code even setting:
"jupyter.debugJustMyCode": false

@rebornix rebornix assigned roblourens and unassigned rebornix Nov 2, 2021
@roblourens roblourens transferred this issue from microsoft/vscode Nov 2, 2021
@roblourens roblourens added bug Issue identified by VS Code Team member as probable bug notebook-debugging labels Nov 2, 2021
@roblourens roblourens added this to the November 2021 milestone Nov 2, 2021
@rchiodo
Copy link
Contributor

rchiodo commented Nov 2, 2021

This is explicitly hardcoded here:

Likely because stepping into 3rd party code isn't going to do what you want. It will end up stepping through the display hooks for ipython and not the 3rd party library.

@int19h is there a way to add libs to the 'justMyCode' list?

@int19h
Copy link
Contributor

int19h commented Nov 3, 2021

I don't think we have anything documented for this, but pydevd exposes some environment variables to control this. Presumably you'd want the library to debug to be listed in IDE_PROJECT_ROOTS, but I'm not sure how this will interact with the default exclusion filters - @fabioz should know.

I wonder if perhaps the Jupyter extension should be using the exclusion filters on a lower level to specifically exclude all those display hooks, while leaving "justMyCode" setting to the user?

@rchiodo
Copy link
Contributor

rchiodo commented Nov 3, 2021

I wonder if perhaps the Jupyter extension should be using the exclusion filters on a lower level to specifically exclude all those display hooks, while leaving "justMyCode" setting to the user?

That sounds interesting too. How do we set exclusion filters?

@int19h
Copy link
Contributor

int19h commented Nov 3, 2021

It's all in the same code linked above, so currently, also via env vars (or using pydevd APIs). But we can always plumb that API through debugpy as needed.

@roblourens
Copy link
Member

Thanks, where does the justMyCode flag actually come in, is it implemented using those flags?

@DonJayamanne DonJayamanne removed this from the November 2021 milestone Nov 4, 2021
@DonJayamanne DonJayamanne added this to the November 2021 milestone Nov 4, 2021
@speediedan
Copy link

In case it's helpful to others until this is addressed, I've been able to workaround the issue effectively using IDE_PROJECT_ROOTS as suggested but would note one needs to add the appropriate ipykernel temp files directory (/tmp by default on Linux) in addition to the third party library code in IDE_PROJECT_ROOTS, e.g.:

IDE_PROJECT_ROOTS=/tmp:/opt/miniconda/envs/<myenv>/lib/<python_ver>/site-packages/<target third party lib>

otherwise, the 3rd-party breakpoints will work but the other breakpoints you set in your notebook will stop working. Hope this helps!

@jhcsjcao
Copy link

jhcsjcao commented Mar 4, 2022

Ok. After about 20 hours of trying, I got vscode to step into to dependent python libraries during debugging. I'm running Jupyter works too.

You have to use 'base' python interpreters - NOT python environments in vscode, and set 'justmycode' to false in vscode's launch.json.

My dependent library modules are in a subdirectory of the directory that contains my Jupyter notebook and is the first directory listed when I run 'os.sys.path'

Hope this helps!

@roblourens
Copy link
Member

Oops, yep thanks. But does ipykernel import that debugger.py right away?

@fabioz
Copy link

fabioz commented Oct 13, 2022

Oops, yep thanks. But does ipykernel import that debugger.py right away?

Apparently yes: https://github.com/ipython/ipykernel/blob/main/ipykernel/ipkernel.py#L19

@fabioz
Copy link

fabioz commented Oct 13, 2022

Note: I also created an issue ipython/ipykernel#1004 for ipykernel to do the right thing here.

@roblourens
Copy link
Member

Thanks. I suppose that we can also actually spawn the kernel with the proper environment. But it really seems like the proper thing is for ipykernel to set this flag. Any reason we haven't already brought it up with them? Does Jupyter Lab know about this or have a solution to this?

@fabioz
Copy link

fabioz commented Oct 13, 2022

Thanks. I suppose that we can also actually spawn the kernel with the proper environment. But it really seems like the proper thing is for ipykernel to set this flag. Any reason we haven't already brought it up with them? Does Jupyter Lab know about this or have a solution to this?

Well, I just brought it up in that ticket (ipython/ipykernel#1004) -- some ipython devs were on microsoft/debugpy#869, so I thought they were up to date on this, but maybe they didn't notice it.

@roblourens
Copy link
Member

Don pushed a change in #11682 to spawn the kernel with the environment variable set, and I confirmed that with that change, justMyCode can work perfectly. So we have that for local kernels, and we just have to keep on top of ipython/ipykernel#1004 as well. Thanks @fabioz and @DonJayamanne!

@roblourens
Copy link
Member

Hm, this is actually not working for the Interactive Window. When I start debugging, I always start at that same line in interactiveshell.py as above #8146 (comment) and can't hit a breakpoint in the cell code. I confirmed that the fix from yesterday is applying to the IW so I see Using PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING: True. Can't tell why the IW would be different from the notebook here, any idea @DonJayamanne?

@roblourens roblourens reopened this Oct 18, 2022
@roblourens
Copy link
Member

I figured out that this happens with the breakpoint() statement, we inject this into the first line of the code run from the IW, and every time this runs, it breaks in interactiveshell.py. Happens if that statement is anywhere in the cell, when the above environment var is set.

@fabioz do you expect breakpoint() to work correctly here?

@roblourens
Copy link
Member

We can probably get that working using a normal breakpoint. I'll open a new issue for that.

@fabioz
Copy link

fabioz commented Oct 25, 2022

I figured out that this happens with the breakpoint() statement, we inject this into the first line of the code run from the IW, and every time this runs, it breaks in interactiveshell.py. Happens if that statement is anywhere in the cell, when the above environment var is set.

@fabioz do you expect breakpoint() to work correctly here?

When you use breakpoint() what you're doing is asking the debugger to stop as soon as possible in the first opportunity, which isn't really what you want in this case (because the first opportunity is in the library code).

What you can do instead in this case is ask the debugger to stop just in user code.

The api for this is not as simple as just breakpoint() though.

If you know that you want to stop just at the main thread you can do something as:

import debugpy
debugpy.trace_this_thread()

from _pydevd_bundle.pydevd_api import PyDevdAPI
PyDevdAPI().stop_on_entry()

Note that this works just for the main thread (because stop_on_entry() makes the configuration for the main thread). We can create a better API for this use case though (maybe a keyword argument to breakpoint() like breakpoint(stop_on_user_code=True) would be good?)

@sachinruk
Copy link

To anyone else coming here looking for an answer

  1. You need the pre-release version of Jupyter extensions (as of 12th Nov 2022)

image

  1. Make sure you have the following in your local .vscode/settings.json file.
{
    "jupyter.debugJustMyCode": false,
}

This takes in the solution of the highest rated answer above and puts it into actual extension.

@nofreewill42
Copy link

holy jesus christ!
@sachinruk
You're my savior man!!

@nofreewill42
Copy link

This is lifechanging!

@nofreewill42
Copy link

Alright it's still not the best it could be, but still:
I don't have to create a new python script that I can debug as I wish just because I've started out with a jupyter notebook where doing so is not really possible.

@nofreewill42
Copy link

Okay, this is actually falling apart.... oh jeez I thought I'm saved but this is getting uglier and uglier : (

@nofreewill42
Copy link

OH YES!!!
This is actually working maaan!!
(just don't soft link your ~/venv to ./.venv in the project directory lol....)

I'm saaaaaaaaaaaaved !!!! :D
I feel like the cows that are released to freedom for the first time in their lives:
https://www.youtube.com/watch?v=jQQTmuOEPLU&ab_channel=TomPembertonFarmLife&t=296

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality notebook-debugging on-testplan
Projects
None yet