3ofcoins / chef-helpers

A collection of helper methods to use in Opscode Chef recipes
MIT License
6 stars 3 forks source link

Fix problem on calling has_resource? multiple times #4

Closed sonots closed 9 years ago

sonots commented 9 years ago

Hi, I found that calling has_resource? multiples times returns different (and wrong) results.

Assume we have files/foo.txt, and run recies/default.rb whose contents are like:

puts has_cookbook_file?("bar.txt") #=> nil
puts has_cookbook_file?("foo.txt") #=> /path/to/cookbook/files/foo.txt
puts has_cookbook_file?("bar.txt") #=> nil
puts has_cookbook_file?("foo.txt") #=> nil!!!!

The 2nd time has_cookbook_file?("foo.txt") misses the existence of files/foo.txt.

It seems the reason is because Chef::CookbookVersion#preferred_manifest_record does destructive operations when cookbook_file is not found (for "bar.txt") at

          # Strip the root_dir prefix off all files for readability
          existing_files.map!{|path| path[root_dir.length+1..-1]} if root_dir

https://github.com/chef/chef/blob/9467b30cc1690f566f083d1710fd0436adb8b67b/lib/chef/cookbook_version.rb#L330

The contents of the existing_files variable were becoming as follows:

# before
existing_files #=> ["/path/to/cookbook/files/foo.txt"]
# after has_cookbook_file?("bar.txt")
existing_files #=> ["files/foo.txt"]
# after has_cookbook_file?("bar.txt") 2nd time
existing_files #=> [nil]

This patch avoids the problem by calling find_preferred_manifest_record instead of preferred_manifest_record.

xamebax commented 9 years ago

Hi! Than you for this PR! I'm sorry for this belated reply. Thanks for your time spent looking into this, for your explanation and the patch. I'm merging.