Tapioca does not generate a signature for ActiveSupport's `Time#rfc3339`
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/[email protected]: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/[email protected].
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.
Looks similar to https://github.com/Shopify/tapioca/issues/640