diff --git a/habitat/plan.sh b/habitat/plan.sh index 11445a80..52393bed 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -69,7 +69,8 @@ wrap_ruby_bin() { set -e # Set binary path that allows InSpec to use non-Hab pkg binaries -export PATH="/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:$pkg_prefix/vendor/bin:\$PATH" +# Include Ruby bin directory so chef-cli exec can find gem, etc. +export PATH="$(pkg_path_for ${ruby_pkg})/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:$pkg_prefix/vendor/bin:\$PATH" # Set Ruby paths defined from 'do_setup_environment()' export GEM_HOME="$pkg_prefix/vendor" diff --git a/lib/chef-cli/helpers.rb b/lib/chef-cli/helpers.rb index b2277d64..0eb23b37 100644 --- a/lib/chef-cli/helpers.rb +++ b/lib/chef-cli/helpers.rb @@ -173,10 +173,12 @@ def habitat_env(show_warning: false) raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix vendor_dir = File.join(vendor_pkg_prefix, "vendor") - # Construct PATH + # Construct PATH including Ruby bin directory for chef-cli exec command + ruby_bin_dir = File.dirname(RbConfig.ruby) path = [ File.join(bin_pkg_prefix, "bin"), File.join(vendor_dir, "bin"), + ruby_bin_dir, # Add Ruby bin directory so exec can find gem etc. ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH ].flatten.uniq diff --git a/spec/unit/helpers_spec.rb b/spec/unit/helpers_spec.rb index b2280a3f..bd0f2bc7 100644 --- a/spec/unit/helpers_spec.rb +++ b/spec/unit/helpers_spec.rb @@ -112,8 +112,9 @@ context "when installed with habitat" do let(:chef_dke_path) { "/hab/pkgs/chef/chef-workstation/1.0.0/123" } let(:cli_hab_path) { "/hab/pkgs/chef/chef-cli/1.0.0/123" } + let(:ruby_bin_dir) { File.dirname(RbConfig.ruby) } let(:expected_gem_root) { Gem.default_dir } - let(:expected_path) { [File.join(chef_dke_path, "bin"), File.join(cli_hab_path, "vendor", "bin"), "/usr/bin:/bin"].flatten } + let(:expected_path) { [File.join(chef_dke_path, "bin"), File.join(cli_hab_path, "vendor", "bin"), ruby_bin_dir, "/usr/bin:/bin"].flatten } let(:expected_env) do { "PATH" => expected_path.join(File::PATH_SEPARATOR),