0

语境

  • 我们的团队最近升级到 rails 6(从 5.2)。AFAIK,到目前为止,事情进展顺利。
  • 我们有一个非常规的文件结构,所以我们选择使用:classicrails loader 而不是:zeitwerk

问题

每当我们srb rbi hidden-definitions现在运行时,我们都会得到以下信息:

Got LoadError when trying to get nested name Tilt::PandocTemplate
Got LoadError when trying to get nested name Tilt::AsciidoctorTemplate
Got LoadError when trying to get nested name Tilt::CreoleTemplate
Got LoadError when trying to get nested name Tilt::LessTemplate
Got LoadError when trying to get nested name Tilt::LiquidTemplate
Got LoadError when trying to get nested name Tilt::LiveScriptTemplate
Got LoadError when trying to get nested name Tilt::MarkabyTemplate
Got LoadError when trying to get nested name Tilt::PrawnTemplate
Got LoadError when trying to get nested name Tilt::RadiusTemplate
Got LoadError when trying to get nested name Tilt::RedClothTemplate
Got LoadError when trying to get nested name Tilt::RstPandocTemplate
Got LoadError when trying to get nested name Tilt::TypeScriptTemplate
Got LoadError when trying to get nested name Tilt::WikiClothTemplate
Got LoadError when trying to get nested name Tilt::YajlTemplate
Naming WebConsoleGot NameError when trying to get nested name WebConsole::SourceLocation_
Generating /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi with 20603 modules and 305 aliases
Printing your code's symbol table into /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/from-source.json
Printing /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi's symbol table into /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.json
Traceback (most recent call last):
        5: from /usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/bin/srb-rbi:237:in `<main>'
        4: from /usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/bin/srb-rbi:224:in `main'
        3: from /usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/bin/srb-rbi:232:in `block in make_step'
        2: from /usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/lib/hidden-definition-finder.rb:38:in `main'
        1: from /usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/lib/hidden-definition-finder.rb:47:in `main'
/usr/local/var/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/sorbet-0.5.5891/lib/hidden-definition-finder.rb:151:in `write_constants': /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi had unexpected errors. Check this file for a clue: /var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.json.err (RuntimeError)

reflect.json.err(错误指向)充满了无数错误,看起来像这样:

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707623: dynamic constant assignment https://srb.help/2001
      707623 |  REFERENCE = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707624: dynamic constant assignment https://srb.help/2001
      707624 |  SETUTITSBUS = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707625: dynamic constant assignment https://srb.help/2001
      707625 |  SLAICEPS = ::T.let(nil, ::T.untyped)
                ^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707626: dynamic constant assignment https://srb.help/2001
      707626 |  SPECIALS = ::T.let(nil, ::T.untyped)
                ^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707627: dynamic constant assignment https://srb.help/2001
      707627 |  SUBSTITUTES = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707628: dynamic constant assignment https://srb.help/2001
      707628 |  VALID_CHAR = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707629: dynamic constant assignment https://srb.help/2001
      707629 |  VALID_XML_CHARS = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707618: class definition in method body https://srb.help/2001
      707618 |class REXML::Text < REXML::Child
              ^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707801: class definition in method body https://srb.help/2001
      707801 |class REXML::UndefinedNamespaceException < REXML::ParseException
              ^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707813: module definition in method body https://srb.help/2001
      707813 |module REXML::Validation
              ^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707817: class definition in method body https://srb.help/2001
      707817 |class REXML::Validation::ValidationException < RuntimeError
              ^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707829: dynamic constant assignment https://srb.help/2001
      707829 |  DEFAULT_ENCODING = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707830: dynamic constant assignment https://srb.help/2001
      707830 |  DEFAULT_STANDALONE = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^^^^^^^^

/var/folders/w4/01z4c79j22l59fhcgyl43f8w0000gn/T/d20200911-19233-vy4tg3/reflection.rbi:707831: dynamic constant assignment https://srb.help/2001
      707831 |  DEFAULT_VERSION = ::T.let(nil, ::T.untyped)
                ^^^^^^^^^^^^^^^

问题

关于如何前进的任何建议?一个预感是我们需要迁移到:zeitwerk以解决这个问题,但这对我们来说是一个相当大的重构,所以我在没有确认这是导致此失败的潜在原因的情况下犹豫是否要探索这个问题。

4

1 回答 1

3

编辑:这个补丁的一个变种现在已经被上传到 Sorbet并且将在 >= 0.5.5911 的版本中可用。对于使用这些版本的任何人,此答案不再相关。


我怀疑您看到的错误是由任何 Rails 配置引起的。您发布的行reflection.json.err也没有显示任何真正的错误。

看起来您使用的是 Ruby 2.7,所以我怀疑您看到的错误是因为在 2.7 中如何通过def foo(...)方法定义完成参数转发。在运行时这些变成def foo(**, &&)Ruby,当 Sorbet 尝试生成参数名称时,它会失败,因为*并且&不是有效的参数名称。

sorbet您可以尝试使用我在下面包含的差异来修补 gem 中的相关方法:

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

您可以使用在编辑器bundle open sorbet中打开sorbet作为您的一部分安装的 gem Gemfile,应用上面的差异并重新运行隐藏定义生成器。如果这解决了你的问题,我很乐意将它公关到上游,以便在下一个冰糕版本中得到它。

于 2020-09-12T00:51:10.103 回答