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
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.
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.
Hi, I found that calling
has_resource?
multiples times returns different (and wrong) results.Assume we have
files/foo.txt
, and runrecies/default.rb
whose contents are like:The 2nd time
has_cookbook_file?("foo.txt")
misses the existence offiles/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") athttps://github.com/chef/chef/blob/9467b30cc1690f566f083d1710fd0436adb8b67b/lib/chef/cookbook_version.rb#L330
The contents of the
existing_files
variable were becoming as follows:This patch avoids the problem by calling
find_preferred_manifest_record
instead ofpreferred_manifest_record
.