Skip to content
New issue

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

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

Already on GitHub? # to your account

gemspec claims compatibility with Ruby 3, but uses Ruby 2 argument forwarding #157

Closed
kyrofa opened this issue Sep 6, 2024 · 0 comments · Fixed by #158
Closed

gemspec claims compatibility with Ruby 3, but uses Ruby 2 argument forwarding #157

kyrofa opened this issue Sep 6, 2024 · 0 comments · Fixed by #158

Comments

@kyrofa
Copy link
Contributor

kyrofa commented Sep 6, 2024

I got some inexplicable "ArgumentError: wrong number of arguments (given 1, expected 0)" errors against Rails edge today. I traced it back to some Ruby 2-era argument forwarding in goldiloader. Turns out there's a bit of that in the ActiveRecord patches that needs to be updated to be compatible with Ruby 3.

To make the issue clear, take this simple example:

class Foo
  def initialize(*args, **kwargs)
    puts "ARGS: #{args}, KWARGS: #{kwargs}"
  end
end

class Bar < Foo
  def initialize(*args)
    super
  end
end

puts 'Foo:'
Foo.new('positional', keyword: 'argument')

puts 'Bar:'
Bar.new('positional', keyword: 'argument')

This runs fine on ruby 2.5:

$ rvm-exec 2.5.8 ruby example.rb
Foo:
ARGS: ["positional"], KWARGS: {:keyword=>"argument"}
Bar:
ARGS: ["positional"], KWARGS: {:keyword=>"argument"}

Also fine on 2.7:

$ rvm-exec 2.7.5 ruby example.rb
Foo:
ARGS: ["positional"], KWARGS: {:keyword=>"argument"}
Bar:
ARGS: ["positional"], KWARGS: {:keyword=>"argument"}

However, it does NOT work on 3.0:

$ rvm-exec 3.0.4 ruby example.rb
Foo:
ARGS: ["positional"], KWARGS: {:keyword=>"argument"}
Bar:
ARGS: ["positional", {:keyword=>"argument"}], KWARGS: {}

As you can see, that ends up passing kwargs as a positional arg. In my case, since upstream find_target only accepts kwargs, it explodes.

In Ruby 3 we can use a combination of ... and explicit *args, **kwargs.

kyrofa added a commit to kyrofa/goldiloader that referenced this issue Sep 6, 2024
Today this uses Ruby 2-era argument forwarding that doesn't separate out
keyword arguments in Ruby 3. Update the forwarding technique to be
compatible with Ruby 3.

Fix salsify#157

Signed-off-by: Kyle Fazzari <kyrofa@ubuntu.com>
@jturkel jturkel closed this as completed in 9193940 Sep 6, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant