Skip to content

Commit 75e6bfc

Browse files
committed
Add useful error message for plugin load
If a plugin has previously been installed, but the path is no longer valid, `rake setup` will fail with an unexpected error due to the file not existing. Instead, we want to present the user with what the issue is and how to resolve the problem.
1 parent 6ccc6ba commit 75e6bfc

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

bundler/lib/bundler/plugin.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,30 @@ def load_plugin(name)
342342
# done to avoid conflicts
343343
path = index.plugin_path(name)
344344

345-
Gem.add_to_load_path(*index.load_paths(name))
345+
paths = index.load_paths(name)
346+
347+
all_paths_valid = true
348+
paths.each do |p|
349+
next if File.directory?(p)
350+
351+
message = <<~MESSAGE
352+
Failed loading plugin #{name}:
353+
354+
Plugin path #{p} does not exist. This can happen if the plugin was \
355+
installed with a different version of Ruby that has since been uninstalled.
356+
357+
If you would like to reinstall the plugin, run:
358+
359+
bundler plugin uninstall #{name} && bundler plugin install #{name}
360+
361+
Continuing without installing plugin #{name}.
362+
MESSAGE
363+
Bundler.ui.warn message
364+
all_paths_valid = false
365+
end
366+
return unless all_paths_valid
367+
368+
Gem.add_to_load_path(*paths)
346369

347370
load path.join(PLUGIN_FILE_NAME)
348371

bundler/spec/bundler/plugin_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,27 @@
333333
end.to output("win\n").to_stdout
334334
end
335335
end
336+
337+
context "the plugin load_path is invalid" do
338+
before do
339+
allow(index).to receive(:load_paths).with("foo-plugin").and_return(["invalid-file-name"])
340+
end
341+
342+
it "outputs a useful warning" do
343+
msg =
344+
"Failed loading plugin foo-plugin:\n\n" \
345+
"Plugin path invalid-file-name does not exist. This can happen if the plugin was " \
346+
"installed with a different version of Ruby that has since been uninstalled.\n\n" \
347+
"If you would like to reinstall the plugin, run:\n\n" \
348+
"bundler plugin uninstall foo-plugin && bundler plugin install foo-plugin\n\n" \
349+
"Continuing without installing plugin foo-plugin.\n"
350+
351+
expect(Bundler.ui).to receive(:warn).with(msg)
352+
353+
Plugin.hook(Bundler::Plugin::Events::EVENT1)
354+
355+
expect(subject.loaded?("foo-plugin")).to be_falsey
356+
end
357+
end
336358
end
337359
end

0 commit comments

Comments
 (0)