0

I'm currently developing for a Ruby on Rails application. I am on a windows machine, using ubuntu through WSL. We have rubocop setup to track formatting on git pushes, and I always run into the same issue. When adding this comment to the top of any file, I receive the error Layout/EndOfLine: Carriage return character detected. The issue seems to be talked about here as well Layout/EndOfLine: Carriage return character detected. module TieConnector

# frozen_string_literal: true

The solution here seems to be to disable the warning, or manually convert the files before pushing.

Does anyone know if there is another solution here? I don't believe I can edit the rubocop settings as it is not my project. The converting method does work, but gets a bit tedious to do every time I make a new pull request.

Would there be a way to run the dox2unix convert when running git push while targeting the files staged for commit? Or a way to force my machine to use the unix encoding by default?

Any help is appreciated, thank you

0lafe
  • 23
  • 4
  • Why don't you just configure your editor or IDE to use `LF` for newlines? Most editors should be able to convert `CRLF` to `LF` automatically when saving a file. – spickermann Aug 29 '22 at 18:22
  • I believe I'm already doing this. I'm using VSCode, and the end of line sequence is set to LF as opposed to CRLF – 0lafe Aug 29 '22 at 19:14
  • I am not familiar with VSCode but it sounds to me like it is configured to use `LF` for new lines (when you press enter) but like it does not fix existing new lines from `CRLF` to `LF` when they exist for other reasons (legacy code or introduced with copy-paste). – spickermann Aug 30 '22 at 06:34

2 Answers2

0

It's a common issue on windows. If you are using vscode you can convert all your files to LF with this shortcut: ctrl + shift + P => 'change all end of line sequence' => Enter => Enter => Enter => 'LF' => Enter Then you can commit and push your code. If it still don't work you might see your git configuration, autocrlf in particular. See this response: stackoverflow.com/a/20653073/7542830

To solve it on a repository level: git config --local core.autocrlf input (on you project root)

Moussa
  • 461
  • 4
  • 14
  • The second method worked for me. Setting `core.autocrlf` to `input` seemed to fix it. Thank you very much! – 0lafe Aug 30 '22 at 16:39
  • 1
    Sure! Also, I don't think it's a rubocop issue. It's rather an issue with the line ending configuration – Moussa Aug 31 '22 at 00:18
0

I'm using RubyInstaller2 (MSYS2 backend, not WSL) and ran into the same issue when RuboCop made some fixes (apparently is used CRLF instead of LF).

I fixed all the files in vim with :bufdo %s/\r//, but still got the error as the first character was still a CR (or CRLF).

Had to end up using dos2unix, as recommended in Layout/EndOfLine: Carriage return character detected. module TieConnector

[EDIT]: Error is intermittent on Windows due to missing BOM: https://github.com/rubocop/rubocop/issues/4669

go2null
  • 2,080
  • 1
  • 21
  • 17