lsp-pyright icon indicating copy to clipboard operation
lsp-pyright copied to clipboard

Are we managed to get organize import and auto import working

Open f279801 opened this issue 4 years ago • 32 comments

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()

  1. I expect the auto import to work but it does not. Instead I got '"socket" is not defined'.
  2. 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

  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: []

f279801 avatar Aug 18 '20 19:08 f279801

auto-import works well in my env.

emacs

organize-imports doesn't work, and raises this message

LSP :: source.organizeImports action not available

seagle0128 avatar Aug 19 '20 15:08 seagle0128

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.

f279801 avatar Aug 19 '20 15:08 f279801

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.

seagle0128 avatar Aug 19 '20 16:08 seagle0128

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.

f279801 avatar Aug 19 '20 22:08 f279801

Ref: https://github.com/microsoft/pyright/issues/926#issuecomment-671073057

f279801 avatar Aug 19 '20 22:08 f279801

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.

seagle0128 avatar Aug 20 '20 07:08 seagle0128

Organize import in lsp protocol is code action. If the server supports it we will call it.

yyoncho avatar Aug 20 '20 17:08 yyoncho

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.

repelliuss avatar Aug 20 '20 17:08 repelliuss

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.

seagle0128 avatar Aug 20 '20 19:08 seagle0128

It works with vscode but doesn't with lsp-mode. Wired.

Can someone provide client/server log from vscode?

yyoncho avatar Aug 20 '20 19:08 yyoncho

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.

f279801 avatar Aug 21 '20 18:08 f279801

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 😭️

seagle0128 avatar Aug 22 '20 16:08 seagle0128

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.

yyoncho avatar Aug 22 '20 16:08 yyoncho

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.

image

seagle0128 avatar Aug 22 '20 17:08 seagle0128

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 avatar Aug 22 '20 17:08 yyoncho

@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?

seagle0128 avatar Aug 22 '20 19:08 seagle0128

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.

f279801 avatar Aug 24 '20 21:08 f279801

  • 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.

f279801 avatar Aug 25 '20 20:08 f279801

Anyone else still receiving LSP :: source.organizeImports action not available?

padraic-mcatee avatar May 19 '21 18:05 padraic-mcatee

@padraic-mcatee According to microsoft/pyright#967, it's as designed in pyright.

seagle0128 avatar May 20 '21 07:05 seagle0128

@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?

ardrabczyk avatar Jun 05 '21 20:06 ardrabczyk

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?

ardrabczyk avatar Jun 06 '21 17:06 ardrabczyk

@seagle0128 cannot we import after typing via something like code action? Screen Shot 2021-06-10 at 16 49 33

tshu-w avatar Jun 10 '21 08:06 tshu-w

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?

wztdream avatar Sep 20 '21 13:09 wztdream

@ardrabczyk I see that this works for me as well.

padraic-mcatee avatar Dec 09 '21 00:12 padraic-mcatee

@seagle0128, any comments on @ardrabczyk 's note above that even though pyright.organizeimports is supposed to work, it doesn't reflect in emacs?

indigoviolet avatar Apr 08 '22 19:04 indigoviolet

@indigoviolet What's the behavior of pyright extension of VSCode? Can you confirm if there are same?

seagle0128 avatar Apr 09 '22 17:04 seagle0128

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.

C-alcium avatar Jul 08 '22 10:07 C-alcium

If the behaviors are same, it seems like an issue of the server side.

seagle0128 avatar Jul 08 '22 11:07 seagle0128

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.

C-alcium avatar Jul 08 '22 13:07 C-alcium