Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout/EndOfLine detects Carriage Return in first line when BOM is not set in UTF-8 #4669

Closed
marchwia opened this issue Aug 15, 2017 · 11 comments
Labels

Comments

@marchwia
Copy link

Layout/EndOfLine detects CRLF in the first line of UTF-8 LF formatted files if BOM is not set.

Problem goes away after setting BOM.


Expected behavior

Pass with no errors for UTF-8 file without BOM.

Actual behavior

> rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Carriage return character detected.
#!/usr/bin/env ruby ...
^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

test.rb

#!/usr/bin/env ruby
#

.rubocop.yml

Layout/EndOfLine:
  EnforcedStyle: lf

Command line

rubocop test.rb

RuboCop version

> rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.3.3 x64-mingw32)
@jonas054
Copy link
Collaborator

Are you absolutely sure there are no carriage return characters in the file when RuboCop reports Carriage return character detected.? Please check with od -a <file.rb> and see if there aren't any cr characters in there.

@marchwia
Copy link
Author

No cr characters for sure:

> od -a test.rb
0000000   #   !   /   u   s   r   /   b   i   n   /   e   n   v  sp   r
0000020   u   b   y  nl   #  nl
0000026

@jonas054
Copy link
Collaborator

Well I'll be darned. Then I don't know. I haven't been able to reproduced the problem, and I've tried on a MinGW system.

If you could write a failing spec example, that would be perfect.

@a-nickol
Copy link

a-nickol commented Feb 8, 2018

I have similar issues. Depending on the platform I use: windows commandline, mingw, or msys, I am getting different results.

Note that if rubocop is running in windows commandline rubocop -V it returns 0.52.1 (using Parser 2.4.0.2, running on ruby 2.4.1 i386-mingw32), too. Maybee marchwia has been using the native windows commandline, too.

The problems start to multiply when using guard-rubocop. Files which are clean when running rubocop alone, get offenses when scanned by guard-rubocop

@a-nickol
Copy link

a-nickol commented Feb 8, 2018

@a-nickol
Copy link

a-nickol commented Feb 9, 2018

If have done some more testing. I don't understand how the parameter --force-exclusion could change the results.

https://gist.github.com/HansPeterIngo/8fb359ba8cbec21e220cf7bfa2bfb6d2

@jonas054
Copy link
Collaborator

jonas054 commented Feb 9, 2018

I have an idea what might be going on here. It's related to caching. It looks like a file whose line endings have changed still has the same hash key (used when caching the inspection result) as it had before the change. This is a bug in the caching mechanism. You should be able to work around it by running

rubocop --cache false

@a-nickol
Copy link

a-nickol commented Feb 9, 2018

You are correct, after adding --cache false all commands return the correct result.

@jonas054 jonas054 added the bug label Feb 9, 2018
@jonas054
Copy link
Collaborator

jonas054 commented Feb 9, 2018

I'll try to fix it.

jonas054 added a commit to jonas054/rubocop that referenced this issue Feb 9, 2018
IO.read converts CRLF to LF on Windows. This means that the
cached results for a file are still considered valid even if
the file has changed EOL characters. This is not good for the
Layout/EndOfLine cop, which will produce incorrect results
taken from the cache. Reading in binary mode fixes the problem.
@ChintuKarthi
Copy link

ChintuKarthi commented Mar 22, 2019

Are you absolutely sure there are no carriage return characters in the file when RuboCop reports Carriage return character detected.? Please check with od -a <file.rb> and see if there aren't any cr characters in there.

Hi, @jonas054 I have a cr in my file, can you explain how to solve this?

Thanks.

@jonas054
Copy link
Collaborator

Unfortunately, the Layout/EndOfLine cop doesn't support --auto-correct so RuboCop can't help you with this.

The solution depends on which platform you're running on (Mac, Windows, ...) and which IDE/Editor. In Emacs, for example, you can search and replace ^M. If you're on MacOS, see this answer: https://stackoverflow.com/questions/21621722/removing-carriage-return-on-mac-os-x-using-sed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants