Skip to content

fix: respect dos line endings #691

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Gatsik
Copy link

@Gatsik Gatsik commented Apr 18, 2025

I encountered a very annoying issue with isort on Windows -- formatter always added unnecessary line after imports.

For example:

-import sys
 import os
+import sys
+

or

-import sys
 import os
+import sys


 def main() -> int:
+
     return 0

(added empty lines are redundant)

It looks like runner doesn't properly handle final \r on processing stdout:

[TRACE] Applying text edits: { {
              newText = "os\r\nimport sys\r\n\r\n\r\ndef main() -> int:\r",
              range = {
               ...
              }
            } }

I came up with 2 solutions: the first one is this PR and the second one is simple:

--- a/lua/conform/runner.lua
+++ b/lua/conform/runner.lua
@@ -153,6 +153,7 @@ local function create_text_edit(
     table.insert(replacement, "")
   end
-  local new_text = table.concat(replacement, "\n")
+  local new_text = table.concat(replacement, "\n"):gsub("\r$", "")
 
   return {
     newText = new_text,

I don't know which one is fine (maybe neither!), but the more complex one ended up as a PR because it edits multiple files

@github-actions github-actions bot requested a review from stevearc April 18, 2025 19:05
@Gatsik Gatsik force-pushed the fix/line-endings branch from 9515f0f to 594bd21 Compare April 18, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant