Skip to content

Commit b20b941

Browse files
hsbtclaude
andcommitted
Relativize a plugin path only when it is really under the root
The save side matched the root as a string prefix, so an absolute path that starts with the root but climbs above it was written back relative, and the filter above then dropped it on the next load. Asking the same question both ways leaves it absolute and loadable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 6a310ea commit b20b941

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

lib/bundler/plugin/index.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def escaping_plugins(index, base)
231231
end
232232

233233
# Expanded here, not by the caller: what gets stored stays joined, because
234-
# the rest of the class matches it against Plugin.root as written.
234+
# #installed_in_plugin_root? matches it against Plugin.root as written.
235235
def contained_in?(path, base)
236236
path = File.expand_path(path)
237237
base = File.expand_path(base)
@@ -264,12 +264,9 @@ def relativize_path(path, base)
264264
pathname = Pathname.new(path)
265265
return path unless pathname.absolute?
266266

267-
base_path = Pathname.new(base)
268-
if pathname == base_path || pathname.to_s.start_with?(base_path.to_s + File::SEPARATOR)
269-
pathname.relative_path_from(base_path).to_s
270-
else
271-
path
272-
end
267+
return path unless contained_in?(pathname.to_s, base)
268+
269+
pathname.relative_path_from(Pathname.new(base)).to_s
273270
end
274271

275272
def absolutize_path(path, base)

spec/bundler/plugin/index_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@
277277
expect(new_index.load_paths(plugin_name)).to eq([plugin_root.join("~nosuchuser", "lib").to_s])
278278
end
279279

280+
it "keeps an absolute path that only looks like it is under the plugin root" do
281+
escaping_path = File.join(Bundler::Plugin.root.to_s, "..", "..", "escaping-plugin")
282+
283+
index.register_plugin("escaping-plugin", escaping_path, [File.join(escaping_path, "lib")], [], [], [])
284+
285+
new_index = Index.new
286+
expect(new_index.installed?("escaping-plugin")).to eq(escaping_path)
287+
end
288+
280289
it "keeps paths outside the plugin root as absolute" do
281290
outside_path = tmp.join("outside", "external-plugin")
282291
FileUtils.mkdir_p(outside_path.join("lib"))

0 commit comments

Comments
 (0)