Shopify / Shopify/tapioca

Tapioca does not generate a signature for ActiveSupport's `Time#rfc3339`

Open
#2,082 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help-wanted
Dominant language
Ruby
Stars
873
Forks
164
Avg merge
4d 27m
Merged PRs (30d)
9

Description

I've discussed this with @andyw8 on Slack.
I'm working on a Rails application that uses Sorbet and Tapioca.
Using Time.now.rfc3339 in the app introduced a Sorbet error:

app/jobs/dead_translations/snapshot_job.rb:190: Method rfc3339 does not exist on Time https://srb.help/7003
     190 |          time: end_time.rfc3339,
                                   ^^^^^^^
  Got Time originating from:
    app/jobs/dead_translations/snapshot_job.rb:186:
     186 |    def get_live_translations(prefix, end_time)
                                                ^^^^^^^^
  There is a singleton class method with the same name:
    sorbet/rbi/gems/activesupport@8.0.0.rbi:21563: Defined here
       21563 |    def rfc3339(str); end
                  ^^^^^^^^^^^^^^^^
    Either:
    - use .class to call it, or
    - remove self. from its definition to make it an instance method
  Autocorrect: Use -a to autocorrect
    app/jobs/dead_translations/snapshot_job.rb:190: Insert .class
     190 |          time: end_time.rfc3339,

Time#rfc3339 is defined in an ActiveSupport core extension: activesupport/core_ext/time/conversions, as an alias to Time#xmlschema, which itself is defined in Ruby's Time class (here).

$ ruby -e "puts Time.now.xmlschema"
-e:1:in `<main>': undefined method `xmlschema' for an instance of Time (NoMethodError)

puts Time.now.xmlschema
             ^^^^^^^^^^

$ ruby -rtime -e "puts Time.now.xmlschema"
2024-11-22T01:16:39+09:00

$ ruby -rtime -e "puts Time.now.rfc3339"
-e:1:in `<main>': undefined method `rfc3339' for an instance of Time (NoMethodError)

puts Time.now.rfc3339
             ^^^^^^^^

$ ruby -ractive_support/core_ext/time/conversions -e "puts Time.now.rfc3339"
2024-11-22T01:17:10+09:00

What's weird is that Tapioca appears to be able to pick up another method defined with alias_method in the same active_support/core_ext/time/conversions file. (This one: to_formatted_s.)

In another experiment, I tried adding def xmlschema; end in that file, active_support/core_ext/time/conversions, before the line alias_method :rfc3339, :xmlschema. When I run bin/tapioca gem activesupport, it adds the signatures for both Time#xmlschema and Time#rfc3339 to sorbet/rbi/gems/activesupport@8.0.0.rbi.

I tried adding either or both of the two lines below in the sorbet/tapioca/require.rb file, but it does not seem to change anything, unfortunately.

require "time"
require "active_support/core_ext/time/conversions"

Also note that Sorbet defines a signature for Time#xmlschema, here.

Finally, I tried replacing Time.now.rfc3339 with Time.now.xmlschema in my code, and now the Sorbet error is gone, so I'll simply go with that for now.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with bin/tapioca gem activesupport and inspect active_support/core_ext/time/conversions, where Time#rfc3339 is aliased. Review sorbet/tapioca/require.rb and the generated activesupport RBI, comparing rfc3339 with to_formatted_s; done means the generated RBI includes the correct instance signature for Time#rfc3339.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.