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

[ActiveSupport] Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes #20494

Merged

Conversation

knovoselic
Copy link
Contributor

This PR includes a test that reproduces the issue described in #20489 and a fix for it. Here's a short explanation of the issue by @ewoutkleinsmann:

ActiveSupport::Concern's class_methods uses const_defined? without a second argument and therefore also searches its ancestors for a ClassMethods module. This results in concerns included in a child class that add class methods to a parent class.

@knovoselic knovoselic changed the title Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes [ActiveSupport] Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes Jun 9, 2015
@rafaelfranca
Copy link
Member

Tests are broken, could you take a look?

@knovoselic
Copy link
Contributor Author

@rafaelfranca I was pretty tired so I thought I'd do it today. Should've left a comment, sorry about that. I was working on master. I just created an empty pull request, just to see if all tests on master are passing, and they are not:
#20502

Should I rebase my changes on top of another branch (one for which the tests are passing) and try again? Which one would you recommend (maybe 4-2-stable)?
Right now I do not know which tests are failing because of my change and which were failing before.

@rafaelfranca
Copy link
Member

master is green as you can see here https://travis-ci.org/rails/rails/branches. You patch broke two tests. ConfigurableActiveSupport#test_configuration_accessors_are_not_available_on_instance and ConfigurableActiveSupport#test_configuration_is_crystalizeable. They are not broken on master.

@knovoselic
Copy link
Contributor Author

@rafaelfranca Ok, thanks. I didn't see that there's Allowed Failures section. I'll try to figure out why ConfigurableActiveSupport#test_configuration_accessors_are_not_available_on_instance and ConfigurableActiveSupport#test_configuration_is_crystalizeable are failing.

@knovoselic knovoselic closed this Jun 10, 2015
@knovoselic knovoselic reopened this Jun 10, 2015
@knovoselic
Copy link
Contributor Author

The problem was in
::Object.__send__(:include, Bar)

I've changed the test to reduce pollution, but it still needs to include Qux in Object (couldn't figure out how to reproduce the bug differently). I'd appreciate any suggestion on how to improve the test.

@rafaelfranca
Copy link
Member

Can we have one more level of constants? Or this bug only happen if the ClassMethods is defined at ``::Object`?

@knovoselic
Copy link
Contributor Author

If I understood it correctly, it happens only when ClassMethods is defined in one of Module's ancestors, and Module.ancestors returns:

irb(main):001:0> Module.ancestors
[
  [0] Module < Object,
  [1] Object < BasicObject,
  [2] Kernel,
  [3] BasicObject
]

@rafaelfranca
Copy link
Member

O_O. How is this bug happening in the wild? Is someone opening one of these Modules?

@knovoselic
Copy link
Contributor Author

This line was triggering the bug in my app:
https://github.com/mongoid/mongoid/blob/master/lib/mongoid/extensions/object.rb#L274

@@ -54,6 +54,12 @@ module Foo
include Bar, Baz
end

module Qux
extend ActiveSupport::Concern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extend needed? mongoid does not extend it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just need to manually call Object.extend in that case. Should I remove it and add Object.extend call?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Looks good

@rafaelfranca
Copy link
Member

Could you squash your commits? I'm going to merge it.

BTW, great work

@sgrif
Copy link
Contributor

sgrif commented Jun 11, 2015

👍 on great work, this was an excellent find.

@knovoselic knovoselic force-pushed the active_support_concern_class_methods_fix branch from 45b3629 to f98e75a Compare June 12, 2015 15:35
@knovoselic
Copy link
Contributor Author

Thanks :) I've squashed the commits, let me know if you need anything else.

rafaelfranca added a commit that referenced this pull request Jun 12, 2015
…s_methods_fix

[ActiveSupport] Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes
@rafaelfranca rafaelfranca merged commit 8beb328 into rails:master Jun 12, 2015
rafaelfranca added a commit that referenced this pull request Jun 12, 2015
…s_methods_fix

[ActiveSupport] Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes
@rafaelfranca
Copy link
Member

Backported as d06e425

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants