python-rope / pylsp-rope

Extended refactoring capabilities for python-lsp-server using Rope
MIT License
118 stars 5 forks source link

code actions fail if `pyproject.toml` has no `tool` section #9

Closed MrGreenTea closed 2 years ago

MrGreenTea commented 2 years ago

Description

I wanted to use code actions by calling :lua vim.lsp.buf.code_action() but get a message that there are no code actions available. When the pyproject.toml file exists and has sections, but no [tool] section it seems pylsp-rope runs in to an exception. If any other sections exist there is a KeyError('tool'). If the file is empty there is an AssertionError. Both seem to originate in pytoolconfig.

It works fine when I either remove this pyproject.toml file or just add an empty [tool] section like this:

[tool]

Details

Output of :LspLog in neovim

[ERROR][2022-07-29 15:34:51] .../vim/lsp/rpc.lua:420    "rpc"   "pylsp" "stderr"        "2022-07-29 15:34:51,827 CEST - WARNING
     - pylsp.config.config - Failed to load hook pylsp_code_actions: 'tool'\n"

Reproduction

Here is a parametrized test that will reproduce the error:

@pytest.mark.parametrize("content", ["", "[example]\n", "[tool]\n"])
def test_pyproject_toml_no_tool_section(
    tmpdir, config, document, workspace, code_action_context, content
):
    pathlib.Path(workspace.root_path, "pyproject.toml").write_text(content)

    response = plugin.pylsp_code_actions(
        config=config,
        workspace=workspace,
        document=document,
        range=Range((0, 0), (0, 0)),
        context=code_action_context,
    )
MrGreenTea commented 2 years ago

I found this related report in pytoolconfig: https://github.com/bageljrkhanofemus/pytoolconfig/issues/5 and https://github.com/bageljrkhanofemus/pytoolconfig/issues/4

lieryan commented 2 years ago

Thanks for writing this bug report @MrGreenTea.

fyi: @bageljrkhanofemus

bagel897 commented 2 years ago

Ye I saw the issues, I'll probably tackle it tomorrow and release. If someone wants to PR it I can look at it too.

MrGreenTea commented 2 years ago

@bageljrkhanofemus I started on a fix yesterday but then haven't finished. I'll try to put up a PR today 👍

bagel897 commented 2 years ago

Fixed in 1.2.2. Lmk if it isn't working.

lieryan commented 2 years ago

@bageljrkhanofemus thanks, confirmed that upgrading to 1.2.2 fixed the issue