Skip to content

Commit

Permalink
Avoid excluding transitive dependencies of default dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Nov 29, 2024
1 parent bfae37d commit 444c202
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
10 changes: 4 additions & 6 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def initial_excluded_gems
end

others.concat(this_gem.to_spec.dependencies) if this_gem
others.concat(others.filter_map { |d| d.to_spec&.dependencies }.flatten)
others.uniq!
others.map!(&:name)

excluded.each do |dependency|
next unless dependency.runtime?
Expand All @@ -281,12 +284,7 @@ def initial_excluded_gems
next unless spec

spec.dependencies.each do |transitive_dependency|
# If the transitive dependency is included in other groups, skip it
next if others.any? { |d| d.name == transitive_dependency.name }

# If the transitive dependency is included as a transitive dependency of a gem outside of the development
# group, skip it
next if others.any? { |d| d.to_spec&.dependencies&.include?(transitive_dependency) }
next if others.include?(transitive_dependency.name)

excluded << transitive_dependency
end
Expand Down
42 changes: 39 additions & 3 deletions lib/ruby_indexer/test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ def test_load_configuration_executes_configure_block
@config.apply_config({ "excluded_patterns" => ["**/fixtures/**/*.rb"] })
uris = @config.indexable_uris

bundle_path = Bundler.bundle_path.join("gems")

assert(uris.none? { |uri| uri.full_path.include?("test/fixtures") })
assert(uris.none? { |uri| uri.full_path.include?("minitest-reporters") })
assert(uris.none? { |uri| uri.full_path.include?("ansi") })
assert(uris.any? { |uri| uri.full_path.include?("sorbet-runtime") })
assert(uris.none? { |uri| uri.full_path.include?(bundle_path.join("minitest-reporters").to_s) })
assert(uris.none? { |uri| uri.full_path.include?(bundle_path.join("ansi").to_s) })
assert(uris.any? { |uri| uri.full_path.include?(bundle_path.join("sorbet-runtime").to_s) })
assert(uris.none? { |uri| uri.full_path == __FILE__ })
end

Expand Down Expand Up @@ -166,5 +168,39 @@ def test_includes_top_level_files
assert(uris.find { |u| File.basename(u.full_path) == "find_me.rb" })
end
end

def test_transitive_dependencies_for_non_dev_gems_are_not_excluded
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
# Both IRB and debug depend on reline. Since IRB is in the default group, reline should not be excluded
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "irb"
gem "ruby-lsp", path: "#{Bundler.root}"
group :development do
gem "debug"
end
RUBY

Bundler.with_unbundled_env do
system("bundle install")

stdout, _stderr = capture_subprocess_io do
script = [
"require \"ruby_lsp/internal\"",
"print RubyIndexer::Configuration.new.instance_variable_get(:@excluded_gems).join(\",\")",
].join(";")
system("bundle exec ruby -e '#{script}'")
end

excluded_gems = stdout.split(",")
assert_includes(excluded_gems, "debug")
refute_includes(excluded_gems, "reline")
refute_includes(excluded_gems, "irb")
end
end
end
end
end
end
1 change: 1 addition & 0 deletions project-words
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ rhtml
rindex
rjson
rmtree
rruby
rubyfmt
rubylibdir
rubylibprefix
Expand Down

0 comments on commit 444c202

Please sign in to comment.