Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

System.InvalidCastException: Unable to cast object of type 'Microsoft.PythonTools.Analysis.Values.ModuleInfo' to type 'Microsoft.PythonTools.Analysis.Values.BuiltinModule'. #744

Closed
qci-amos opened this issue Mar 13, 2019 · 15 comments

Comments

@qci-amos
Copy link

I'm getting this exception a lot:

Starting Microsoft Python language server.
Microsoft Python Language Server version 0.1.80.0
Initializing for C:\Users\me\AppData\Local\conda\conda\envs\my_env\python.exe
Reloading modules... 

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.PythonTools.Analysis.Values.ModuleInfo' to type 'Microsoft.PythonTools.Analysis.Values.BuiltinModule'.
   at Microsoft.PythonTools.Analysis.PythonAnalyzer.LoadKnownTypesAsync(CancellationToken token)
   at Microsoft.PythonTools.Analysis.PythonAnalyzer.ReloadModulesAsync(CancellationToken token)
   at Microsoft.Python.LanguageServer.Implementation.Server.ReloadModulesQueueItem.Analyze(CancellationToken cancel)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Python.LanguageServer.Implementation.Server.ReloadModulesAsync(CancellationToken token)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
[Info  - 7:49:42 AM] Connection to server got closed. Server will restart.
Microsoft Python Language Server version 0.1.80.0
Initializing for C:\Users\me\AppData\Local\conda\conda\envs\my_env\python.exe
Reloading modules... 

And just now, I got a pop-up informing me

The Python Tools server crashed 5 times in the last 3 minutes. The server will not be restarted.
Source: Python (Extension).

I don't see anybody else reporting this, so perhaps I'm doing something atypical. A bit of info on my setup: Although I'm editing my codebase in VS Code, I'm executing my code in Windows Subsystem Linux (Ubuntu) (the shell for which is in cmder). I've created an Anaconda environment for my software in both Windows and in WSL. The python version linked to my project in VS Code is Python 3.6.8. I've lots of python extensions installed including numpy, scipy, matplotlib, etc. The problem does seem to occur most often when my code is actually running in WSL while I move my mouse around in VS Code.

I think this issue started appearing a couple weeks(?) ago. I'm not sure if it's causing other problems though, although I do notice the Python extension sometimes uses a lot more memory than I'd expect (>8gb).

@MikhailArkhipov
Copy link

MikhailArkhipov commented Mar 13, 2019

Try switching to 0.2+ available on beta channel.

"python.analysis.downloadChannel": "beta"

In 0.2 this code no longer exists.

@qci-amos
Copy link
Author

Confirmed, the exception goes away, thanks! I do get a lot of these lines while my code is running in WSL:

[Info  - 1:26:39 PM] Reloading modules... 
[Info  - 1:26:39 PM] done.
[Info  - 1:26:39 PM] Analysis restarted.

and intellisense is sluggish, but not a big deal for me.

Is it known when this will become stable / is there a published roadmap somewhere?

@jakebailey
Copy link
Member

Interesting, can you provide an overview of your setup? What is running in WSL, and where does your code live? Are you running VS Code out of WSL or something else?

We use ZenHub to track progress, which either uses a browser extension or https://app.zenhub.com. You can also look at the milestones, but they're tracking specific timeframes and not releases.

@MikhailArkhipov
Copy link

This means something is changing in the paths we monitor. Monitor watches for changes in libraries so we can properly react on pip install. We consider paths received from Python (sys.path) for watching. Frequent reloading means some of these paths may actually be yours so each time file changes LS thinks something was installed or removed.

@jakebailey is working on #720 which is related. We may want to handle file.exclude perhaps so you could specify where NOT to watch.

@qci-amos
Copy link
Author

My repo is in /mnt/c/Users/me/Documents/my_repo so that it can be simultaneously accessed by VS Code and by WSL.
I'm just running my code as a command line tool. It's a simulation which generates files in the cwd (e.g., a log file) as it goes, and maybe the problem is that my cwd is on sys.path. I confirmed that if I work in a different directory, I don't get those messages in the Python server's OUTPUT tab.

@jakebailey
Copy link
Member

That makes sense given how we watch the search path. Maybe the matcher needs to be modified to only watch for **/*.py* (but I haven't looked).

@MikhailArkhipov
Copy link

There is files.exclude setting in VSC we should be able to leverage. Then the work folder in this case (or specific patterns) can be excluded. Also, I actually would like a setting to turn off the watcher completely.

@jakebailey
Copy link
Member

I'm still working out the details, but we can't really use it like that because those patterns include site-packages, which we still have to watch. Currently the only real use of the exclude patterns is for indexing.

(Technically, the extension is the one adding those on, so we could coordinate and upgrade that when we push 0.2 stable, but defining what excludeFiles is supposed to mean now that the LS doesn't just add everything is important to try to do first.)

@MikhailArkhipov
Copy link

In any case, it would be trivial to add python.analysis.watchFiles and handle it. The extension then could add explicit command to tell LS to reload modules - say, via new python/reloadModules.

@jakebailey
Copy link
Member

I'm not really sure what that would solve; We do the watching for package installs/uninstalls, which the extension has no idea about, and we're changing it such that the extension never really has to know any of the search paths at all. How would the extension know when to send it? If it's the user that needs to do it, then that's a regression from the current behavior of being able to just install something and have the analysis automatically refresh (as opposed to them needing to explicitly set some array of paths they may not know or be accurate when switching around virtualenvs).

@MikhailArkhipov
Copy link

I mean in case watching goes in the user way, they can turn it off and reload modules manually via the command when they install or uninstall something.

@jakebailey
Copy link
Member

Got it; I thought you meant python.analysis.watchFiles was a list of patterns, not a boolean.

@qci-amos
Copy link
Author

Having it only watch python files makes sense, and what I'd assume it to be doing anyway. Beyond that, personally, I wouldn't mess with a setting to fine tune watching behavior. The only reason I came looking for help after ignoring this for so long was because the exceptions were becoming unbearable.

@MikhailArkhipov
Copy link

It actually watches installation of packages which often consist of other files like lib, dylib, so, pyd etc. However, they usually contain some py at least.

@MikhailArkhipov
Copy link

#765
Closing this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants