From b482870173b42171efa9c35e0f92cc53644812b1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 Mar 2013 11:55:06 -0800 Subject: [PATCH] Capture stdout/stderr when loading plugins so that it doesn't just happen --- lib/vagrant.rb | 28 +++++++++++++++++++++++++--- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 7 +++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 697c8030f..171167ae0 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -42,9 +42,11 @@ if ENV["VAGRANT_LOG"] && ENV["VAGRANT_LOG"] != "" end end -require 'pathname' -require 'childprocess' require 'json' +require 'pathname' +require 'stringio' + +require 'childprocess' require 'i18n' # OpenSSL must be loaded here since when it is loaded via `autoload` @@ -166,6 +168,12 @@ module Vagrant # # @param [String] name Name of the plugin to load. def self.require_plugin(name) + # Redirect stdout/stderr so that we can output it in our own way. + previous_stderr = $stderr + previous_stdout = $stdout + $stderr = StringIO.new + $stdout = StringIO.new + # Attempt the normal require begin require name @@ -189,9 +197,23 @@ module Vagrant end end + # Get the string data out from the stdout/stderr captures + stderr = $stderr.string + stdout = $stdout.string + if !stderr.empty? || !stdout.empty? + raise Errors::PluginLoadFailedWithOutput, + :plugin => name, + :stderr => stderr, + :stdout => stdout + end + # And raise an error itself - raise Errors::PluginLoadFailed, :plugin => name + raise Errors::PluginLoadFailed, + :plugin => name end + ensure + $stderr = previous_stderr + $stdout = previous_stdout end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 474c274c1..85952de94 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -311,6 +311,10 @@ module Vagrant 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 c5e77679b..a7a5a3298 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -196,6 +196,13 @@ en: 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. + + stdout: %{stdout} + + stderr: %{stderr} plugin_not_found: |- The plugin '%{name}' could not be found. Please install this plugin prior to attempting to do anything with it.