Fix the declared return type of RustCodeOwners.for_file - #176
Merged
Conversation
The Rust extension serializes a `Team` struct with three fields — `team_name` and `team_config_yml` (both `String`) plus `reasons` (`Vec<String>`) — so the `T::Hash[Symbol, String]` declared for it in `TeamFinder.for_file` and `CodeOwnership.for_file_verbose` never matched: `reasons` is an Array. Sorbet's runtime only checks the outer class of a collection type, so `T::Hash[Symbol, String]` was satisfied by any Hash and the mismatch went unnoticed. It surfaces as soon as a host application makes `valid?` descend into elements (`recursively_valid?`), where every `for_file` call raises. Replace both with a shape matching the struct. `team_name` is a non-optional Rust `String`, so it can never serialize to nil: the `result[:team_name].nil?` branch and its paired `T.must` are now dead, and the stubs in the specs return the whole struct rather than a `team_name`-only hash.
dkisselev
approved these changes
Aug 11, 2026
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.
The bug
RustCodeOwners.for_filereturns a serialized copy of the RustTeamstruct:Two places declare that as
T::Hash[Symbol, String], which never matched, becausereasonsis an Array:CodeOwnership::Private::TeamFinder.for_file—T.let(RustCodeOwners.for_file(file_path), T.nilable(T::Hash[Symbol, String]))CodeOwnership.for_file_verbose— declaredreturns(T.nilable(T::Hash[Symbol, String]))It went unnoticed because
sorbet-runtimeonly checks the outer class of a collection type:T::Hash[Symbol, String].valid?isobj.is_a?(Hash), so any Hash passes and the value types are never looked at.How it surfaced
Enabling recursive runtime typechecking in a host application — pointing each collection type's
valid?at its existingrecursively_valid?, as Homebrew/brew#20644 does — makes everyfor_filecall raise:In our monolith that's 238 raises during boot alone, via
CodeOwnership.for_classin an observability initializer.The fix
Declare the actual shape, shared by both call sites:
team_nameis a non-optional RustString, so it can never serialize to nil. That makes theresult.nil? || result[:team_name].nil?guard and its pairedT.mustdead code, and the shape now enforces what the guard was defending against — both are dropped.Specs
The stubs returned
{ team_name: 'Bar' }, which the shape correctly rejects; they now return the whole struct.caches nil when team_name is nilcovered a state the extension cannot produce and the type no longer admits, so it's removed —caches nil when rust returns nilstill covers the nil path.bundle exec rspec→ 91 examples, 0 failures, 1 pending.bundle exec srb tc→ clean.