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

--auto-correct on windows changes the line-ending #1566

Closed
ccoenen opened this issue Jan 12, 2015 · 4 comments
Closed

--auto-correct on windows changes the line-ending #1566

ccoenen opened this issue Jan 12, 2015 · 4 comments

Comments

@ccoenen
Copy link

ccoenen commented Jan 12, 2015

I am using Unix-Line-Endings everywhere, but --auto-correct writes back CRLF to my files on windows. As far as i can see, there is no setting to change that.

If you are opening a file for writing on windows with filemode "W" (File.open(name, 'w')), the resulting file will always have CRLF as newline characters. To write \n, you would have to open a file for binary writing ('wb')

I found two places where a file was opened for writing, i am not sure which is the right place, but maybe it helps: formatter_set.rb#add_formatter and team.rb#autocorrect.

As an experiment, i changed this to be 'wb' on my machine, this did the trick. I was rewarded with unix-style newlines.

@jonas054
Copy link
Collaborator

On my system (Ubuntu Linux), everything still works if I change to 'wb'. I think it makes most sense to only do this change in Team#autocorrect, thus keeping whatever line endings were used. In FormatterSet#add_formatter we create a new file, which should have the system default line endings.

@alexdowad
Copy link
Contributor

I was rewarded with unix-style newlines.

Hmm. You could have opened a PR...

@ccoenen
Copy link
Author

ccoenen commented Dec 21, 2015

Yes, I could have, but since I had no way of verifying other platforms at that time and my sketchy knowledge of the codebase, i chose not to.

I am glad that you are taking this up again, I have to admit that I did not use --auto-correct since, and completely forgot about this issue.

DavidS pushed a commit to DavidS/rubocop that referenced this issue Jul 3, 2019
This change fixes the main call site of `File.open` when autocorrecting
to use binary I/O, so no additional translation from ruby is happening.

Additionally there are some required changes to the test infrastructure
to keep everything aligned.
@erys
Copy link

erys commented Apr 22, 2024

This still seems to be an issue in current rubocop. Here's my monkey patch solution that seems to work for anyone else having this issue:

# lib/rubocop_autocorrect.rb

RuboCop::Cop::Team.class_eval do
  def autocorrect(processed_source, report, original:, offset:)
    @updated_source_file = false
    return unless autocorrect?
    return if report.processed_source.parser_error

    new_source = autocorrect_report(report, original:, offset:)

    return unless new_source

    if @options[:stdin]
      # holds source read in from stdin, when --stdin option is used
      @options[:stdin] = new_source
    else
      filename = processed_source.file_path
      File.write(filename, new_source, mode: 'wb')
    end
    @updated_source_file = true
  end
end

and

# .rubocop.yml

require:
  - ./lib/rubocop_autocorrect.rb

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

No branches or pull requests

4 participants