microsoft / python-language-server

Microsoft Language Server for Python
Apache License 2.0
912 stars 131 forks source link

Run Microsoft.Python.LanguageServer standalone encountered a null pointer problem #2139

Closed Soulghost closed 3 years ago

Soulghost commented 3 years ago

Hi, I am trying to using Microsoft.Python.LanguageServer outside of vscode. I spawn it with the same args, envs and options, and the jsonrpc connection is established, but the server raise some exceptions:

{jsonrpc: "2.0", id: 9,…}
error: {message: "Object reference not set to an instance of an object.", code: -32000,…}
code: -32000
data: "   at Microsoft.Python.LanguageServer.Documents.Document.GetAnalysisAsync(Uri uri, IServiceContainer services, Int32 msTimeout, CancellationToken cancellationToken) in E:\A\_work\1\s\src\LanguageServer\Impl\Documents\Document.cs:line 28↵   at Microsoft.Python.LanguageServer.Implementation.Server.Hover(TextDocumentPositionParams params, CancellationToken cancellationToken) in E:\A\_work\1\s\src\LanguageServer\Impl\Implementation\Server.Editor.cs:line 69↵   at Microsoft.Python.LanguageServer.Implementation.LanguageServer.Hover(JToken token, CancellationToken cancellationToken) in E:\A\_work\1\s\src\LanguageServer\Impl\LanguageServer.cs:line 174"
message: "Object reference not set to an instance of an object."

image

It makes hover, jump or auto completion not work, so what's wrong with my spawn?

This is my code to spawn the process (the same as vscode-languageclient/lib/node/main.js 's spawn):

const socketConnection = server.createConnection(reader, writer, () => socket.dispose());
const serverConnection = server.createServerProcess('Python', 
                                                    '/Users/soulghost/Desktop/git/vscode-python/languageServer.0.5.59/Microsoft.Python.LanguageServer', 
                                                    ['--debug'],
                                                    {
                                                        cwd: '/Users/soulghost/Desktop/20181218134850_detail_destroy',
                                                        stdio: 'pipe',
                                                        env: {
                                                            PATH: <PATH the same as vscode-languageclient/lib/node/main.js runtime PATH> 
                                                        }
                                                    });
server.forward(socketConnection, serverConnection, message => {
    if (rpc.isRequestMessage(message)) {
        if (message.method === lsp.InitializeRequest.type.method) {
            const initializeParams = message.params as lsp.InitializeParams;
            initializeParams.processId = process.pid;
        }
    }
    return message;
});

So can anyone tell me why some NPE exceptions occured, thanks.

Soulghost commented 3 years ago

Finally, I figured it out that I did not send the response of workspace/configuration to server, after that, everything seems to be work fine.

$ _INFO_:send message, content = {"jsonrpc":"2.0","method":"initialized","params":{}}
$ _INFO_:Received message: {"jsonrpc":"2.0","id":2,"method":"workspace/configuration","params":{"items":[{"scopeUri":"file:///Users/soulghost/Desktop/20181218134850_detail_destroy","section":"python"}]}}
$ _INFO_:send message, content = {"jsonrpc":"2.0","id":2,"result":[{"diagnostics":{"sourceMapsEnabled":false},"autoComplete":{"addBrackets":false,"extraPaths":[],"showAdvancedMembers":true,"typeshedPaths":[]},"autoUpdateLanguageServer":true,"logging":{"level":"error"},"experiments":{"enabled":true,"optInto":[],"optOutFrom":[]},"defaultInterpreterPath":"python","dataScience":{"allowImportFromNotebook":true,"widgetScriptSources":[],"askForLargeDataFrames":true,"askForKernelRestart":true,"enabled":true,"exportWithOutputEnabled":false,"jupyterLaunchTimeout":60000,"jupyterLaunchRetries":3,"jupyterServerURI":"local","jupyterCommandLineArguments":[],"notebookFileRoot":"${fileDirname}","searchForJupyter":true,"changeDirOnImportExport":false,"useDefaultConfigForJupyter":true,"jupyterInterruptTimeout":10000,"allowInput":true,"showCellInputCode":true,"collapseCellInputCodeByDefault":true,"maxOutputSize":400,"alwaysScrollOnNewCell":false,"enableScrollingForCellOutputs":true,"errorBackgroundColor":"#FFFFFF","sendSelectionToInteractiveWindow":false,"showJupyterVariableExplorer":true,"variableExplorerExclude":"module;function;builtin_function_or_method","codeRegularExpression":"^(#\\s*%%|#\\s*\\<codecell\\>|#\\s*In\\[\\d*?\\]|#\\s*In\\[ \\])","defaultCellMarker":"# %%","markdownRegularExpression":"^(#\\s*%%\\s*\\[markdown\\]|#\\s*\\<markdowncell\\>)","allowLiveShare":true,"ignoreVscodeTheme":false,"themeMatplotlibPlots":false,"liveShareConnectionTimeout":1000,"decorateCells":true,"enableCellCodeLens":true,"enableAutoMoveToNextCell":true,"autoPreviewNotebooksInInteractivePane":false,"useNotebookEditor":true,"allowUnauthorizedRemoteConnection":false,"enablePlotViewer":true,"gatherToScript":false,"gatherSpecPath":"","codeLenses":"python.datascience.runcell,  python.datascience.runallcellsabove, python.datascience.debugcell","debugCodeLenses":"python.datascience.debugcontinue, python.datascience.debugstop, python.datascience.debugstepover","debugpyDistPath":"","stopOnFirstLineWhileDebugging":true,"remoteDebuggerPort":-1,"disableJupyterAutoStart":false,"textOutputLimit":20000,"colorizeInputBox":true,"stopOnError":true,"addGotoCodeLenses":true,"variableQueries":[],"interactiveWindowMode":"multiple","magicCommandsAsComments":false,"runMagicCommands":"","runStartupCommands":"","debugJustMyCode":true,"alwaysTrustNotebooks":false},"disableInstallationCheck":false,"envFile":"${workspaceFolder}/.env","formatting":{"autopep8Args":[],"autopep8Path":"autopep8","provider":"autopep8","blackArgs":[],"blackPath":"black","yapfArgs":[],"yapfPath":"yapf"},"globalModuleInstallation":false,"jediMemoryLimit":0,"jediPath":"","languageServer":"Microsoft","analysis":{"diagnosticPublishDelay":1000,"errors":[],"warnings":[],"information":[],"disabled":[],"typeshedPaths":[],"cacheFolderPath":"","memory":{"keepLibraryAst":false},"logLevel":"Error","symbolsHierarchyDepthLimit":10},"linting":{"enabled":true,"flake8Args":[],"flake8CategorySeverity":{"E":"Error","F":"Error","W":"Warning"},"flake8Enabled":false,"flake8Path":"flake8","ignorePatterns":[".vscode/*.py","**/site-packages/**/*.py"],"lintOnSave":true,"maxNumberOfProblems":100,"banditArgs":[],"banditEnabled":false,"banditPath":"bandit","mypyArgs":["--ignore-missing-imports","--follow-imports=silent","--show-column-numbers"],"mypyCategorySeverity":{"error":"Error","note":"Information"},"mypyEnabled":false,"mypyPath":"mypy","pycodestyleArgs":[],"pycodestyleCategorySeverity":{"E":"Error","W":"Warning"},"pycodestyleEnabled":false,"pycodestylePath":"pycodestyle","prospectorArgs":[],"prospectorEnabled":false,"prospectorPath":"prospector","pydocstyleArgs":[],"pydocstyleEnabled":false,"pydocstylePath":"pydocstyle","pylamaArgs":[],"pylamaEnabled":false,"pylamaPath":"pylama","pylintArgs":[],"pylintCategorySeverity":{"convention":"Information","error":"Error","fatal":"Error","refactor":"Hint","warning":"Warning"},"pylintEnabled":true,"pylintPath":"pylint","pylintUseMinimalCheckers":true},"pythonPath":"/Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/bin/python","condaPath":"","pipenvPath":"pipenv","poetryPath":"poetry","sortImports":{"args":[],"path":""},"terminal":{"activateEnvironment":true,"executeInFileDir":false,"launchArgs":[],"activateEnvInCurrentTerminal":false},"testing":{"cwd":null,"debugPort":3000,"nosetestArgs":[],"nosetestsEnabled":false,"nosetestPath":"nosetests","promptToConfigure":true,"pytestArgs":[],"pytestEnabled":false,"pytestPath":"pytest","unittestArgs":["-v","-s",".","-p","*test*.py"],"unittestEnabled":false,"autoTestDiscoverOnSaveEnabled":true},"venvFolders":[],"venvPath":"","workspaceSymbols":{"ctagsPath":"ctags","enabled":false,"exclusionPatterns":["**/site-packages/**"],"rebuildOnFileSave":true,"rebuildOnStart":true,"tagFilePath":"${workspaceFolder}/.vscode/tags"},"insidersChannel":"off","showStartPage":true}]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"GetCurrentSearchPaths /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/bin/python "}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"Interpreter search paths:"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib_pypy"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib-python/2.7"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib-python/2.7/lib-tk"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib-python/2.7/plat-darwin"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib-python/2.7/plat-mac"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/lib-python/2.7/plat-mac/lib-scriptpackages"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"    /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/site-packages"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"User search paths:"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"Initializing for /Users/soulghost/.pyenv/versions/pypy2.7-6.0.0/bin/python"}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/languageServerStarted","params":[]}
$ _INFO_:send message, content = {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///Users/soulghost/Desktop/20181218134850_detail_destroy/src/test.py","languageId":"python","version":1,"text":"from half import Half\nimport sys\nimport os\nimport numpy as np\n\nh = Half()\nh.test()\n\ncount = h.count\nprint('the count is', h.count)\n\na = 100\nb = '200'\nc = 300\na = 300\nb = {\n    'aa': 'bbb'\n}\ndd = 500\nhaha = 500\n\neee = 10000\nadjasiod = 1203123123\n\nclass HeHe:\n    def __init__(self):\n        self.num = 0\n\n    def test(self):\n        self.num = 100\n\ndef a():\n    pass\n\nif __name__ == '__main__':\n    a()\n    pass\n\nhe = HeHe()\nhello = 200\n"}}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/beginProgress","params":[]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 7 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 25 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///Users/soulghost/Desktop/20181218134850_detail_destroy/src/test.py","diagnostics":[{"range":{"start":{"line":3,"character":7},"end":{"line":3,"character":12}},"severity":2,"code":"unresolved-import","source":"Python","message":"unresolved import 'numpy'"}]}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///Users/soulghost/Desktop/20181218134850_detail_destroy/src/half.py","diagnostics":[]}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 85 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 145 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 285 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 286 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 177 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 124 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 78 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/reportProgress","params":["Analyzing in background, 39 items left..."]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"python/endProgress","params":[]}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"telemetry/event","params":{"EventName":"python_language_server/analysis_complete","Properties":{"plsVersion":"0.5.59.0"},"Measurements":{"privateMB":0,"peakPagedMB":0,"workingMB":122.236928,"elapsedMs":6572.25,"moduleCount":243,"rdtCount":108}}}
$ _INFO_:send message, content = {"jsonrpc":"2.0","id":5,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///Users/soulghost/Desktop/20181218134850_detail_destroy/src/test.py"},"position":{"line":38,"character":7}}}
$ _INFO_:Received message: {"jsonrpc":"2.0","method":"telemetry/event","params":{"EventName":"python_language_server/rpc.request","Properties":{"method":"textDocument/hover","plsVersion":"0.5.59.0"},"Measurements":{"elapsedMs":23.4628}}}
$ _INFO_:Received message: {"jsonrpc":"2.0","id":5,"result":{"contents":{"kind":"markdown","value":"```\nclass HeHe\n```"},"range":{"start":{"line":38,"character":5},"end":{"line":38,"character":9}}}}

Ref

https://github.com/microsoft/python-language-server/issues/2004