patb23
patb23

Reputation: 397

Ruby Uninitialized Constant Error when Inheriting in Chef

In my LWRP extending a supermarket cookbook, I got the following working.

module PIWebsphereCookBook
 class WebsphereJbdc < WebsphereCookBook::WebsphereBase

Whereas when I introduced my own class in between these 2, I am getting the 'Unitialized Constant Error'

require_relative './pi_websphere_base'
module PIWebsphereCookBook
  class WebsphereJbdc < PIWebsphereBase

The base class I am trying to extend is as shown below:

module PIWebsphereCookBook
  class PIWebsphereBase < WebsphereCookbook::WebsphereBase

I see from the logs that the referenced class/file is being loaded

[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/pi_websphere_base.rb
[2017-06-23T18:37:28-04:00] DEBUG: Loaded contents of /var/chef/cache/cookbooks/pi_websphere/resources/pi_websphere_base.rb into resource pi_websphere_pi_websphere_base (Custom resource pi_websphere_pi_websphere_base from cookbook pi_websphere)
[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/websphere_j2c.rb
[2017-06-23T18:37:28-04:00] DEBUG: Loaded contents of /var/chef/cache/cookbooks/pi_websphere/resources/websphere_j2c.rb into resource pi_websphere_websphere_j2c (Custom resource pi_websphere_websphere_j2c from cookbook pi_websphere)
[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb
[2017-06-23T18:37:28-04:00] DEBUG: Filtered backtrace of compile error: /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in `<module:PIWebsphereCookBook>',/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:2:in `class_from_file'
[2017-06-23T18:37:28-04:00] DEBUG: Backtrace entry for compile error: '/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in `<module:PIWebsphereCookBook>''
[2017-06-23T18:37:28-04:00] DEBUG: Line number of compile error: '3'

Recipe Compile Error in 
    /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb
NameError
---------
uninitialized constant #
<Class:0x000000052796d8>::PIWebsphereCookBook::PIWebsphereBase

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in `    <module:PIWebsphereCookBook>'
  /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:2:in `    class_from_file'

    Relevant File Content:
----------------------
/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:
  1:  require_relative './pi_websphere_base'
  2:  module PIWebsphereCookBook
  3>>   class WebsphereJbdc < PIWebsphereBase
  4:      require_relative 'helper'
  5:  

The folder structure is given below

[root@localhost pi_websphere]# tree
.
├── attributes
│   └── default.rb
├── Gemfile
├── jaas_auth.py
├── libraries
│   ├── abc.rb
│   ├── artifactory_helper.rb
│    ├── pi_websphere_base.rb
│   └── websphere_base.rb
├── metadata.rb
├── README.md
├── resources
│   ├── artifactory_client.rb
│   ├── helper.rb
│   ├── websphere_j2c.rb
│   ├── websphere_jdbc.rb

Upvotes: 1

Views: 707

Answers (1)

coderanger
coderanger

Reputation: 54181

You need to make sure the file containing PIWebsphereBase is loaded first. Normally cookbook library files are loaded in ASCII-betical order, but you can force things a little more with the require_relative you have there but is commented out. You probably want require_relative './pi_websphere_base' or something like that, or tweak the filenames.

Upvotes: 1

Related Questions