python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.9k stars 197 forks source link

Rope autoimport doesn't work and cause all completion to not function #503

Open eyalk11 opened 9 months ago

eyalk11 commented 9 months ago

I couldn't get autoimport to work. And when I try to enabled rope_autoimport, suddenly nothing works. It simply doesn't suggest anything (python).

version: 1.9.0

python-lsp-jsonrpc 1.1.2 python-lsp-server 1.9.0

require('lspconfig').pylsp.setup{
    capabilities = capabilities,
    on_attach = on_attach,
    settings =
    {
        pylsp = {
            plugins =
            {
                pycodestyle = {
                    enabled = false,
                    ignore = {'E225','E231'},
                },
                pydocstyle = {
                    enabled= false
                },
                pylint = { enabled = false },
                --rope = {enabled = true },
                rope_autoimport = {enabled = true, {code_actions = {enabled = true}}},
                jedi_symbols = { enabled = true, all_scopes = true, include_import_symbols = true},

            }
        }
    }
}
  sources = cmp.config.sources(
{
    {
    name = "dictionary",
    keyword_length = 2,
    priority=0.1
    },
    { name = 'buffer', priority = 1 },

    { name = 'ultisnips', priority= 3 }, -- For vsnip users.
    { name = 'nvim_lsp' , priority = 100}
    -- { name = 'luasnip' }, -- For luasnip users.
    -- { name = 'ultisnips' }, -- For ultisnips users.
    -- { name = 'snippy' }, -- For snippy users.
})

When I did enabled it, I see it found some options But I couldn't see them in the menu.

2023-12-25 12:17:31,819 Jerusalem Standard Time - DEBUG - pylsp_jsonrpc.endpoint - Got result from synchronous request handler: {'isIncomplete': False, 'items': [{'label': 'nt', 'kind': 9, 'sortText': '[z00044', 'data': {'doc_uri': 'file:///C:/gitproj/Auto-GPT/autogpt/command_decorator.py'}, 'detail': '# Auto-Import\nimport nt', 'additionalTextEdits': [{'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 0, 'character': 0}}, 'newText': 'import nt\n'}]}, {'label': 'NEW_URL', 'kind': 6, 'sortText': '[z00070', 'data': {'doc_uri':  .... 
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - pylsp.plugins.rope_autoimport - autoimport: searching for word: os
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - diff parser start
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - line_lengths old: 67; new: 67
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - -> code[equal] old[1:31] new[1:31]
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - copy old[1:4] new[1:4]
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - parse_part from 5 to 7 (to 7 in part parser)
2023-12-25 12:10:09,508 Jerusalem Standard Time - DEBUG - parso.python.diff - parse_part from 8 to 8 (to 9 in part parser)
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - copy old[10:66] new[9:31]
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - -> code[replace] old[32:32] new[32:32]
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - parse_part from 32 to 32 (to 34 in part parser)
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - -> code[equal] old[33:67] new[33:67]
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - copy old[35:66] new[33:66]
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - parso.python.diff - diff parser end
2023-12-25 12:10:09,510 Jerusalem Standard Time - DEBUG - pylsp.plugins.rope_autoimport - [<Name full_name='__future__.annotations', description='instance annotations'>, <Name full_name='functools', description='module functools'>, <Name full_name='typing.TYPE_CHECKING', description='instance TYPE_CHECKING'>, <Name full_name='typing.Any', description='class Any'>, <Name full_name='typing.Callable', description='class Callable'>, <Name full_name='typing.Optional', description='class Optional'>, <Name full_name='typing.TypedDict', description='class TypedDict'>, <Name full_name='autogpt.core.planning.strategies.name_and_goals.NameAndGoals', description='class NameAndGoals'>, <Name full_name='autogpt.config.Config', description='class Config'>, <Name full_name='autogpt.models.command.Command', description='class Command'>, <Name full_name='autogpt.models.command.CommandParameter', description='class CommandParameter'>, <Name full_name='command_decorator.AUTO_GPT_COMMAND_IDENTIFIER', description='AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command"'>, <Name full_name='command_decorator.CommandParameterSpec', description='class CommandParameterSpec'>, <Name full_name='command_decorator.command', description='def command'>]
2023-12-25 12:10:10,019 Jerusalem Standard Time - DEBUG - pylsp.config.config -       pylsp_lint [hook]
          config: <pylsp.config.config.Config object at 0x00000227AA7006A0>
          workspace: <pylsp.workspace.Workspace object at 0x00000227AB2DB820>
          document: file:///C:/gitproj/Auto-GPT/autogpt/command_decorator.py
          is_saved: False

2023-12-25 12:10:10,023 Jerusalem Standard Time - DEBUG - pylsp_jsonrpc.endpoint - Sending request with id 1a3cf8b3-7575-4bc7-9942-db01eca38315: window/workDoneProgress/create {'token': 'd3b05aa0-eacd-484e-9e2d-f91e406c484b'}
2023-12-25 12:10:10,027 Jerusalem Standard Time - DEBUG - pylsp.config.config -       finish pylsp_completions --> [[{'label': 'os', 'kind': 9, 'sortText': '[z00063', 'data': {'doc_uri': 'file:///C:/gitproj/Auto-GPT/autogpt/command_decorator.py'}, 'detail': '# Auto-Import\nimport os', 'additionalTextEdits': [{'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 0, 'character': 0}}, 'newText': 'import os\n'}]}, {'label': 'OSPEED', 'kind': 6, 'sortText': '[z00092', 'data': {'doc_uri': 'file:///C:/gitproj/Auto-GPT/autogpt/command_decorator.py'}, 'detail': '# Auto-Import\nfrom tty import OSPEED', 'additionalTextEdits': [{'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 0, 'character': 0}}, 'newText': 'from tty import OSPEED\n'}]}, {'label': 'os', 'kind': 9, 'sortText': '[z00097', 'data': {'doc_uri': '......
2023-12-25 12:10:10,031 Jerusalem Standard Time - DEBUG - pylsp_jsonrpc.endpoint - Handling response from client {'jsonrpc': '2.0', 'result': None, 'id': '60d958d5-57a3-41cd-bd11-702dd12fd0e0'}
2023-12-25 12:10:10,031 Jerusalem Standard Time - DEBUG - pylsp_jsonrpc.endpoint - Received result for message 60d958d5-57a3-41cd-bd11-702dd12fd0e0: None

When I disable rope_autocomplete , it suddenly work (basic completion) .

Do you have an idea why? or direction? Thanks

eyalk11 commented 9 months ago

lsprel.log Since 12:40 until ~13:00 It didn't work at all. When I disabled the plugin (rope-autoimport), it suddenly worked (normal completion).

eyalk11 commented 9 months ago

It also seem that it doesn't send the full text I type often.

2023-12-25 12:44:12,290 Jerusalem Standard Time - DEBUG - pylsp.plugins.rope_autoimport - autoimport: searching for word: c

Though hard to be sure.

tkrabel commented 9 months ago

It's hard for me to understand what the issue here is. Can you Answer the following questions for clarity?

  1. What where you trying to do? Can you give an example?
  2. What did you expect?
  3. What did you see instead?
eyalk11 commented 9 months ago
  1. I am trying to get auto-import work. It doesn't work on any python file. When I enable auto-import plugin, I get no suggestions at all for anything. That is if I include this line:
                rope_autoimport = {enabled = true, {code_actions = {enabled = true}}}

    2 and 3: For example, I type DataFrame, and get no suggestions. (Expect to see from pandas improt DataFrame). I type os and get no suggestions.

In fact, I get no suggestions at all even for same file variables. Unless I disable auto-import. I tried 3 different installations of pylsp with different python versions.

What do you suggest to do?

The log seems to suggest some valid suggestions even as I don't see them.

PS

does it matter that python version that runs pylsp and python3_host are different?

tkrabel commented 9 months ago

Can you check again with the following setting:

                rope_autoimport = {enabled = true, {completions = {enabled = true}}}

Does that work?

eyalk11 commented 9 months ago

No, the entire plugin doesn't work in this case ( except diagnostics).

Maybe it is related?

I allowed both completion and code actions and for a sec it displayed the options.

2024-01-05 08:05:01,829 Jerusalem Standard Time - DEBUG - pylsp.plugins.rope_autoimport - autoimport: searching for word: DataFrame

I typed it at least 10 times, so I wonder why it only appeared once. Then I was able to see code actions for a short while.

Out of desperation I improved the implantation of nvim-pymode to use sqllite db , and probably will include your great scoring method. But your implementation is better. I don't understand how it doesn't work no matter what I do.

tkrabel commented 9 months ago

Sorry to hear it still doesn’t work. Can you record your screen of the behavior? Everything just works fine on my end …

eyalk11 commented 9 months ago

https://drive.google.com/file/d/1XUNISIc83vcxI0R4CsR-NonNrJzvSP0A/view?usp=sharing

image

(Trying to add project root doesn't solve it)

tkrabel-db commented 8 months ago

@eyalk11 from the video, it seems the LS is broken. E.g. in 0:23, on line 18, it says undefined name 'f', which doesn't make sense to me given the content of the line.

Do you get any LSP errors when doing that again?

eyalk11 commented 8 months ago

I think it is related to the autosave , so it remembered a former expression. As you could see, in the second half, it worked really fine. I added the log . No errors. try.log

awalvie commented 6 months ago

I have the same issue. When I enable rope_autoimport completions stop working.

smartinellimarco commented 6 months ago

In my case, completions work but I dont see any coming for autoimports. Nor the code actions. I inspected the lsp settings in Mason and saw this:

        → pylsp.plugins.rope_autoimport.code_actions.enabled        default: true
        → pylsp.plugins.rope_autoimport.completions.enabled         default: true
        → pylsp.plugins.rope_autoimport.enabled                     default: false

with the following config:

    pylsp = {
      -- FIXME: does not work
      plugins = {
        rope_autoimport = { enabled = true, { completions = { enabled = true } } },
      },
    },

also, from LSP logs: [ERROR][2024-03-18 14:22:31] .../vim/lsp/rpc.lua:734 "rpc" "/Users/marco/.local/share/nvim/mason/bin/pylsp" "stderr" "2024-03-18 14:22:31,650 -03 - WARNING - pylsp_jsonrpc.endpoint - Received cancel notification for unknown message id 84\n"

HungryJoe commented 1 day ago

I'm having the exact same issue described above. I don't see a bug-report template for this project, so I'll try and give all the context I think is relevant.

Context

Relevant Config: config-with-rope.txt (See this repo for my full Helix config)

Here's a sample log file so that you can see that all plugins load successfully.

Steps to reproduce

  1. Open an existing Python file
  2. Type any symbol
  3. Type a symbol from an installed library that hasn't been imported

Expectation

Step (2) would cause an auto-complete window to show up. Step (3) would cause an auto-complete window to show up that would auto-import the symbol on confirming it.

What happens instead

No auto-complete window shows up.

Misc Notes

When rope_autoimport and rope_completion are disabled, auto-completion works great, except that there's no auto-import and symbols from installed libraries that haven't been imported into the current file don't show up in the completion list. See config.txt for a sample configuration.

Also, disabling pylsp_rope doesn't help.