Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flake8 can error out when deleting lines #438

Closed
krassowski opened this issue Sep 10, 2023 · 2 comments · Fixed by #441
Closed

flake8 can error out when deleting lines #438

krassowski opened this issue Sep 10, 2023 · 2 comments · Fixed by #441
Labels
bug Something isn't working
Milestone

Comments

@krassowski
Copy link
Contributor

In 1.8.0 (did not check earlier versions yet) when lines are deleted while flake8 lint is running, and the diagnostic would end out of range of current document, flake8 can error out:

Traceback (most recent call last):
  File "site-packages/pylsp/config/config.py", line 40, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pluggy/_manager.py", line 418, in traced_hookexec
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pluggy/_result.py", line 108, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "site-packages/pluggy/_result.py", line 70, in from_call
    result = func()
             ^^^^^^
  File "site-packages/pluggy/_manager.py", line 415, in <lambda>
    lambda: oldcall(hook_name, hook_impls, caller_kwargs, firstresult)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pluggy/_callers.py", line 116, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "site-packages/pluggy/_callers.py", line 80, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pylsp/plugins/flake8_lint.py", line 83, in pylsp_lint
    return parse_stdout(document, output)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pylsp/plugins/flake8_lint.py", line 215, in parse_stdout
    "character": len(document.lines[line]),
                     ~~~~~~~~~~~~~~^^^^^^
IndexError: list index out of range

The relevant line is:

"character": len(document.lines[line]),

To reproduce create a python file like:




variable

This will generate "F821 undefined name 'variable'". Then proceed to removing the empty lines and observe the error. Note:

  • it does not happen every time, you may need more empty lines to delete and vary how quickly you delete them
  • pyflakes works well in this case.
@krassowski krassowski added the bug Something isn't working label Sep 10, 2023
@krassowski krassowski changed the title flake8 can error out when deeeting lines flake8 can error out when deleeting lines Sep 10, 2023
@krassowski krassowski changed the title flake8 can error out when deleeting lines flake8 can error out when deleting lines Sep 10, 2023
@ccordoba12 ccordoba12 added this to the v1.8.1 milestone Sep 10, 2023
@ccordoba12
Copy link
Member

It seems there is a mismatch between the state of the document (reflected in document.lines) and the flake8 output. So when document.lines is accessed in the line you referenced, the actual lines are different.

A possible solution could be to pass document.lines to parse_output, instead of document, so that the latter method is called with the current document state.

Could you try that to see if it fixes the problem? Thanks!

@krassowski
Copy link
Contributor Author

krassowski commented Sep 10, 2023

It does. In particular, it seems that accessing document.lines in a loop is a bad idea as it reads can read from disk at each access (if the private _source is None):

@property
@lock
def lines(self):
return self.source.splitlines(True)
@property
@lock
def source(self):
if self._source is None:
with io.open(self.path, "r", encoding="utf-8") as f:
return f.read()
return self._source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants