Skip to content

Commit

Permalink
Merge pull request #1319 from ruby/gemspec-dependencies
Browse files Browse the repository at this point in the history
Skip loading `gemspec` gems via RBS collection
  • Loading branch information
soutaro authored Apr 26, 2023
2 parents c95081d + 1b6e902 commit ca435f9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 30 deletions.
60 changes: 31 additions & 29 deletions lib/rbs/collection/config/lockfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def generate
end

if spec = gem_hash[dep.name]
assign_gem(name: dep.name, version: spec.version, ignored_gems: ignored_gems, src_data: nil)
assign_gem(name: dep.name, version: spec.version, ignored_gems: ignored_gems, src_data: nil, skip: dep.source.is_a?(Bundler::Source::Gemspec))
end
end

Expand All @@ -91,43 +91,45 @@ def generate
end
end

private def assign_gem(name:, version:, src_data:, ignored_gems:)
private def assign_gem(name:, version:, src_data:, ignored_gems:, skip: false)
return if ignored_gems.include?(name)
return if lockfile.gems.key?(name)

# @type var locked: Lockfile::library?
unless skip
# @type var locked: Lockfile::library?

if existing_lockfile
locked = existing_lockfile.gems[name]
end
if existing_lockfile
locked = existing_lockfile.gems[name]
end

# If rbs_collection.lock.yaml contain the gem, use it.
# Else find the gem from gem_collection.
unless locked
source =
if src_data
Sources.from_config_entry(src_data, base_directory: config.config_path.dirname)
else
find_source(name: name)
# If rbs_collection.lock.yaml contain the gem, use it.
# Else find the gem from gem_collection.
unless locked
source =
if src_data
Sources.from_config_entry(src_data, base_directory: config.config_path.dirname)
else
find_source(name: name)
end

if source
installed_version = version
best_version = find_best_version(version: installed_version, versions: source.versions(name))

locked = {
name: name,
version: best_version.to_s,
source: source,
}
end

if source
installed_version = version
best_version = find_best_version(version: installed_version, versions: source.versions(name))

locked = {
name: name,
version: best_version.to_s,
source: source,
}
end
end

if locked
lockfile.gems[name] = locked
if locked
lockfile.gems[name] = locked

locked[:source].dependencies_of(locked[:name], locked[:version])&.each do |dep|
assign_stdlib(name: dep["name"], from_gem: name)
locked[:source].dependencies_of(locked[:name], locked[:version])&.each do |dep|
assign_stdlib(name: dep["name"], from_gem: name)
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion sig/collection/config/lockfile_generator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ module RBS

# Inserts a entry to lockfile of a gem and its dependencies, if not included in `ignored_gems:`
#
def assign_gem: (name: String, version: String?, src_data: Sources::source_entry?, ignored_gems: Set[String]) -> void
# * If `skip:` is true, it skips adding the gem, but adds it's dependencies.
#
def assign_gem: (name: String, version: String?, src_data: Sources::source_entry?, ignored_gems: Set[String], ?skip: bool) -> void

def assign_stdlib: (name: String, from_gem: String?) -> void

Expand Down
2 changes: 2 additions & 0 deletions sig/shims/bundler.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Bundler

class Dependency < Gem::Dependency
attr_reader autorequire: Array[String]?

attr_reader source: untyped
end

class Definition
Expand Down
67 changes: 67 additions & 0 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,73 @@ def test_collection_update
end
end

def test_collection_install_gemspec
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
dir = Pathname(dir)
dir.join(RBS::Collection::Config::PATH).write(<<~YAML)
sources:
- name: ruby/gem_rbs_collection
remote: https://github.com/ruby/gem_rbs_collection.git
revision: b4d3b346d9657543099a35a1fd20347e75b8c523
repo_dir: gems
path: #{dir.join('gem_rbs_collection')}
YAML
dir.join('Gemfile').write(<<~GEMFILE)
source 'https://rubygems.org'
gemspec
GEMFILE
dir.join('Gemfile.lock').write(<<~LOCK)
PATH
remote: .
specs:
hola (0.0.0)
ast (>= 2)
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
PLATFORMS
arm64-darwin-22
DEPENDENCIES
hola!
BUNDLED WITH
2.2.0
LOCK
gemspec_path = dir / "hola.gemspec"
gemspec_path.write <<~RUBY
Gem::Specification.new do |s|
s.name = "hola"
s.version = "0.0.0"
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Nick Quaranto"]
s.email = "nick@quaran.to"
s.files = ["lib/hola.rb", "sig/hola.rbs"]
s.homepage =
"https://rubygems.org/gems/hola"
s.license = "MIT"
s.add_dependency "ast", "> 2"
end
RUBY
(dir/"sig").mkdir

stdout, _ = run_rbs("collection", "install", bundler: true)

assert_match /^Installing ast:(\d(\.\d)*)/, stdout
refute_match /^Using hola:(\d(\.\d)*)/, stdout

assert dir.join('rbs_collection.lock.yaml').exist?
assert dir.join('gem_rbs_collection/ast').exist?
end
end
end

def assert_rbs_test_no_errors cli, dir, arg_array
args = ['-I', dir.to_s, 'test', *arg_array]
assert_instance_of Process::Status, cli.run(args)
Expand Down

0 comments on commit ca435f9

Please sign in to comment.