ruby / ruby/ostruct

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

Open
#27 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
175
Forks
35
Avg merge
16h 14m
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with OpenStruct.new(gem: 'foobar').gem under ruby and bundle exec, then inspect Bundler's lib/bundler/rubygems_integration.rb around line 316. Determine whether the behavior belongs in OpenStruct or Bundler, and add coverage showing that the chosen fix makes the attribute usable and avoids the cryptic error.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.