You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary: If an LSP client sends text to the pylsp server with Windows-style line endings (\r\n), the pylsp-mypy plugin will report the wrong line numbers for some diagnostics.
Description:
The error occurs under the following conditions:
The LSP client sends text to the server with Windows-style line endings (\r\n).
The plugin has live mode enabled.
A change is made to the document, and the document is not saved afterwards.
When the last two conditions are met, the plugin writes the document text to a temp file, from which mypy reads the text and performs its analysis. The problem arises when Python writes the text to the temp file: by default, Python translates all instances of the newline character (\n) to the value of os.linesep when writing to a file. If the LSP server is running on Windows, os.linesep is equal to \r\n, so the line ending \r\n is translated to \r\r\n. Later, when mypy reads the file, it interprets the character sequence \r\r\n as two separate line endings; in effect, mypy is receiving a file with an extra line inserted after every line in the original text. mypy thus reports the wrong line numbers, which are propogated back to the client.
You will need an LSP client that sends line endings as \r\n instead of \n. Neovim v0.9.5 for Windows does this.
Install python-lsp-server latest (v1.12.0) and pylsp-mypy latest (v0.6.9).
Configure pylsp-mypy to enable live mode.
Run pylsp with the flag -vv for verbose logging.
Open a new Python source file. Save the file. Enter the following into the file but do not save it again.
passapass
If your editor shows the resulting diagnostic, you will see it appears on line 3 instead of line 2 of the source file. You can also look at the language server's logs to verify that pylsp-mypy is in fact reporting the wrong line numbers to the language server, which then propogates those line numbers to the client. Below is a screenshot from Neovim showing the diagnostic on the wrong line.
I've attached a log file for this example; see lines 144-151 for the text being passed to the server, and then lines 156, 158, 160, 161 for the response diagnostic with the incorrect line number. logs.txt
Proposed fix:
The plugin should write the text to the temporary file in binary mode. This will pass on the text to mypy exactly how it was sent by the client. mypy already reads the content of the file in binary mode, so there will be no line-ending translation at any point in the flow.
Tests should also be added to verify that the plugin can handle any of the valid line endings correctly.
The text was updated successfully, but these errors were encountered:
Summary: If an LSP client sends text to the pylsp server with Windows-style line endings (
\r\n
), the pylsp-mypy plugin will report the wrong line numbers for some diagnostics.Description:
The error occurs under the following conditions:
\r\n
).When the last two conditions are met, the plugin writes the document text to a temp file, from which mypy reads the text and performs its analysis. The problem arises when Python writes the text to the temp file: by default, Python translates all instances of the newline character (
\n
) to the value ofos.linesep
when writing to a file. If the LSP server is running on Windows,os.linesep
is equal to\r\n
, so the line ending\r\n
is translated to\r\r\n
. Later, when mypy reads the file, it interprets the character sequence\r\r\n
as two separate line endings; in effect, mypy is receiving a file with an extra line inserted after every line in the original text. mypy thus reports the wrong line numbers, which are propogated back to the client.The Langauge Server Protocol specifies that
\r
,\n
, and\r\n
are all valid line-ending sequences, so pylsp-mypy should be able to handle any of them.Reproduction steps:
\r\n
instead of\n
. Neovim v0.9.5 for Windows does this.-vv
for verbose logging.I've attached a log file for this example; see lines 144-151 for the text being passed to the server, and then lines 156, 158, 160, 161 for the response diagnostic with the incorrect line number.
logs.txt
Proposed fix:
The plugin should write the text to the temporary file in binary mode. This will pass on the text to mypy exactly how it was sent by the client. mypy already reads the content of the file in binary mode, so there will be no line-ending translation at any point in the flow.
Tests should also be added to verify that the plugin can handle any of the valid line endings correctly.
The text was updated successfully, but these errors were encountered: