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

The suggestions of the FilePath cop cause a TypeError if %w is used to specify the path #1389

Open
cyclingzealot opened this issue Nov 28, 2024 · 0 comments

Comments

@cyclingzealot
Copy link

Expected behavior

With a line like File.join(Rails.root, %w[test files migration]) with an array, it should be either
A. be ignored or
B. turned into Rails.root.join('test', 'files', 'migration').to_s

Personally, I prefer the notation with %w to minimize quotes. Thus I wouild choose option A, but I suspect gem author prefers B, which is acceptable.

Actual behavior

Running Rails/FilePath cop on File.join(Rails.root, %w[test files migration]) turns into Rails.root.join(%w[test files migration]).to_s, which Pathname.join can't handle array

Steps to reproduce the problem

  1. Steup test file
$ cat > tmp/test.rb
require File.expand_path("./config/environment")
File.join(Rails.root, %w[foo bar])
  1. Test and confirm clean execution
$ docker-compose exec web ruby tmp/test.rb
$ echo $?
0
  1. Run rubocop
docker-compose exec web rubocop --only Rails/FilePath  -a tmp/test.rb

tmp/test.rb:2:1: C: [Corrected] Rails/FilePath: Prefer Rails.root.join('path/to').to_s.
File.join(Rails.root, %w[foo bar])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1. Confirm change
$ cat tmp/test.rb 
require File.expand_path("./config/environment")
Rails.root.join(%w[foo bar]).to_s
  1. Attempt to execute file
$ docker-compose exec web ruby tmp/test.rb
/usr/local/lib/ruby/3.3.0/pathname.rb:419:in `initialize': no implicit conversion of Array into String (TypeError)

    result = Pathname.new(result) unless Pathname === result
                          ^^^^^^
	from /usr/local/lib/ruby/3.3.0/pathname.rb:419:in `new'
	from /usr/local/lib/ruby/3.3.0/pathname.rb:419:in `join'
	from tmp/test.rb:2:in `<main>'

RuboCop version

$ docker-compose exec web rubocop --verbose-version
1.69.0 (using Parser 3.3.6.0, rubocop-ast 1.36.2, analyzing as Ruby 3.3, running on ruby 3.3.6) [x86_64-linux-musl]
  - rubocop-performance 1.23.0
  - rubocop-rails 2.27.0

Reference

Similar to #472 but specific when using %w

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

1 participant