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

Mixed in class methods detection should be best effort #252

Merged
merged 1 commit into from
Mar 9, 2021

Conversation

paracycle
Copy link
Member

Motivation

Fixes: #237

We try to do something sneaky to detect modules that are mixed into a class through inclusion of another module, where we try to infer that information from the change in the ancestor list of a test class after including the said module to that class.

However, that operation is risky since a self.included method on the target module could potentially run any Ruby code and it turns out some code in the wild even try to require optional gems when the concern is being included.

Implementation

Since this detection is best effort, it should be fine to catch all kinds of Exceptions during include and bail out of the operation without further side-effects.

Tests

Added a test to verify that we can still generate module definitions even if self.included fails.

We try to do something sneaky to detect modules that are mixed into a
class through inclusion of another module, where we try to infer that
information from the change in the ancestor list of a test class after
including the said module to that class.

However, that operation is risky since a `self.included` method on the
target module could potentially run any Ruby code and it turns out some
code in the wild even try to require optional gems when the concern is
being included.

Since this detection is best effort, it should be fine to catch
all kinds of `Exception`s and bail out of the operation without
side-effects.

Fixes: #237
@paracycle paracycle requested a review from a team March 9, 2021 22:38
Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

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

Catching Rescuing makes sense to me 👍

Copy link
Collaborator

@Morriar Morriar left a comment

Choose a reason for hiding this comment

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

Not for this pull-request but I wonder if we could monkey patch mixes_in_class_method so it register the list of receiver and mixes modules while we load the app?

@paracycle
Copy link
Member Author

Not for this pull-request but I wonder if we could monkey patch mixes_in_class_method so it register the list of receiver and mixes modules while we load the app?

That's not quite what we are doing here, though. It would have been simpler if that was the case, indeed.

What we are trying to detect here is given a random module Foo, does it have any side-effects from the include Foo operation, like extending the target class with other modules etc. Basically we are trying to detect what mixes_in_class_methods stanza we should "generate" for Foo, not how to detect the mixes_in_class_methods call in Foo (since we don't expect random gem code to have any such usage).

@paracycle paracycle merged commit 96b99ec into master Mar 9, 2021
@paracycle paracycle deleted the uk-fix-micm-detection branch March 9, 2021 22:45
mixins_from_modules[mod] = singleton_class.ancestors - before
end
rescue Exception # rubocop:disable Lint/RescueException
# this is a best effort, bail if we can't perform this

Choose a reason for hiding this comment

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

Might be nice to warn here, otherwise this "silent failure" could make future debugging difficult. @paracycle

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed in principle, but I would like to address the few places where we unconditionally and silently rescue errors similar to this into a more uniform framework. The output is already pretty noisy and I would not like to make it more noisy by surfacing these errors as is.

I had created the #238 issue to handle that in the DSL generation part of the codebase, but will most certainly extend the same thing to other parts of the codebase too. It will probably be a file-based logging operation with an indication in the UI that there might have been some errors encountered during the operation, so best to inspect the file.

@paracycle paracycle temporarily deployed to production March 9, 2021 23:07 Inactive
This was referenced Mar 10, 2021
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

Successfully merging this pull request may close these issues.

sync: LoadError: cannot load such file -- RMagick
4 participants