Skip to content

2.16 autocorrect of Rails/RootPathnameMethods is unsafe, i.e. breaking code #763

Closed
@Jay-Schneider

Description

@Jay-Schneider

Expected behavior

When rubocop detects a violation of of Rails/RootPathnameMethods and autocorrects it, I expect it to keep the behaviour of the code unchanged.

Actual behavior

The code breaks, so autocorrection for this cop is not safe in the current version.

Steps to reproduce the problem

I was able to reproduce the problem with the following simple file:

# foo.rb
puts Dir.glob(Rails.root.join("**/*.rb"))

When I run rails runner foo.rb it prints a list of files.

rubocop foo.rb reports the offense

foo.rb:2:6: C: [Correctable] Rails/RootPathnameMethods: Rails.root is a Pathname so you can just append #glob.
puts Dir.glob(Rails.root.join("**/*.rb"))

Now, using rubocop -a foo.rb changes the content of the file to

# foo.rb
puts Rails.root.join("**/*.rb").glob

which is not runnable code anymore and instead raises an error: foo.rb:2:in 'glob': wrong number of arguments (given 0, expected 1..2) (ArgumentError)

I think what is supposed to happen when autocorrecting is

# foo.rb
puts Rails.root.glob("**/*.rb")

because this actually seems to be equivalent to the original code and does not violate the Rails/RootPathnameMethods cop. But that's not what's happening.

RuboCop version

1.36.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.1.2) [arm64-darwin21]
  - rubocop-performance 1.14.3
  - rubocop-rails 2.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions