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

Fix Rails engine symbol loading #1028

Merged
merged 2 commits into from
Jul 1, 2022
Merged

Conversation

paracycle
Copy link
Member

@paracycle paracycle commented Jul 1, 2022

Motivation

Fixes #1027

Since this commit we have not been loading Rails engine symbols from gems. The commit started filtering Rails engines by looking up the name of the engine in the payload symbol table which was not the correct thing to do and would never return an engine for consideration.

What should be happening is to find the engine that is exported from a gem, and to collect all the files that belong to that engine, so that Sorbet can find the symbols.

Generally, this would not be causing a problem if all the constants loaded from an engine were under the gem's top level namespace, since we walk the namespace down and would discover those constants (since there was, hopefully, no problem with loading the engine files). If the engine was exporting a top-level constant, though, we would not be able to catch that since we never scanned the symbols. This was happening, for example, for the view_component gem's PreviewHelper module.

Implementation

Instead of memoizing engine_symbols, we are now memoizing the list of engines, and engine_symbols method takes the gem object to filter the list of engines to find the one exported by the gem.

Tests

Added a test that creates a Rails engine and defines both top-level and namespaced classes within the engine.

Since [this commit](ed9e590)
we have not been loading Rails engine symbols from gems. The commit
started filtering Rails engines by looking up the name of the engine
in the payload symbol table which was not the correct thing to do and
would never return an engine for consideration.

What should be happening is to find the engine that is exported from a
gem, and to collect all the files that belong to that engine, so that
Sorbet can find the symbols.

Generally, this would not be causing a problem if all the constants
loaded from an engine were under the gem's top level namespace, since we
walk the namespace down and would discover those constants (since there
was, hopefully, no problem with loading the engine files). If the engine
was exporting a top-level constant, though, we would not be able to catch
that since we never scanned the symbols. This was happening, for example,
for the `view_component` gem's [`PreviewHelper` module](https://github.com/github/view_component/blob/8dd9063d0b74125c0e20222ee76e64faac6d13a2/app/helpers/preview_helper.rb).
@paracycle paracycle requested a review from a team July 1, 2022 14:29
@paracycle paracycle merged commit 54163e2 into main Jul 1, 2022
@paracycle paracycle deleted the uk-fix-engine-symbol-loading branch July 1, 2022 19:03
@shopify-shipit shopify-shipit bot temporarily deployed to production July 7, 2022 17:53 Inactive
@shopify-shipit shopify-shipit bot temporarily deployed to 0-9-stable August 19, 2022 20:37 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrectly included gem helper module
2 participants