Skip to content

Skip RBS rewrite when no markers are present - #2616

Closed
mattkubej wants to merge 1 commit into
mainfrom
rbs-marker-guard
Closed

Skip RBS rewrite when no markers are present#2616
mattkubej wants to merge 1 commit into
mainfrom
rbs-marker-guard

Conversation

@mattkubej

@mattkubej mattkubej commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Avoid running Spoom's RBS-to-Sorbet-sig translator for typed Ruby files that cannot contain runtime-rewriteable RBS syntax.

Tapioca currently calls Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs for every typed Ruby file loaded through the RBS rewriter. A large portion of typed files in Core do not contain RBS signatures or supported RBS annotations, so the translator often parses source only to return it unchanged.

This keeps the existing typed: gate and adds a conservative marker gate. Translation now runs only when source contains one of:

  • #:
  • #|
  • # @abstract
  • # @interface
  • # @sealed
  • # @final
  • # @requires_ancestor:
  • # @override
  • # @overridable
  • # @without_runtime

False positives are safe because they still use the existing translator. False negatives would be unsafe, so the annotation list is intentionally aligned with Spoom's currently supported runtime-rewrite annotations.

@mattkubej
mattkubej force-pushed the rbs-marker-guard branch from c06244a to 3ad4466 Compare May 7, 2026 23:24
@mattkubej mattkubej added the enhancement New feature or request label May 8, 2026
@mattkubej mattkubej mentioned this pull request May 8, 2026
@mattkubej
mattkubej marked this pull request as ready for review May 11, 2026 16:16
@mattkubej
mattkubej requested a review from a team as a code owner May 11, 2026 16:16
Comment on lines +63 to +68
def possible_rbs_runtime_rewrite_syntax?(source)
return true if source.include?("#:") || source.include?("#|")
return false unless source.include?("# @")

RBS_ANNOTATION_MARKERS.any? { |marker| source.include?(marker) }
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be scanning the source string multiple times, can we turn the search into a Regexp.union and do a single scan through the source?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think something like:

SEARCH_REGEXP = Regexp.union(TYPED_FILE_PATTERN, Regexp.escape("#:"), Regexp.escape("#|"), *RBS_ANNOTATION_MARKERS.map { Regexp.escape(it) })

def should_rewrite?
  source.index(SEARCH_REGEXP)
end

should give you the decision to rewrite or not with a single pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call on the redundant scans. I just pushed an amend.

However, I kept typed_file? as a separate stage rather than folding into the union. A single union would introduce OR semantics, so a typed file with no markers would match and I think we'd want to avoid the expensive Spoom translator execution in this case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, right, I forgot that the typed check wasn't being OR'ed. Good call.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

heh I had the same suggestion here: https://github.com/shop/world/pull/675510/changes#r3220064805

Is that other pull request just a PoC that's replaced by this one?

@paracycle paracycle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me, but have one question

Comment thread lib/tapioca/rbs/rewriter.rb
@KaanOzkan
KaanOzkan requested a review from amomchilov May 12, 2026 13:15
return unless typed_file?(source)
return source unless possible_rbs_runtime_rewrite_syntax?(source)

Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs(source, file: path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't see any reason why somebody might want Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs without your early-return optimization. Would it be better to move this optimization into Spoom itself?

@amomchilov

Copy link
Copy Markdown
Contributor

Thank you! We'll move this logic right into Spoom Shopify/spoom#916

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants