Search Unity

inconsistent line endings

Discussion in 'Scripting' started by hjbaard, Feb 6, 2010.

  1. JackofTraes

    JackofTraes

    Joined:
    May 28, 2014
    Posts:
    10
    I wasn't aware of how new it was, but I posted to let others know that there is an easier way to fix the problem. I was struggling to find any forum or stack-exchange posts that mentioned it. Sorry if it came off like I was criticizing the last post; that was not my intention.
     
  2. sristyh

    sristyh

    Joined:
    Jul 4, 2018
    Posts:
    1
    go to file advanced option from where??
     
  3. Khonkaengame

    Khonkaengame

    Joined:
    Aug 17, 2018
    Posts:
    4
    I want to share some magic just happened to me.
    On MacOS i use cmd + A copy all of code in the file from Visual Studio 2017 and paste in the to file on * Visual Studio CODE * then remove the last line (may not have to do this) and copy code back to Visual Studio 2017. The problem is just gone.

    I am trying to prove it one more time. but it just gone and not comeback again. Hope this help.
     
  4. Deleted User

    Deleted User

    Guest

    Ha ha! Thank you so much!!!!! :D :dancing:
     
    etzl and BaihuTR like this.
  5. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Yes, I logged in just to give a like. I finally get rid of these annoying warnings!
     
  6. azizulislam1

    azizulislam1

    Joined:
    Jan 28, 2019
    Posts:
    1
    Thanks. You have saved my day.
     
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I never understood why Unity bothers with this warning. Does it affect your scripts? Nope. Is it of any consequence? Nope. Then why on earth are we being bothered? Because. Can Vusiual Studio work with both? Yes. Can MonoDevelop? Yes. Is it of any consequence? Nope. Yet here we are being warned, but when I change a class Name so that it no longer matches the file name (with sometimes dire consequences) -- crickets.

    I really hope they remove this annoying, silly warning. It's in Visual Studio 2017 and MonoDevelop that came with 2017.

    Alternatively, can anyone give a plausible, COMMON use case that justifies the warning?

    -ch
     
    Ubrano likes this.
  8. nofreewill42

    nofreewill42

    Joined:
    Feb 3, 2019
    Posts:
    5
    FIXED on Ubuntu in MonoDevelop. I've just did this: Ctrl+A, Ctrl+C, Ctrl+V. It seems that copy-pasting inserts new lines the correct way.
     
  9. FiredLight

    FiredLight

    Joined:
    May 11, 2019
    Posts:
    1
    What I do that works without having to change the script template or use any extensions is to use the Format Document tool in visual studio as soon as I open a new script. This has the added benefit of also changing the ugly default formatting (spaces between method brackets yuk). As long as you've set up your Code Style rules to your liking in Visual Studio's preferences this is the nicest solution imo.
     
  10. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    I've had this issue for some time and tried all the fixes like changing the settings, but I still got the error.

    Then I got to this thread and read a comment on the first page that mentioned what line endings actually were, them being lines such as //r // r, so that's when it clicked in my brain that my variations of notes might cause the issue.

    If you thought (like me) that line endings were something at the very end of your script then I guess you're wrong. It also matters how you end your lines throughout your script. It still doesn't make sense to me how that's different per OS and why the Unity console kept buggering me about such a thing, but so far I found sticking to a consistent 'no space, or one space' in my script comments to be my solution.
     
  11. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,627
    Line endings have nothing to do with how you manually end your lines. Line endings refer to the invisible character code used to signify a new line. They appear at the end of every single line in a text file. You never see the line endings in your code. They are hidden, but are embedded in the text file (.cs file). (Some text editors allow you to see the line ending code if you enabled it.) Developers of the different operating systems made different decisions as to which character codes to use to end a line in a text file way back in the early days of the operating systems. If you open a text file on a Unix-based machine (Mac), it will encode new lines with one character, then you modify that file on a Windows-based machine, it may use Windows-style new line codes when you insert new lines. That's what the "inconsistent line endings" warning is telling you. (Generally, text editors will detect the new line style being used and won't mix new line code types regardless of the operating system in use. You can also choose a specific line ending style in most editors.)

    More details here;
    https://en.wikipedia.org/wiki/Newline
     
  12. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    Thanks. Now I'm even more confused as to why I get that error then. I'm using Unity and Visual studio on a Windows 10 PC, alone, and don't share work (besides manually 'copying' code from tutorials and such) I have Unity set to 'native / Windows' line endings and I also got a similar setting enabled in Visual Studio. I'll just ignore that error from now on to keep my sanity.
     
  13. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,627
    Copying and pasting code in that uses different line endings could definitely do it. I don't think Visual Studio will convert the line endings for you when pasting from a file that uses different line endings. Many other things can lead to this such as when Unity changes code for you in your scripts (API updater). I've especially seen it happen with shaders when they silently change some deprecated API calls and add notes to the top of the file without using the same line endings as the rest of the file. Anything that may modify your text file is a potential culprit.

    VS has an option in File -> Advanced Save Options where you can set line ending style, but it sounds like you already have this set. You can also use common text editors to change the line endings in a file manually. For example, in Notepad++, go to Edit -> EOL conversion. Here's a thread on changing line endings in bulk across many files.

    Another common source of this issue which was mentioned in this thread is the Unity new C# script file template which defaults to Unix-style line endings. You make a new C# file in Unity (the default "public ClassName : MonoBehaviour", blah file), open the file in Visual Studio, make some changes, save, and bam, you get the warning. You added Windows-style line endings to a file that had Unix-style line endings to begin with. The setting in VS -> File -> Advanced Save Options -> Line Endings -> Windows (CR LF) should prevent this, but by default VS won't modify existing line endings by itself. One option is to modify the files in Program Files\Unity\Editor\Data\Resources\ScriptTemplates and change their line ending styles in a text editor. Unity also added an option to set the line endings for new scripts. See this post.

    Edit: It seems the VS -> File -> Advanced Save Options -> Line Endings -> Windows (CR LF) setting is a one-time setting that only applies to the file currently open for the next save. It's essentially the same thing as using Notepad++ to convert the line endings. It doesn't seem to enforce a specific line ending style throughout the solution.
     
    Last edited: Jul 21, 2019
    DonLoquacious likes this.
  14. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    This problem can also happen when you copy a code snippet and insert into your own file. This happened to me when copying from Unity's manual.

    A REALLY quick solution was to copy all the script code, import it into a simple editor that doesn't do formatting (like notepad), copy all the code from that, then replace what was in your script file with this now-stripped code.
     
  15. unpluggeDloop

    unpluggeDloop

    Joined:
    Apr 12, 2014
    Posts:
    1
    There are inconsistent line endings in the 'Assets/Scripts/YourScriptName.cs' script. Some are Mac OS X (UNIX) and some are Windows.
    This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands.

    You can go in your folder where script is for example C:\Documents\Game\ExampleBugScript.cs open it in notepad and save it. When you go back in visual studio popup will show click on Yes and another popup will say The line endings in the following file are not consistent. Do you want to normalize the line endings? Line ending: in drop down select Windows (CR LF) click on Yes, save your file and when you enter in Unity error will disappear.

    Or

    In Visual Studio go to File > Save (File) As > Save (click dropdown arrow) > Save with Encoding > Do you want to replace it? > Yes > Advanced Save Option > Encoding: Unicode (UTF-8 with signature) - Codepage 65001 | Line endings: Windows(CR LF) > OK
     
    Last edited: Feb 2, 2020
  16. HannuH

    HannuH

    Joined:
    Feb 23, 2020
    Posts:
    1
    THIS worked for me.Thanks
     
  17. saporter

    saporter

    Joined:
    Mar 19, 2015
    Posts:
    7
    This! Thank you so much. This is the updated answer and it worked for me.
     
  18. the_big_kahuna

    the_big_kahuna

    Joined:
    Apr 2, 2020
    Posts:
    1
    Unity 2019.3.11f1 go into edit > project settings > editor and scroll to bottom, you can change it there
    upload_2020-5-13_13-14-54.png
     
    den4icccccc likes this.
  19. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I guess the real question is: why flag this as an error at all? It's a non-issue, there is no potential risk. And it's not a Win/*NIX issue at all. It's an annoying warning that dates back to ca. the early 1980's, when there was a potential issue moving source files between CP/M, DOS and UCSD-pS, and your compiler could barf when encountering CRLF instead of just CR (or vice versa). That issue was resolved before most of our parents even got interested in each other. So why warn about things that are irrelevant? Does MS still holds a grudge against Digital? They won. MS should move on.
     
    jo_moe likes this.
  20. ahmedaniss

    ahmedaniss

    Joined:
    Sep 18, 2019
    Posts:
    98
    problem fixed here :
     
  21. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,355
    I wonder if now there is a single click solution to remove those useless annoying warnings.
     
  22. masuelele

    masuelele

    Joined:
    Nov 25, 2022
    Posts:
    2
    This helped to solve my errors converted with notepad++ 81-C# Script-NewBehaviourScript.cs.txt to windows ending lines, reopened the proyect and dissapeared like a magic when i run the game.
     
  23. alexisrenart

    alexisrenart

    Joined:
    Oct 20, 2022
    Posts:
    1
    A bit of a radical solution but it works for me:
    Code (CSharp):
    1. // CWLE Ignore
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEditor.Compilation;
    5.  
    6.  
    7. public class CleanWindowsLineEndings
    8. {
    9.     private static readonly string[] Patterns = { "*.cs", "*.hlsl" };
    10.  
    11.     private static readonly string[] IgnoredDirectories = { "Plugins" };
    12.  
    13.     [MenuItem("Tools/Clean Windows Line Endings &l")] // Alt + L
    14.     private static void Clean()
    15.     {
    16.         bool hasChanged = false;
    17.  
    18.         foreach (string pattern in Patterns)
    19.         {
    20.             string[] paths = System.IO.Directory.GetFiles(Application.dataPath, pattern, System.IO.SearchOption.AllDirectories);
    21.  
    22.             foreach (string path in paths)
    23.             {
    24.                 bool isIgnored = false;
    25.  
    26.                 foreach (string excludedDir in IgnoredDirectories)
    27.                 {
    28.                     if (path.Contains(System.IO.Path.DirectorySeparatorChar + excludedDir + System.IO.Path.DirectorySeparatorChar))
    29.                     {
    30.                         isIgnored = true;
    31.                         break;
    32.                     }
    33.                 }
    34.  
    35.                 if (isIgnored)
    36.                     continue;
    37.  
    38.                 string content = System.IO.File.ReadAllText(path);
    39.  
    40.                 if (content.ToLower().StartsWith("// cwle ignore"))
    41.                     continue;
    42.  
    43.                 if (content.Contains("\n\r") || content.Contains("\r"))
    44.                 {
    45.                     hasChanged = true;
    46.                     string newContent = content.Replace("\n\r", "\n").Replace("\r", "");
    47.                     System.IO.File.WriteAllText(path, newContent);
    48.  
    49.                     Debug.Log("CWLE clean file \"" + System.IO.Path.GetFileName(path)  + "\" at path:\n" + path);
    50.                 }
    51.             }
    52.         }    
    53.  
    54.         if (hasChanged)
    55.             CompilationPipeline.RequestScriptCompilation();
    56.     }
    57. }
     
    Last edited: Dec 2, 2022
  24. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    From mac :

    Use Terminal :
    1. Be sure dos2unix is installed : brew install dos2unix
    2. Drop the cs file from the warning into Terminal and add dos2unix before the whole path.
    3. Press enter, that's it ;)

    Screenshot 2566-01-25 at 10.26.31.png