diff --git a/libraries/windows_helper.rb b/libraries/windows_helper.rb index ca6a4bc7..b36c4785 100644 --- a/libraries/windows_helper.rb +++ b/libraries/windows_helper.rb @@ -18,12 +18,15 @@ # limitations under the License. require 'uri' +require 'Win32API' if Chef::Platform.windows? +require 'chef/exceptions' module Windows module Helper AUTO_RUN_KEY = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'.freeze unless defined?(AUTO_RUN_KEY) ENV_KEY = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.freeze unless defined?(ENV_KEY) + ExpandEnvironmentStrings = Win32API.new('kernel32', 'ExpandEnvironmentStrings', ['P', 'P', 'L'], 'L') if Chef::Platform.windows? # returns windows friendly version of the provided path, # ensures backslashes are used everywhere @@ -82,6 +85,17 @@ def cached_file(source, checksum=nil, windows_path=true) end end + # Expands the environment variables + def expand_env_vars(path) + # We pick 32k because that is the largest it could be: + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms724265%28v=vs.85%29.aspx + buf = 0.chr * 32 * 1024 # 32k + if ExpandEnvironmentStrings.call(path, buf, buf.length) == 0 + raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings (received 0)" + end + buf.strip + end + end end diff --git a/providers/path.rb b/providers/path.rb index d297aec9..88c49780 100644 --- a/providers/path.rb +++ b/providers/path.rb @@ -19,11 +19,27 @@ # use_inline_resources if defined?(use_inline_resources) +include Windows::Helper + action :add do env "path" do action :modify delim ::File::PATH_SEPARATOR value new_resource.path + notifies :run, "ruby_block[fix ruby ENV['PATH']]", :immediately + end + + # The windows Env provider does not correctly expand variables in + # the PATH environment variable. Ruby expects these to be expanded. + # This is a temporary fix for that. + # + # Follow at https://github.com/opscode/chef/pull/1876 + # + ruby_block "fix ruby ENV['PATH']" do + block do + ENV['PATH'] = expand_env_vars(ENV['PATH']) + end + action :nothing end end