What Is the Directory Structure for Adding Sorbet Rbis to a Gem

Does Sorbet add signatures for standard lib?

Yes, Sorbet does include types for standard library of Ruby.
For example, here's the type annotation for sort!
https://github.com/sorbet/sorbet/blob/119e937e9e3b03ec27308cd8874f482791d15864/rbi/core/array.rbi#L670-L677

Note that in Ruby sort! does return a value and Sorbet correctly models that.

Adding Sorbet type signatures to attr_reader/attr_writer with multiple variables

Yes, the only option is to split up your attribute declarations, just like how you would have done if you were defining separate getter/setter methods for them, unless all your attributes are of the same type.

The reason for this is that Sorbet, in the DSL phase of its operation, actually uses the sig on an attr_reader/attr_writer/attr_accessor declaration to define the sig on the synthetic methods that are produced by those declarations. Thus, a single getter for attr_reader, a single setter for attr_writer and a getter/setter pair for attr_accessor are generated synthetically and the sigs are applied to them.

As a result of this, this would be valid:

sig { returns(String) }
attr_reader :some_string_attr, :other_string_attr

but this would not be:

sig { returns(String, User) }
attr_reader :some_string_attr, :some_user_attr

Running srb rbi hidden-definitions gives unclear error

EDIT: A variant of this patch has now been upstreamed into Sorbet and will be available in versions >= 0.5.5911. For anyone using those versions, this answer is no longer relevant.


I doubt that the error you are seeing is caused by any Rails configuration. The lines that you have posted from reflection.json.err do not show any real errors, either.

It looks like you are on Ruby 2.7, so I suspect that the error you are seeing is because of how the argument forwarding is done in 2.7 via def foo(...) method definitions. At runtime these turn into def foo(**, &&) in Ruby, and when Sorbet tries to generate the argument names, it fails, since * and & are not valid argument names.

You can try to patch the relevant method in sorbet gem using the diff I include below:

diff --git a/lib/serialize.rb b/lib/serialize.rb
index aec19ccb2..301793856 100644
--- a/lib/serialize.rb
+++ b/lib/serialize.rb
@@ -337,6 +337,14 @@ class Sorbet::Private::Serialize
uniq += 1
end
end
+
+ # Sanitize parameter names that are not valid
+ name = name.to_s.gsub(/[^a-zA-Z0-9_]/) do
+ result = '_' + (uniq == 0 ? '' : uniq.to_s)
+ uniq += 1
+ result
+ end
+
[kind, name]
end
end

You can use bundle open sorbet to open the sorbet gem installed as part of your Gemfile in your editor, apply the diff above and re-run the hidden definitions generator. If that solves your problem, I'd be happy to PR it upstream to get it in the next sorbet release.

Is it safe to exclude the Sorbet errors.txt from your git repo?

Yes, it's safe to be ignored. As you mention, it's used to debug when things go wrong.



Related Topics



Leave a reply



Submit