5

When I execute any rails command in my project, I'm receiving this warning:

/home/vagrant/.rvm/rubies/ruby-2.5.3/bin/ruby: warning: shebang line ending with \r may cause problems

I tried to install another Ruby version, but the warning is the same. I'm using the same Ubuntu version of the developer.

Rodrigo Corrêa
  • 51
  • 1
  • 1
  • 2
  • _"line ending with \r"_ – seems like your code has Windows line endings, i.e. `\r\n`. Use Unix line endings instead, i.e. `\n`. – Stefan Jan 21 '19 at 09:40
  • @Stefan I’d say it’s rather Macintosh legacy. Win lines do indeed _end with `\n`_, despite there is `\r` before it. – Aleksei Matiushkin Jan 21 '19 at 09:53
  • 3
    @AlekseiMatiushkin: The warning is badly worded. The shebang line is terminated with `\n`. If there is a Windows line ending, then the last character that is *part of the shebang line*, i.e. the last character *before the terminating `\n`* will be `\r`. This will still be interpreted as part of the interpreter name by at least some operating systems, so it will actually search for an interpreter executable named `ruby\r`, which it obviously won't find. So, the warning message considers the last character before the terminator as the "line ending" and the terminator not part of the line. – Jörg W Mittag Jan 21 '19 at 11:36
  • The corresponding method [`warn_cr_in_shebang`](https://github.com/ruby/ruby/blob/v2_6_0/ruby.c#L1871-L1876) actually checks for `\r\n`. I've opened a [PR](https://github.com/ruby/ruby/pull/2073) to reword the message. – Stefan Jan 21 '19 at 11:57

3 Answers3

1

I also had the same problem when I was trying to run bin/rails db:create. It took me hours trying to figure out the problem and yet it was an easy fix. I am using Visual Studio Code as my editor.

My solution To solve this, I navigated to the ruby file located in the bin folder(bin/rails). At the bottom of the Visual Studio Code editor(status bar), you should be able to see a button CRLF(control character). Click on it and change it to LF.

Reason for the error message This is because Windows by default uses the CRLF control character and now that you are using a Windows subsystem for Linux, the control character should be LF

I hope this helps anyone out there.

0

You have a Windows line ending in your file ('\r\n') instead of a unix line ending ('\n'). So the shell tries to get the first line, up to and excluding the first \n, and finds that the line ends with \r.

There are several ways to avoid this issues:

  1. With git, automatically: git config --global core.autocrlf true (see https://help.github.com/articles/dealing-with-line-endings/ and How to change line-ending settings)

  2. With os-level tools: dos2unix (use your OS tools to install it)

  3. Editors + editorconfig file: Check https://editorconfig.org/#file-format-details to see how you can set the end_of_line config, and let your editor do the rest.

rewritten
  • 16,280
  • 2
  • 47
  • 50
0

What worked for me on Windows:

  • Remove git default line ending conversion for Windows: git config --global core.autocrlf false
  • Convert all files line endings from crlf to lf (I used linux terminal for this): sudo find . -type f -exec dos2unix {} \;
  • Renormalize with git: git add --renormalize

You shouldn't have anything to commit and you can check it worked with: git ls-files --eol