Skip to content

Commit

Permalink
Merge pull request #1731 from ruby/gem-rbs-load
Browse files Browse the repository at this point in the history
Allow loading RBS from different version of a gem
  • Loading branch information
soutaro committed Jan 24, 2024
2 parents c63d959 + 9de6aec commit 0e238dc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/rbs/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ def add_collection(lockfile)
repository.add(lockfile.fullpath)

lockfile.gems.each_value do |gem|
add(library: gem[:name], version: gem[:version], resolve_dependencies: false)
name = gem[:name]
locked_version = gem[:version]

if (source = gem[:source]).is_a?(Collection::Sources::Rubygems)
# allow loading different version of a gem

unless source.has?(name, locked_version)
if (spec, _ = self.class.gem_sig_path(name, nil))
RBS.logger.warn { "Loading type definition from gem `#{name}-#{spec.version}` because locked version `#{locked_version}` is unavailable. Try `rbs collection update` to fix the (potential) issue." }
locked_version = spec.version.to_s
end
end
end

add(library: gem[:name], version: locked_version, resolve_dependencies: false)
end
end

Expand Down
42 changes: 42 additions & 0 deletions test/rbs/environment_loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,48 @@ def test_loading_from_rbs_collection
end
end

def test_loading_from_rbs_collection__gem_version_mismatch
mktmpdir do |path|
lockfile_path = path.join('rbs_collection.lock.yaml')
lockfile_path.write(<<~YAML)
sources:
- name: ruby/gem_rbs_collection
remote: https://github.com/ruby/gem_rbs_collection.git
revision: b4d3b346d9657543099a35a1fd20347e75b8c523
repo_dir: gems
path: '.gem_rbs_collection'
gems:
- name: rbs-amber
version: "1.1"
source:
type: "rubygems"
YAML
RBS::Collection::Installer.new(lockfile_path: lockfile_path, stdout: StringIO.new).install_from_lockfile
lock = RBS::Collection::Config::Lockfile.from_lockfile(lockfile_path: lockfile_path, data: YAML.load_file(lockfile_path))

repo = RBS::Repository.new()

loader = EnvironmentLoader.new(repository: repo)

io = StringIO.new
old_output = RBS.logger_output
RBS.logger_output = io
begin
loader.add_collection(lock)
env = Environment.new
loader.load(env: env)
ensure
RBS.logger_output = old_output
end

assert_operator(
io.string,
:include?,
"Loading type definition from gem `rbs-amber-1.0.0` because locked version `1.1` is unavailable. Try `rbs collection update` to fix the (potential) issue."
)
end
end

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

0 comments on commit 0e238dc

Please sign in to comment.