Skip to content

line-ending detection doesn't work on Windows and --help explain how to use --line-ending #2453

@idbrii

Description

@idbrii

When passing a file with Unix-style LF line endings to isort, it always converts it to CRLF on Windows.

C:\> isort --version

                 _                 _
                (_) ___  ___  _ __| |_
                | |/ _/ / _ \/ '__  _/
                | |\__ \/\_\/| |  | |_
                |_|\___/\___/\_/   \_/

      isort your imports, so you don't have to.

                    VERSION 7.0.0


C:\> cat C:\scratch\aletest.py | file -
/dev/stdin: Python script, ASCII text executable, with very long lines (965)

C:\> isort - < C:\scratch\aletest.py | file -                   
/dev/stdin: Python script, ASCII text executable, with CRLF line terminators

C:\> file --version
file-5.45
magic file from /usr/share/misc/magic

Same results with a minimal file created from scratch:

C:\> echo import re > testfile.py
C:\> echo import os >> testfile.py
C:\> dos2unix testfile.py
C:\> file testfile.py
testfile.py: ASCII text
C:\> isort - < C:\scratch\testfile.py | file -
/dev/stdin: ASCII text, with CRLF line terminators

On Linux (WSL Ubuntu using bash) it works:

$ isort - < /mnt/c/scratch/aletest.py | file -
/dev/stdin: Python script, ASCII text executable
$ isort --version | grep VERSION
                    VERSION 5.13.2

(I originally encountered the issue on v5 under Windows.)

So auto line ending detection is not working. So I tried to force it to use \n line endings. The docs say:

Line Ending

Forces line endings to the specified value. If not set, values will be guessed per-file.

Type: String
Default:
Config default:
Python & Config File Name: line_ending
CLI Flags:

--le
--line-ending

However, what values are valid? CRLF, \n, unix? There are many ways line endings are described, so it's not entirely clear which ones isort expects.

I think the correct value is the literal character at the end of the line: \n. But how do you specify that? I tried what seemed obvious but it didn't work and always uses CRLF:

C:\> isort --line-ending \n - < C:\scratch\testfile.py 
import os\nimport re\n
C:\> isort --line-ending \\n - < C:\scratch\testfile.py
import os\\nimport re\\n
C:\> isort --line-ending "\n" - < C:\scratch\testfile.py
import os\nimport re\n
C:\> isort --line-ending "\\n" - < C:\scratch\testfile.py
import os\\nimport re\\n

So all of those options seem wrong.

On Win11 in cmd.exe. I'm using file from my git install (via scoop). I got isort via pip on Python 3.11.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions