Skip to content

With bundler, an OpenStruct can't have a field called 'gem' (and it's really hard to troubleshoot -- redefine_method) #27

@ianfixes

Description

@ianfixes

After many hours of head-scratching, I can offer this bug report:

OpenStruct.new(gem: 'foobar').gem

The above code works in "bare" ruby, but not with bundler.

Steps to reproduce:

$ mkdir ostruct-bug

$ cd ostruct-bug

$ echo "source 'https://rubygems.org'" > Gemfile

$ bundle install
The Gemfile specifies no dependencies
Resolving dependencies...
Bundle complete! 0 Gemfile dependencies, 1 gem now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]

$ bundle -v
Bundler version 2.1.4

$ ruby -e "require 'ostruct' ; puts OpenStruct.new(gem: 'foobar').gem"
foobar

$ bundle exec ruby -e "require 'ostruct' ; puts OpenStruct.new(gem: 'foobar').gem"
Traceback (most recent call last):
	1: from -e:1:in `<main>'
/Users/ikatz/.rbenv/versions/2.7.2/lib/ruby/2.7.0/bundler/rubygems_integration.rb:316:in `block (2 levels) in replace_gem': wrong number of arguments (given 0, expected 1+) (ArgumentError)

$ bundle exec ruby -e "require 'ostruct' ; puts OpenStruct.new(germ: 'foobar').germ"
foobar

Whether or not this is a problem with bundler (vs ostruct), the error is very unhelpful -- gem seems to all appearances like a perfectly valid attribute name and jumping into bundler source is generally not my first thought when debugging my own project.

The relevant bundler code is here:
https://github.com/rubygems/bundler/blob/master/lib/bundler/rubygems_integration.rb#L316

It would seem that the correct behavior in ostruct would be to check whether a method name is available (possibly with self.respond_to?(new_attribute_name) at the time the attribute is being defined , so that it can produce a warning or exception immediately instead of cryptic behavior when the attribute is later accessed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions