Skip to content

Commit

Permalink
Merge pull request #818 from Shopify/uk-at-less-require
Browse files Browse the repository at this point in the history
Need less entries in `require.rb`
  • Loading branch information
paracycle committed Mar 3, 2022
2 parents 4754fc1 + ee595f6 commit 83a55f6
Show file tree
Hide file tree
Showing 16 changed files with 5,182 additions and 456 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/commands/gem.rb
Expand Up @@ -124,7 +124,7 @@ def loader

sig { returns(Gemfile) }
def bundle
@bundle ||= Gemfile.new
@bundle ||= Gemfile.new(@exclude)
end

sig { void }
Expand Down
49 changes: 47 additions & 2 deletions lib/tapioca/gemfile.rb
Expand Up @@ -16,6 +16,50 @@ class Gemfile
)
end

# This is a module that gets prepended to `Bundler::Dependency` and
# makes sure even gems marked as `require: false` are required during
# `Bundler.require`.
module AutoRequireHook
extend T::Sig
extend T::Helpers

requires_ancestor { ::Bundler::Dependency }

@exclude = T.let([], T::Array[String])

class << self
extend T::Sig

sig { params(exclude: T::Array[String]).returns(T::Array[String]) }
attr_writer :exclude

sig { params(name: T.untyped).returns(T::Boolean) }
def excluded?(name)
@exclude.include?(name)
end
end

sig { returns(T.untyped).checked(:never) }
def autorequire
value = super

# If the gem is excluded, we don't want to force require it, in case
# it has side-effects users don't want. For example, `fakefs` gem, if
# loaded, takes over filesystem operations.
return value if AutoRequireHook.excluded?(name)

# If a gem is marked as `require: false`, then its `autorequire`
# value will be `[]`. But, we want those gems to be loaded for our
# purposes as well, so we return `nil` in those cases, instead, which
# means `require: true`.
return nil if value == []

value
end

::Bundler::Dependency.prepend(self)
end

sig { returns(Bundler::Definition) }
attr_reader(:definition)

Expand All @@ -25,8 +69,9 @@ class Gemfile
sig { returns(T::Array[String]) }
attr_reader(:missing_specs)

sig { void }
def initialize
sig { params(exclude: T::Array[String]).void }
def initialize(exclude)
AutoRequireHook.exclude = exclude
@gemfile = T.let(File.new(Bundler.default_gemfile), File)
@lockfile = T.let(File.new(Bundler.default_lockfile), File)
@definition = T.let(Bundler::Dsl.evaluate(gemfile, lockfile, {}), Bundler::Definition)
Expand Down
243 changes: 0 additions & 243 deletions sorbet/rbi/gems/activemodel-serializers-xml@1.0.2.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions sorbet/rbi/gems/activemodel@6.1.4.6.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83a55f6

Please sign in to comment.