Search code examples
visual-studioline-endings

Inconsistent line endings warning when line endings are consistent


I was opening an auto-generated file in Visual Studio to inspect it, and Visual Studio tells me the line-endings are inconsistent. I know about the difference between LF and CRLF, so I thought I would try making a small change to the generation code to use the proper newline depending on the platform.

I tell Visual Studio not to change it, and I close the file. I open it with Python and take a look at each of the lines with this snippet of code.

with open(filename, 'r') as f:
    # uses %r so it prints the non-formatted string (so I can see \r and \n)
    print '\n'.join(('%r' % x for x in f.xreadlines()))

I take a look at the output, and every line (except for the last) ends with a '\r\n'. The last line has no newline, so it contains only the text.

I also open the file with Emacs and it doesn't auto-detect to use DOS mode, and shows me the ^M character on every line.

Why are the line endings of the file "inconsistent" when I can see that every line is using the same line ending?


Solution

  • (answering my own question, but maybe it will help others in the future)

    The line endings weren't "inconsistent", but they also weren't valid. With a little look at hexdump, I found in the file the following at the point where a CRLF should be.

    0d 0d 0a
    

    When the file was written out, it must have written the \r character twice or potentially, the literal \r\n was used and whatever wrote out the file change the \n character into a \r\n automatically.