Sunday, October 18, 2015

Stop Visual Studio from complaining about inconsistent line endings when working with Unity .cs files

If you are like me, you probably don't like the constant popup dialogs from Visual Studio and MonoDevelop on the inconsistent line endings. You may not have any idea what I'm talking about but constantly see this in Visual Studio while working with your Unity .cs files














First - what on earth are inconsistent line endings? Let's look at what happens when you press ENTER on a PC based system vs a Unix based OS like OSX. A PC writes a CR/LF (carriage return / line feed) pair. A LF is '\n' or if you like hex 0x0A or 10 in decimal. A CR is '\r', 0x0D, 13 respectively.  For you c/c++/c# programmers out there you've probably seen some string "Hello World\n" or "Hello World\r\n" - also noting we have a LF and CR/LF

Let's take the following text....

See cat run
See sharp

Specifically if you look in a Hex Editor, the bytes in the file look like this on PC (note the highlight 0D 0A)












and like this on a Mac (note the missing 0D)










Why does this happen with Unity and Visual Studio? When Unity creates a C# file, it creates it by default with LF only as shown here.





















Visual Studio will open this up without a problem and without notifying you of anything. You aren't mixing CR/LF with your LF. Now you go ahead and paste some code into here, or use some existing code snippets, or format the document, or rely on some auto-complete/formatting features and they may stick in CR/LF into this doc (as you are editing on a pc).  Now you have a file with LF and CR/LF mixed in it. It's inconsistent, hence you get a dialog box like the first image shown above Visual Studio loads that file.

Also note that if you do tell Visual Studio to normalize the line endings (ie clicking yes in that first dialog) it will fix this issue, but for this file only.

We have another product called Visual Studio Code (the free cross platform code editor/debugger) which actually just has a little drop down to select line endings and doesn't prompt you either as its a cross platform code editor. Visual Studio (a very different product) on the other hand is a PC based only full-featured IDE and expects PC based line endings but of course _can_ work with either CR/LF, you just have to tell it not to warn you. It thinks its doing you a favor (some third party tools over the years actually get messed up by mixed line endings, so its generally a good idea to have consistent line endings in your project).


The fix

Aha. At last. The big reveal. The big secret orphan annie ovaltine decoder ring thingy reveal (our ending is a bit better but for those that have no idea what I'm talking about check out this video clip here). The fix is actually super simple. Visual Studio has a feature called Quick Launch in the upper right hand corner. Simply search for "line" as shown below, select "Environment->Documents (Change line ending settings)" and UNCHECK "Check for consistent line endings on load". Voila. Annoyance resolved. Both the Mono and .NET C# compilers will handle mixed line endings just fine, so there's no problem here with doing this. Btw - if you are using a PC and aren't using my free "Right click to open folder as Unity project" registry script, check out my blog post on this. Enjoy!





15 comments:

  1. You could also open up the C# code template file with a different editor like Notepad++ and perform a replace all \r -> \r\n (make sure Search Mode "Extented" or "Regular expression" is checked). That way, you don't have to turn off the warnings from Visual Studio that might be helpful on a different project and even get line endings consistent with your OS.

    ReplyDelete
    Replies
    1. No need, Visual Studio will already correct the line endings for you if you click yes to the first image shown above, but people get tired of seeing the dialog and having to click yes for every new file they make certain changes too.

      Delete
    2. That is way you should open up the template file and make the changes directly there. This way, for every newly generated .cs file, the line endings will be correct and you won't get the "incorrent file endings" dialog, even with the warning enabled (after all, the line endings are correct).

      Delete
    3. Ah got ya - you'll only be prompted once in Visual Studio to update it though and it will do the same thing without having to open in an external tool. BTW I'm asking that feedback go to Unity to change the default template, so we shall see :)

      Delete
    4. Well, I guess the default template is a Unix template to be used with Mono, where line endings are encoded by '\n'. I don't know how Mono behaves when it suddenly encounters '\r\n'.

      Delete
  2. Thank you. Just did a reinstall of VS the other day and was getting drowned in these messages.

    ReplyDelete
  3. This is realy useful article, I like Adam Tupliper he looks very samrt and his posts is very interesting. Tutuapp ios

    ReplyDelete
  4. Nice blog with actual content. Information on your blog facilitates practical knowledge.
    Thanks
    Offshore dedicated server

    ReplyDelete

Note: Only a member of this blog may post a comment.