Skip to content
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
32 changes: 29 additions & 3 deletions bundler/lib/bundler/plugin/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,49 @@ def installed_in_plugin_root?(name)
# @param [Pathname] index file path
# @param [Boolean] is the index file global index
def load_index(index_file, global = false)
base = global ? Plugin.global_root : Plugin.root

SharedHelpers.filesystem_access(index_file, :read) do |index_f|
valid_file = index_f&.exist? && !index_f.size.zero?
break unless valid_file

data = index_f.read

require_relative "../yaml_serializer"
index = YAMLSerializer.load(data)
loaded = YAMLSerializer.load(data)
# Bundler 4.1 writes empty sections as "{}", which this serializer
# reads as a string.
index = %w[commands hooks load_paths plugin_paths sources].to_h do |key|
[key, loaded[key].is_a?(Hash) ? loaded[key] : {}]
end

@commands.merge!(index["commands"])
@hooks.merge!(index["hooks"])
@load_paths.merge!(index["load_paths"])
@plugin_paths.merge!(index["plugin_paths"])
@load_paths.merge!(absolutize_paths(index["load_paths"], base))
@plugin_paths.merge!(absolutize_paths(index["plugin_paths"], base))
@sources.merge!(index["sources"]) unless global
end
end

# Bundler 4.1 records the paths of a plugin installed in the plugin root
# relative to it.
def absolutize_paths(paths, base)
paths.transform_values do |value|
if value.is_a?(Array)
value.map {|path| absolutize_path(path, base) }
else
absolutize_path(value, base)
end
end
end

def absolutize_path(path, base)
pathname = Pathname.new(path)
return path if pathname.absolute?

Pathname.new(base).join(pathname).to_s
end

# Should be called when any of the instance variables change. Stores the
# instance variables in YAML format. (The instance variables are supposed
# to be only String key value pairs)
Expand Down
37 changes: 37 additions & 0 deletions bundler/spec/bundler/plugin/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@
new_index = Index.new
expect(new_index.load_paths(plugin_name)).to eq([lib_path(plugin_name).join("lib").to_s])
end

it "reads empty sections that Bundler 4.1 writes as {}" do
index.index_file.write(<<~YAML)
---
commands: {}
hooks: {}
load_paths:
#{plugin_name}:
- #{lib_path(plugin_name).join("lib")}
plugin_paths:
#{plugin_name}: #{lib_path(plugin_name)}
sources: {}
YAML

new_index = Index.new
expect(new_index.plugin_path(plugin_name)).to eq(lib_path(plugin_name))
expect(new_index.load_paths(plugin_name)).to eq([lib_path(plugin_name).join("lib").to_s])
end

it "expands paths that Bundler 4.1 records relative to the plugin root" do
index.index_file.write(<<~YAML)
---
commands:
hooks:
load_paths:
#{plugin_name}:
- #{plugin_name}/lib
plugin_paths:
#{plugin_name}: #{plugin_name}
sources:
YAML

plugin_root = Bundler::Plugin.root
new_index = Index.new
expect(new_index.plugin_path(plugin_name)).to eq(plugin_root.join(plugin_name))
expect(new_index.load_paths(plugin_name)).to eq([plugin_root.join(plugin_name, "lib").to_s])
end
end

describe "commands" do
Expand Down
Loading