Variables in recipes

I have an issue when using variables in recipes.

I created a custom resource, which i use from a recipe. Within the recipe i have defined a variable. The custom resource has a property which has the same name as the variable. This seems to give a conflict.

How can i prevent that, when using variables the conflict with a resource? As i do not know all possible properties this can always happen (i assume).

resource_name :minimal

property :name, String, name_property: true
property :libj, [Array, String], required: true

action :install do
  # TODO: CLEANUP
  # TODO: check if libj property works as intended.
  sid = node['logcollector']['sid'].upcase
  sid_d = sid.downcase 
  sidadm = "#{sid_d}adm"
  rlibj = [*new_resource.libj]
  adapterpath = "/hana/shared/#{sid}/streaming/cluster/#{sid_d}/adapters"

  rlibj.each do |l|
    print "#{l}\n"
  end
end
libj = 'This collides with libj in minimal resource'

minimal 'rtparseradapter' do
  libj [ 
    'https://docs.chef.io/_static/chef_logo_v2.svg',
    'https://docs.chef.io/_static/LearnChefRally_300x250_v01.1b.jpg',
    'https://docs.chef.io/_static/usability_testing%20_banner_300x250.png'
  ]
  action :install
  notifies :restart, 'etdproject[proxy_tle]', :immediately
end

etdproject 'proxy_tle' do
  action :nothing
end
ArgumentError
-------------
wrong number of arguments (given 3, expected 1..2)

Cookbook Trace:
---------------
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:11:in `[]'
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:11:in `block in from_file'
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:10:in `from_file'

Relevant File Content:
----------------------
/etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:

  4:  #
  5:  # Copyright:: 2018, The Authors, All Rights Reserved.
  6:  #
  7:
  8:  libj = 'This collides with libj in minimal resource'
  9:
 10:  minimal 'rtparseradapter' do
 11>>   libj [
 12:      'https://docs.chef.io/_static/chef_logo_v2.svg',
 13:      'https://docs.chef.io/_static/LearnChefRally_300x250_v01.1b.jpg',
 14:      'https://docs.chef.io/_static/usability_testing%20_banner_300x250.png'
 15:    ]
 16:    action :install
 17:    notifies :restart, 'etdproject[proxy_tle]', :immediately
 18:  end
 19:
 20:  etdproject 'proxy_tle' do

Change the name of your variable to avoid the reported conflict, Why is this hard?.

I know how to fix t his specific issue, but it took me two days to find the reason for the error message. It is not very explaining.

I am looking for a general solution to avoid such problems in the future. I don’t know which resources might be introduced in the future.

What you’re seeing looks basically like a “scope” issue. What you are using as a very local hash happens to have the same name as a hash used elsewhere, and I suspect you selected it because it was very short and easy to type. So keeping your hash names more distinctive can be good practice, to help associate them with your specific recipe. I’d use “etd_libj” rather than “libj”, for example, to help segregate it from other cookbooks or system components.