Repository navigation
Fixes SystemStackError for has_many assocation that points to own model - #2736
Merged
paracycle merged 2 commits intoOct 6, 2026
Conversation
DaneHarrison
marked this pull request as ready for review
October 6, 2026 00:11
paracycle
approved these changes
Oct 6, 2026
paracycle
left a comment
Member
There was a problem hiding this comment.
Thanks for the detailed analysis and the fix. This implementation makes sense to me.
Contributor
Author
|
Thanks for reviewing and merging! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #1883 - Tapioca crashes with a
SystemStackErrorwhen ahas_many :throughassociation ends up being its own source.The association in the issue doesn't set
source:, sochild_group_membersresolves to itself as the source and keeps looping. This also happens without Tapioca, in a standalone Active Record script and in the demo app withrails runner, as soon aschild_group_membersis called. Rails looked fine in the demo because the app only used thechild_membersmethod and never called the association. Addingsource: :membersfixed it.The demo repo from the ticket can be found here
Script that replicates SystemStackError in rails
Script output
Implementation
Since the association can't work as written, I treated it as a configuration issue that Tapioca surfaces as an error rather than falling back to
T.untyped.I added a check that raises
SelfReferentialSourceReflectionErrorwhen the source reflection is the association itself, before.klassis called and starts recursing.The new error is rescued in
populate_associationsand reported the same way as the missing source case, so the association is skipped and the rest of the RBI is still generated.Further Considerations:
DFS cycle detection approach
Tests
has_many :throughassociation whose source is itself: the association is left out of the RBI and the error is reported