-
Notifications
You must be signed in to change notification settings - Fork 769
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
Highlighting fails after switching between monorepo projects (when switching python interpreters) #5995
Comments
I think it has something to do with pylance not finishing loading with one module before switching to another. I found that if I switch back to the previous module and then wait for the highlights to paint that it sometimes unsticks the jupyter notebook. i.e. starting at notebook a in module A going to file b in module B, if you go back to a too quickly then it seems to get stuck. But if you navigate back to b and wait for a little bit, it sometimes allows a to paint as well. NOTE: this only occurs when you remain on b and have a open in a separate tab / split screen. However, if you navigate back to a it remains stuck. |
Another behavior I noticed was that if the highlighting is frozen and you change the project interpreter manually (i.e. cmd + shift + p -> select interpreter) it unfreezes the highlights. But then if you go back to the interpreter that's the same as one being used for the jupyter kernel it will then freeze again. |
Here's a video of me reproducing the problem, it took me a few tries but you'll see near the end that it freezes as described: |
Since Pylance owns semantic highlighting for Python, I've brought the main issue over to our repo and will resolve #5986 as a dupe. |
Here's the link to your log file which was the only piece of info that I think was specific to the other issue:
|
I've noticed that this seems to happen more often when the setting python.analysis.nodeExecutable is set to \usr\bin\node (or something other than empty). I started noticing this after I changed this setting due to memory overload warnings. note that it still happens, though. |
One work-around I have found is to disable then enable the jupyter extension without reloading vscode. In other words, disable without committing that change. I think this problem is related to this post as well: https://stackoverflow.com/questions/78643088/pylance-in-vscode-jupiter-notebooks-cannot-resolve-modules |
force-stopping the pylance processes also causes it to get unstuck. Is there a way to restart pylance every time the interpreter changes? And or can you add a command to restart all processes? It would make my life a lot easier :) |
No. It shouldn't be necessary to restart the process. Pylance will be notified of the config change.
That exists already actually -- "Python: Restart Language Server" |
I haven't been able to repro this. Every time I think it has repro'd, I wait a little bit and it recovers. I'm curious if the same would happen for you if you waited longer. I'm not saying that that's a good experience -- but if it does recover eventually that would be good to know. It would also be interesting to see what's happening in the "Python Language Server" log (in the Output pane) when you believe that this has repro'd. Is it actively doing anything? Is it still catching up on recent interpreter changes triggered by Python Envy? Changing the interepreter "every" time you change tabs can be expensive as it causes us to clear our type cache (we need to start from scratch on analysis), reindex your files, etc. So I'm not surprised that you see more issues in a real monorepo with more files. Have you tried opening your repo as a multiroot workspace rather than simply opening the root folder? You'd use "Add Folder to Workspace" to add each folder that has its own venv. That's a one time thing. You'd then save the layout as a Of course, that's assuming that you're not relying on Python Envy for other things. @heejaechang might have other ideas. |
I'll look into the By the way, I've noticed that when it freezes some pylance errors stop functioning correctly. Like it won't error on undefined variables until the processes are manually reset even in python files in different workspace roots and when highlighting is working for those files.
I don't see this command, but will try to find it as that would be helpful for me to quickly refresh / get highlighting to work. What other information / logs can I collect to you and where I can securely share it? |
I've looked into the workspaces that you mentioned. I'll keep messing around with it to try and make it work for my use-case, but I'll point out that python-envy is more intuitive and requires less setup. update: played around with it more, seems interesting, will let you know if I run into issues |
That's strange. You're looking in the Command Palette? Ctrl+Shift+P?
Here is the docs page on that: https://code.visualstudio.com/docs/editor/multi-root-workspaces
You can email me at
That's fair. We're aware that our monorepo support is a bit rough at the moment. |
Ah, one problem I'm seeing with the workspaces solution is that you can't refactor across project roots. When using python envy refactors propogate |
Yeah, it would have that limitation. So I guess that means that you have cross-root/cross-project imports? Out of curiosity, how do you currently handle those imports -- are the paths absolute or relative? Are you happy with whatever approach you're using at the moment? @erictraut, do you have any suggestions on how to handle a monorepo where the projects each have their own venv? Do we have a good way to handle that while still allowing refactoring (ex. rename) across projects? Would execution environments help here? |
I sent the logs @debonte I didn't include much of the logs for security reasons, but I captured the final parts where it eventually stopped outputting after claiming that Yep, I have cross-root/cross-project imports using poetry development paths. This affords me a similar workflow to typescript monorepos which is very nice. Basically, it lets me check the usage of some variable / function whatever across the entire monorepo and refactor it as well as if it were a single project. Currently I setup my project in the same way as the original codesandbox that I linked, I setup each project in the same structure as a typescript monorepo (with pnpm as well for added control) and make sure that poetry saves the dependencies in the local .venv. Then when I navigate between projects python envy changes the interpreter which allows me to navigate naturally through the project. I'm very happy with this workflow, the biggest issue being pylance being a bit delayed when switching and it using up a lot of CPU. But to me it's worth it because of the ease of refactoring and seeing where things are being used acros projects. p.s. for reference here are the logs after the problem occurred and I was navigating through the frozen notebook:
with python trace on:
|
To be clear btw poetry dev paths treats the other projects as packages. I'm the same way that you can import a pnpm package in Typescript |
These are the options I can think of:
|
Given the empty semantic tokens response, my best guess is that we're somehow no longer aware that the cells in that notebook exist. How we got into that state, I'm not sure. I'll try to repro this some more. |
@erictraut that's not an ideal solution because it causes conflicts when there are some projects that require heavy dependencies or have conflicts with others (torch being a good example) . Have you found this to be an issue as well? Using python envy + poetry dev paths allows me to have project configs for each project that are separate from each other in the same way you'd have a monorepo in typescript. Have you given that a try? |
We tried to keep versions of third-party libraries roughly in sync across the monorepo, so this wasn't an issue. In cases where the library version wasn't exactly in sync, we were typically able to use the later version and assume that the earlier version was backward compatible. In a few rare cases, we needed to create custom stubs.
I don't think an ideal solution is possible in this case. You're going to need to make some compromises. If your monorepo is using different venvs, then any modules that are used in two or more venvs are potentially going to be interpreted differently because of symbols they import from the different venvs. Even if pylance were to understand different venvs without using workspaces (something that's very unlikely because it would require a substantial redesign and rewrite of pylance and pyright), it would still be dangerous to perform refactoring actions across venv boundaries. Refactoring is based on static type information, and the venv affects the static analysis. That means a refactor action that is correct for one venv may not be for another. Most of the time this probably wouldn't matter, and the refactor would be safe, but the edge cases would be ugly. |
@erictraut just out of curiosity, how do you deal with submodules and dependencies that have dependency conflicts that cross major version updates? I see, would it also require a lot of work to make switching between the venv more efficient / fast? |
Are you finding this to be slow currently? Pyright alone (without the additional functionality in pylance) should enable very fast switching between venvs. Pylance may add overhead if it needs to reindex based on the new venv, but indexing is done in the background. |
Semantic highlighting is slow, and that's also on the background thread. But the real issue is that sometimes, after a venv change, semantic highlighting stops working entirely. I suspect that that's happening because somehow the active notebook's cells are not in the expected workspace. But I haven't been able to repro this, so I'm not positive. When this repros in the sample repo, the user is repeatedly and rapidly switching venvs by switching tabs with the Python Envy extension installed. Apparently when this repros in a real monorepo it doesn't require so much tab switching. Maybe one is enough? |
@debonte yes one is enough and I think your hypothesis makes sense. One thing I noticed is that the notebook begins to highlight if you switch to a different venv / out of that notebook. I can show you what I mean in a video if that'd help. It stops highlighting after you switch back to the notebook. |
@erictraut other than the issue with it freezing it's pretty responsive. My biggest complaint is that it causes the cpu usage to spike when switching venv and sometimes if I switch quickly it takes a while to catch up. Is it possible to cache the indexing for the venv so that it requires less indexing between switches? Also, if I'm not mistaken pylance seems to index fully for each venv change even if I switch before it finishes (e.g. say I switch from venv A to venv B and then back to A before B finishes indexing, it seems that pylance will finish indexing B despite this switch), but I may be mistaken there. |
Yeah, a video of that would be interesting. Here's an experiment that I'd be curious about:
|
@debonte I was having trouble reproducing the issue, but I might have found something. It seems that if I import tqdm it becomes a lot easier to reproduce the issue. I have updated the linked codesandbox with tqdm to make trying it easier for you. For reference here's a video of me reproducing it: https://www.loom.com/share/7c2ad9054cf042a0990c6f20118820a1 |
Good clue! I'm out of town this week, so i won't be able to look at this until next week. |
I've noticed switching sometimes stops highlighting regular python files as well as jupyter notebooks. |
@debonte it seems that this isn't available in jupyter notebook for some reason, i.e. in my vscode if a jupyter notebook is focused it doesn't show up, but it does if I have a python file selected. (I'm referring to the "restart language server" that we had talked about previously) |
Also this may be a separate issue, but it seems that when switching environments and trying to check for a funciton's references while indexing it sometimes kills the language server. The log outputs are as follows.
Sorry, this might be a different issue I think aside from reducing the weight of changing the env by caching indexes or something along those lines, it would be nice also if the server would restart in the event of a failure like the above. I currently have to reload vscode because it doesn't seem to recover from the heap out of memory error. note that doing "restart language server" does not seem to fix the problem, only a reload works. |
Thanks for your continued help on this. I'm now able to reproduce the issue on codesandbox and have confirmed that the problem is that we, for some reason, removed the target notebook from our internal view of the project. I'll continue investigating and let you know when I figure it out. Please open a separate issue for the crash. |
This issue has been fixed in prerelease version 2024.7.103, which we've just released. You can find the changelog here: CHANGELOG.md |
@heejaechang it seems that it did kind of fix the problem but it still happens if you switch enough and in addition it now affects standard python files as well as notebooks :( EDIT: it might be that my extension reverted to non-pre-release. I'll update if it still breaks. Edit 2: it seems that this issue is fixed but there are still other issues, I'll create a new issue for those. Thank you guys so much! |
Applies To
What happened?
Basically, I expect the highlighting on the Jupyter notebook to update when I switch from one project to another. However, when I switch from one project to another it fails to update the highlighting until I either reload the window or I restart the extension host. Note that normal python files highlight just fine.
To reproduce this see the following codesandbox
After you've loaded into the codesandbox, open it in vscode. Make sure that you have the following extensions:
open "third_project/another_one.ipynb" and select the appropriate .venv which is located at "third_project/.venv". Notice that things are highlighting.
open other_project/init.py
You can also get here by navigating to the definition of the "other_project" module. Notice that python envy changes the interpreter.
Now navigate back to the "third_project/another_one.ipynb". If you're "lucky" you'll see that when you scroll down and then back up (to force re-draw the highlihgts) that sometimes it freezes. When it freezes like this it's frozen indefinitely.
NOTE:
VS Code Version
1.90.0
Jupyter Extension Version
v2024.5.0
Jupyter logs
Coding Language and Runtime Version
python v3.8
Language Extension Version (if applicable)
Pylance v2024.6.1
Anaconda Version (if applicable)
No response
Running Jupyter locally or remotely?
Remote
The text was updated successfully, but these errors were encountered: