From 7c995caae54c2af709527a2c1c19908c258640bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Jan 2014 14:45:43 -0800 Subject: [PATCH] core: Catch plugin load errors and show them to the user ina friendly way --- bin/vagrant | 109 +++++++++++++++++++-------------------- lib/vagrant.rb | 8 ++- lib/vagrant/errors.rb | 8 --- templates/locales/en.yml | 15 ++---- 4 files changed, 64 insertions(+), 76 deletions(-) diff --git a/bin/vagrant b/bin/vagrant index 448803bd7..4f854235f 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -54,72 +54,69 @@ if argv.include?("--debug") ENV["VAGRANT_LOG"] = "debug" end -# Require some stuff that is NOT dependent on RubyGems -require "vagrant/shared_helpers" - # Setup our dependencies by initializing Bundler. If we're using plugins, # then also initialize the paths to the plugins. require "bundler" Bundler.setup -require 'log4r' -require 'vagrant' -require 'vagrant/cli' -require 'vagrant/util/platform' - -# Create a logger right away -logger = Log4r::Logger.new("vagrant::bin::vagrant") -logger.info("`vagrant` invoked: #{ARGV.inspect}") - # Stdout/stderr should not buffer output $stdout.sync = true $stderr.sync = true -# These will be the options that are passed to initialze the Vagrant -# environment. -opts = {} - -# Disable color in a few cases: -# -# * --no-color is anywhere in our arguments -# * STDOUT is not a TTY -# * The terminal doesn't support colors (Windows) -# -if argv.include?("--no-color") || ENV["VAGRANT_NO_COLOR"] - # Delete the argument from the list so that it doesn't - # cause any invalid arguments down the road. - argv.delete("--no-color") - - opts[:ui_class] = Vagrant::UI::Basic -elsif !Vagrant::Util::Platform.terminal_supports_colors? - opts[:ui_class] = Vagrant::UI::Basic -elsif !$stdout.tty? && !Vagrant::Util::Platform.cygwin? - # Cygwin always reports STDOUT is not a TTY, so we only disable - # colors if its not a TTY AND its not Cygwin. - opts[:ui_class] = Vagrant::UI::Basic -end - -# Also allow users to force colors. -if argv.include?("--color") - argv.delete("--color") - opts[:ui_class] = Vagrant::UI::Colored -end - -# Highest precedence is if we have enabled machine-readable output -if argv.include?("--machine-readable") - argv.delete("--machine-readable") - opts[:ui_class] = Vagrant::UI::MachineReadable -end - -# Default to colored output -opts[:ui_class] ||= Vagrant::UI::Colored - -# Recombine the arguments -argv << "--" -argv += argv_extra - env = nil begin + require 'log4r' + require 'vagrant' + require 'vagrant/cli' + require 'vagrant/util/platform' + + # Create a logger right away + logger = Log4r::Logger.new("vagrant::bin::vagrant") + logger.info("`vagrant` invoked: #{ARGV.inspect}") + + # These will be the options that are passed to initialze the Vagrant + # environment. + opts = {} + + # Disable color in a few cases: + # + # * --no-color is anywhere in our arguments + # * STDOUT is not a TTY + # * The terminal doesn't support colors (Windows) + # + if argv.include?("--no-color") || ENV["VAGRANT_NO_COLOR"] + # Delete the argument from the list so that it doesn't + # cause any invalid arguments down the road. + argv.delete("--no-color") + + opts[:ui_class] = Vagrant::UI::Basic + elsif !Vagrant::Util::Platform.terminal_supports_colors? + opts[:ui_class] = Vagrant::UI::Basic + elsif !$stdout.tty? && !Vagrant::Util::Platform.cygwin? + # Cygwin always reports STDOUT is not a TTY, so we only disable + # colors if its not a TTY AND its not Cygwin. + opts[:ui_class] = Vagrant::UI::Basic + end + + # Also allow users to force colors. + if argv.include?("--color") + argv.delete("--color") + opts[:ui_class] = Vagrant::UI::Colored + end + + # Highest precedence is if we have enabled machine-readable output + if argv.include?("--machine-readable") + argv.delete("--machine-readable") + opts[:ui_class] = Vagrant::UI::MachineReadable + end + + # Default to colored output + opts[:ui_class] ||= Vagrant::UI::Colored + + # Recombine the arguments + argv << "--" + argv += argv_extra + # Create the environment, which is the cwd of wherever the # `vagrant` command was invoked from logger.debug("Creating Vagrant environment") @@ -141,6 +138,8 @@ begin # Exit with the exit status from our CLI command exit(exit_status) rescue Vagrant::Errors::VagrantError => e + require 'log4r' + logger = Log4r::Logger.new("vagrant::bin::vagrant") logger.error("Vagrant experienced an error! Details:") logger.error(e.inspect) logger.error(e.message) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index a2a6e7400..e92c2afb9 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -262,4 +262,10 @@ Vagrant.source_root.join("plugins").children(true).each do |directory| end # If we have plugins enabled, then load those -Bundler.require(:plugins) if Vagrant.plugins_enabled? +if Vagrant.plugins_enabled? + begin + Bundler.require(:plugins) + rescue Exception => e + raise Vagrant::Errors::PluginLoadError, message: e.to_s + end +end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 40777581f..814522b02 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -481,14 +481,6 @@ module Vagrant error_key(:plugin_load_error) end - class PluginLoadFailed < VagrantError - error_key(:plugin_load_failed) - end - - class PluginLoadFailedWithOutput < VagrantError - error_key(:plugin_load_failed_with_output) - end - class PluginNotFound < VagrantError error_key(:plugin_not_found) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a30bdd3b0..3edd28a23 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -531,19 +531,10 @@ en: %{conflicts} plugin_load_error: |- - The plugin "%{plugin}" could not be found. Please make sure that it is - properly installed via `vagrant plugin`. Note that plugins made for - Vagrant 1.0.x are not compatible with 1.1+ and this error will likely - continue to show when you use `plugin install` with a 1.0.x plugin. - plugin_load_failed: |- - Failed to load the "%{plugin}" plugin. View logs for more details. - plugin_load_failed_with_output: |- - Failed to load the "%{plugin}" plugin. The output from loading - the plugin is shown below. View the logs for complete details. + The plugins failed to load properly. The error message given is + shown below. - stdout: %{stdout} - - stderr: %{stderr} + %{message} plugin_not_found: |- The plugin '%{name}' could not be found. Please install this plugin prior to attempting to do anything with it.