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

Load dependencies on -r option also #1013

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ task :validate => :compile do

FileList["stdlib/*"].each do |path|
lib = [File.basename(path).to_s]
if File.exist?("#{path}/0/manifest.yaml")
YAML.load_file("#{path}/0/manifest.yaml")["dependencies"].each do |dep|
lib << dep["name"]
end
end

sh "#{ruby} #{rbs} #{lib.map {|l| "-r #{l}"}.join(" ")} validate --silent"
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rbs/collection/config/lockfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def generate

upsert_gem specified, locked
source = Sources.from_config_entry(locked['source'])
manifest = source.manifest_of(locked) or return
manifest['dependencies']&.each do |dep|
source.dependencies_of(locked)&.each do |dep|
@gem_queue.push({ name: dep['name'], version: nil} )
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rbs/collection/sources.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative './sources/base'
require_relative './sources/git'
require_relative './sources/stdlib'
require_relative './sources/rubygems'
Expand Down
12 changes: 12 additions & 0 deletions lib/rbs/collection/sources/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module RBS
module Collection
module Sources
module Base
def dependencies_of(config_entry)
manifest = manifest_of(config_entry) or return
manifest['dependencies']
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rbs/collection/sources/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module RBS
module Collection
module Sources
class Git
include Base
METADATA_FILENAME = '.rbs_meta.yaml'

class CommandError < StandardError; end
Expand Down
1 change: 1 addition & 0 deletions lib/rbs/collection/sources/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Collection
module Sources
# Signatures that are inclduded in gem package as sig/ directory.
class Rubygems
include Base
include Singleton

def has?(config_entry)
Expand Down
1 change: 1 addition & 0 deletions lib/rbs/collection/sources/stdlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Collection
module Sources
# signatures that are bundled in rbs gem under the stdlib/ directory
class Stdlib
include Base
include Singleton

REPO = Repository.default
Expand Down
24 changes: 20 additions & 4 deletions lib/rbs/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@ def initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new)
@core_root = core_root
@repository = repository

@libs = []
@libs = Set.new
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary to avoid an infinite loop.

I know EnvironmentLoader also checks duplication in each_decl, but this change is also necessary to avoid an infinite loop during add phase.

@dirs = []
end

def add(path: nil, library: nil, version: nil)
def add(path: nil, library: nil, version: nil, resolve_dependencies: true)
case
when path
dirs << path
when library
libs << Library.new(name: library, version: version)
if libs.add?(Library.new(name: library, version: version)) && resolve_dependencies
resolve_dependencies(library: library, version: version)
end
end
end

def resolve_dependencies(library:, version:)
[Collection::Sources::Rubygems.instance, Collection::Sources::Stdlib.instance].each do |source|
# @type var gem: { 'name' => String, 'version' => String? }
gem = { 'name' => library, 'version' => version }
next unless source.has?(gem)

gem['version'] ||= source.versions(gem).last
source.dependencies_of(gem)&.each do |dep|
add(library: dep['name'], version: nil)
end
return
end
end

Expand All @@ -53,7 +69,7 @@ def add_collection(collection_config)
repository.add(collection_config.repo_path)

collection_config.gems.each do |gem|
add(library: gem['name'], version: gem['version'])
add(library: gem['name'], version: gem['version'], resolve_dependencies: false)
end
end

Expand Down
12 changes: 12 additions & 0 deletions sig/collection/sources.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module RBS
def install: (dest: Pathname, config_entry: Config::gem_entry, stdout: CLI::_IO) -> void
def to_lockfile: () -> source_entry
def manifest_of: (Config::gem_entry) -> manifest_entry?
def dependencies_of: (Config::gem_entry) -> Array[{"name" => String}]?
end

type source_entry = Git::source_entry
Expand All @@ -18,7 +19,13 @@ module RBS
"dependencies" => Array[{"name" => String}]?,
}

module Base : _Source
def dependencies_of: (Config::gem_entry config_entry) -> Array[{"name" => String}]?
end

class Git
include Base

METADATA_FILENAME: String

type source_entry = {
Expand Down Expand Up @@ -77,8 +84,11 @@ module RBS

# signatures that are bundled in rbs gem under the stdlib/ directory
class Stdlib

REPO: Repository

include Base

type source_entry = {
'type' => 'stdlib',
}
Expand All @@ -103,6 +113,8 @@ module RBS

# sig/ directory
class Rubygems
include Base

type source_entry = {
'type' => 'rubygems',
}
Expand Down
6 changes: 4 additions & 2 deletions sig/environment_loader.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module RBS
attr_reader core_root: Pathname?
attr_reader repository: Repository

attr_reader libs: Array[Library]
attr_reader libs: Set[Library]
attr_reader dirs: Array[Pathname]

# The source where the RBS comes from.
Expand Down Expand Up @@ -76,7 +76,9 @@ module RBS
# If RBS files cannot be found in the gem, it tries to load RBSs from repository.
#
def add: (path: Pathname) -> void
| (library: String, version: String?) -> void
| (library: String, version: String?, ?resolve_dependencies: boolish) -> void

def resolve_dependencies: (library: String, version: String?) -> void

# Add repository path and libraries via rbs_collection.lock.yaml.
def add_collection: (Collection::Config collection_config) -> void
Expand Down
14 changes: 14 additions & 0 deletions test/rbs/environment_loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ def test_loading_from_gem_without_rbs
end
end

def test_loading_dependencies
mktmpdir do |path|
loader = EnvironmentLoader.new
loader.add(library: "yaml")

env = Environment.new
loader.load(env: env)

assert_operator env.class_decls, :key?, TypeName("::YAML")
assert_operator env.class_decls, :key?, TypeName("::DBM")
assert_operator env.class_decls, :key?, TypeName("::PStore")
end
end

def test_loading_from_rbs_collection
mktmpdir do |path|
lockfile_path = path.join('rbs_collection.lock.yaml')
Expand Down