lsp-pyright
lsp-pyright copied to clipboard
Are we managed to get organize import and auto import working
Just a question on if we are able to get organize import and auto import working?
If we are then would you mind to give me a hint.
s = socket.socket()
- I expect the auto import to work but it does not. Instead I got '"socket" is not defined'.
- Even when I do it with
lsp-organize-imports
, it also does not help.LSP :: source.organizeImports action not available
[-]/home/xtaidin/workspace/dev/lsp-pyright -[-]pyright:31970 |-[-]Buffers | -[+]test.py -[-]Capabilities |-[X]callHierarchyProvider: t |-[-]executeCommandProvider: | |-[X]workDoneProgress: t | -[X]commands: [] |-[-]codeActionProvider: | |-[X]workDoneProgress: t | -[X]codeActionKinds: [quickfix source.organizeImports] |-[-]signatureHelpProvider: | |-[X]workDoneProgress: t | -[X]triggerCharacters: [( , )] |-[-]completionProvider: | |-[X]workDoneProgress: t | |-[X]resolveProvider: t | -[X]triggerCharacters: [. [] |-[-]renameProvider: | -[X]workDoneProgress: t |-[-]documentHighlightProvider: | -[X]workDoneProgress: t |-[-]hoverProvider: | -[X]workDoneProgress: t |-[-]workspaceSymbolProvider: | -[X]workDoneProgress: t |-[-]documentSymbolProvider: | -[X]workDoneProgress: t |-[-]referencesProvider: | -[X]workDoneProgress: t |-[-]definitionProvider: | -[X]workDoneProgress: t -[X]textDocumentSync: 1
- Maybe it's a server problem?
[Trace - 09:33:23 ] Sending request 'textDocument/codeAction - (259)'. Params: { "textDocument": { "uri": "file:///home/xtaidin/workspace/dev/lsp-pyright/testpython/test.py" }, "range": { "start": { "line": 1, "character": 11 }, "end": { "line": 1, "character": 11 } }, "context": { "diagnostics": [ { "source": "Pyright (reportUndefinedVariable)", "severity": 1, "message": ""socket" is not defined", "range": { "end": { "character": 16, "line": 1 }, "start": { "character": 10, "line": 1 } } } ], "only": [ "source.organizeImports" ] } }
[Trace - 09:33:23 ] Received response 'textDocument/codeAction - (259)' in 2ms. Result: []
auto-import works well in my env.
organize-imports doesn't work, and raises this message
LSP :: source.organizeImports action not available
Great to hear that. Do you have any addtional setup like configuration file or similar thing like that? Besides, with version of pyright you are using, I'm using pyright 1.1.63.
Would it be possible if you paste your io log as well. I'm curious about what the server replied to us.
Great to hear that. Do you have any addtional setup like configuration file or similar thing like that? Besides, with version of pyright you are using, I'm using pyright 1.1.63.
Would it be possible if you paste your io log as well. I'm curious about what the server replied to us.
No additional settings. I am using pyright 1.1.64.
I think I know what's the problem. If the module name or keyword was not input in the buffer, it won't be listed in the completion list. The issue needs lsp-mode core to take a look.
I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server. I get this to work for some use cases now, below are some observations:
- A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
- The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
- organizeImports seems to be removed now after the commit 2d0e3eb.
Ref: https://github.com/microsoft/pyright/issues/926#issuecomment-671073057
I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server. I get this to work for some use cases now, below are some observations:
- A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
- The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
- organizeImports seems to be removed now after the commit 2d0e3eb.
@yyoncho can u pls take a look at the issue about auto import and organize imports?
@f279801 The commit 2d0e3eb seems just removing organize-imports on saving. I tried in VSCode, the command works manually.
Organize import in lsp protocol is code action. If the server supports it we will call it.
I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server. I get this to work for some use cases now, below are some observations:
- A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
- The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
- organizeImports seems to be removed now after the commit 2d0e3eb.
I'm having the same behavior and the workaround works for me. Also, organize imports doesn't work for me either. It returns the same LSP :: source.organizeImports action not available
message.
Organize import in lsp protocol is code action. If the server supports it we will call it.
It works with vscode but doesn't with lsp-mode. Wired.
It works with vscode but doesn't with lsp-mode. Wired.
Can someone provide client/server log from vscode?
It works with vscode but doesn't with lsp-mode. Wired.
Can someone provide client/server log from vscode?
@seagle0128 would you mind to help with this request? I don't use vscode. I still suspect that the server somehow manage to have a different result during it import resolution phase.
It works with vscode but doesn't with lsp-mode. Wired.
Can someone provide client/server log from vscode?
@seagle0128 would you mind to help with this request? I don't use vscode. I still suspect that the server somehow manage to have a different result during it import resolution phase.
I tried a several times but failed to get the verbose logs in vscode 😭️
Can someone point me to vscode client? Seems like the server has code action pyright.organizeimports
but I am not sure if it is invoked explicitly or it is returned as code action.
Can someone point me to vscode client? Seems like the server has code action
pyright.organizeimports
but I am not sure if it is invoked explicitly or it is returned as code action.
C-S-P
to select Pyright: Organize Imports
. If choosing Organize Imports
, the same error message appears: "no organize imports action available". I think another action is implemented in pyright extension.
So, generally it should work either via this:
(lsp-make-interactive-code-action pyright-organize-imports "pyright.organizeimports")
Or by doing something like:
(lsp--send-execute-command "pyright.organizeimports" [])
@yyoncho I supposed it should work, but still got error: LSP :: pyright.organizeimports action not available
. The command exists in pyright indeed. Please refer to https://github.com/microsoft/pyright/blob/1d56d6765f3cb7f579081d5b3c363a3df7d83337/server/src/commands/commands.ts and https://github.com/microsoft/pyright/blob/fbe2eb4da7d4fcfbce0cd6ef3c5bb1cb4f10df87/client/package.json.
Please advise?
I'm not sure if this is lsp-mode core problem or not because based on the io log, the suggestion was not sent by the server. I get this to work for some use cases now, below are some observations:
- A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
- The auto imported could only be done as part of completion action, i.e: if user type the whole module name without completion, the auto import will not happen and 'is not defined' problem will be reported. This can be fixed by navigation into the problematic module and trigger the auto completion action.
- organizeImports seems to be removed now after the commit 2d0e3eb.
I'm having the same behavior and the workaround works for me. Also, organize imports doesn't work for me either. It returns the same
LSP :: source.organizeImports action not available
message.
I suspect that the first problem is actually an expected behavior and opened a question to pyright here: https://github.com/microsoft/pyright/issues/967
Let's wait for their answer.
- A module must be imported manually once in the whole workspace before it can appear in the suggestion list (except the builtin one).
I suspect that the first problem is actually an expected behavior and opened a question to pyright here: microsoft/pyright#967
Let's wait for their answer.
So it's confirmed that it's an expected behavior.
Anyone else still receiving LSP :: source.organizeImports action not available
?
@padraic-mcatee According to microsoft/pyright#967, it's as designed in pyright.
@seagle0128 I also get LSP :: pyright.organizeimports action not available
and (lsp--send-execute-command "pyright.organizeimports" [])
does nothing for me. Have you figured out how to use it?
pyright.organizeimports command has be invoked like that: (lsp-send-execute-command "pyright.organizeimports" ["file:///home/ja/python/test.py"])
(the file:// parameter is obligatory).
I checked that this._generateSortedImportText(sortedStatements)
in pyright/packages/pyright-internal/src/languageService/importSorter.ts contains a properly sorted list of import statements but the changes are not reflected in Emacs buffer. I guess this is because Emacs does not listen for the reply returned by server or cannot parse it?
@seagle0128 cannot we import after typing via something like code action?
Since pyright will not support code action, if we still use pyright, is there any other package we can use for code action? For example type np
auto add import numpy as np
in the file?
@ardrabczyk I see that this works for me as well.
@seagle0128, any comments on @ardrabczyk 's note above that even though pyright.organizeimports
is supposed to work, it doesn't reflect in emacs?
@indigoviolet What's the behavior of pyright extension of VSCode? Can you confirm if there are same?
Seems like to me that it behaves exactly the same way in VsCode, and the only way to get imports in a sane way is by adding an import alias or importing a module directly.
e.g.
Works
import django.urls as urls
urls.include(... # this will autocomplete
Does not work
from django.urls import include
include( ... # This will not, and it will also complain that it is an unknown symbol)
Edit: I also just noticed that if you have something like this:
include( ... # This will not, and it will also complain that it is an unknown symbol)
and you delete the include
and retype it, it can and does suggest the correct auto-import. But it is not smart enough to see that include can / should be imported from django.urls. What's going on there, I'm not sure, but it is quite frustrating I have to say.
If the behaviors are same, it seems like an issue of the server side.
That seems to be the case to me - I think the only way to fix this is to get the pyright server to fix it.