Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tapioca automatically converts nested style to compact style, causing 'Unable to resolve constant $class' error in RBI #1861

Open
hiko1129 opened this issue Apr 10, 2024 · 1 comment

Comments

@hiko1129
Copy link

sorbet version: 0.5.11332
tapioca version: 0.13.1
ruby version: 3.2.2

I encountered an issue when using Tapioca to generate RBI files for a gem dependency. The generated RBI file contains an Unable to resolve constant $class error.

Here's a minimal reproduction of the issue:

In repo X, I have the following files:
lib/x.rb

module X
  class Foo
  end

  class Bar
    def foo
      Foo.new
    end
  end
end

rbi/defs.rbi

# typed: strong

module X
  class Foo
  end

  class Bar
    sig { returns(Foo) }
    def foo
    end
  end
end

In repo Y, which depends on the gem from repo X, I have the following files:
Gemfile

gem "x"

After running bin/tapioca gem, the generated RBI file at sorbet/rbi/gems/x.rbi looks like this:

module X; end

class X::Foo
end

class X::Bar
  sig { returns(Foo) } # Unable to resolve constant `Foo`
  def foo
  end
end

Suspect file: pipeline.rb on GitHub

@KaanOzkan
Copy link
Contributor

Thanks for the report. This is definitely annoying but I think the implementation will be too complex with too much overhead that it might not be worth it. Currently the exported gem rbis functionality is quiet simple. We parse the contents of rbi/ and merge it with generated RBI definitions. So we don't do any introspection for the contents of the rbi/ folder. For this to be automated we'd have to find the fully qualified name of each constant referenced in a sig and modify the tree.

A better solution could be encouraging gem authors to use fully qualified names while writing signatures. We could have a cop in https://github.com/shopify/rubocop-sorbet to do that but it's hard to enforce adoption of rubocop-sorbet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants