Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cookbook generator converge fails when run from the same parent directory as new cookbook #61

Open
bipinbachhao-lilly opened this issue Mar 3, 2020 · 5 comments
Assignees
Labels
Triage: Confirmed Indicates and issue has been confirmed as described. Type: Bug Does not work as expected.

Comments

@bipinbachhao-lilly
Copy link

bipinbachhao-lilly commented Mar 3, 2020

Version:

Chef Workstation version: 0.16.33
Chef Infra Client version: 15.8.23
Chef InSpec version: 4.18.51
Chef CLI version: 2.0.0
Test Kitchen version: 2.3.4
Cookstyle version: 5.22.6

Environment:

[Details about the environment such as the Operating System, cookbook details, etc...]
MacOS -10.14.6

Scenario:

Generator code generator and trying use it to generate new cookbook

Steps to Reproduce:

chef generate generator test_code_generator

Try to create new cookbook using generator

chef generate cookbook -g /Users/Workspace/projects/chef/cookbooks/test_code_generator/ test_cookbook

Expected Result:

New cookbook should be generatored

Actual Result:

Generator generator:
chef generate generator test_code_generator

Copied built-in generator cookbook to /Users/Workspace/projects/chef/cookbooks/test_code_generator
Add the following to your config file to enable it:
chefcli.generator_cookbook "/Users/Workspace/projects/chef/cookbooks/test_code_generator"

Generator cookbook:

chef generate cookbook -g /Users/Workspace/projects/chef/cookbooks/test_code_generator/ test_cookbook

Generating cookbook test_cookbook
- Ensuring correct cookbook content


Error executing action `create_if_missing` on resource 'template[/Users/Workspace/projects/chef/cookbooks/test_cookbook/metadata.rb]'


Chef::Exceptions::CookbookNotFoundInRepo
----------------------------------------
The directory /Users/Workspace/projects/chef/cookbooks/test_cookbook does not contain a cookbook

Resource Declaration:
---------------------
# In /Users/Workspace/projects/chef/cookbooks/test_code_generator/recipes/cookbook.rb

 25: template "#{cookbook_dir}/metadata.rb" do
 26:   helpers(ChefCLI::Generator::TemplateHelper)
 27:   variables(
 28:     spdx_license: spdx_license
 29:   )
 30:   action :create_if_missing
 31: end
 32:

Compiled Resource:
------------------
# Declared in /Users/Workspace/projects/chef/cookbooks/test_code_generator/recipes/cookbook.rb:25:in `from_file'

template("/Users/Workspace/projects/chef/cookbooks/test_cookbook/metadata.rb") do
  action [:create_if_missing]
  default_guard_interpreter :default
  source "metadata.rb.erb"
  helper_modules [ChefCLI::Generator::TemplateHelper]
  declared_type :template
  cookbook_name :test_code_generator
  recipe_name "cookbook"
  variables {:spdx_license=>"All Rights Reserved"}
  path "/Users/Workspace/projects/chef/cookbooks/test_cookbook/metadata.rb"
  owner nil
  group nil
  mode nil
  verifications []
end

System Info:
------------
chef_version=15.8.23
ruby=ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
program_name=/usr/local/bin/chef-cli
executable=/opt/chef-workstation/bin/chef-cli

ERROR: Chef Infra failed to converge: template[/Users/Workspace/projects/chef/cookbooks/test_cookbook/metadata.rb] (test_code_generator::cookbook line 25) had an error: Chef::Exceptions::CookbookNotFoundInRepo: The directory /Users/Workspace/projects/chef/cookbooks/test_cookbook does not contain a cookbook from file /opt/chef-workstation/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/cookbook/cookbook_version_loader.rb:78:in `load!'
Caused by: (Chef::Exceptions::CookbookNotFoundInRepo) template[/Users/Workspace/projects/chef/cookbooks/test_cookbook/metadata.rb] (test_code_generator::cookbook line 25) had an error: Chef::Exceptions::CookbookNotFoundInRepo: The directory /Users/Workspace/projects/chef/cookbooks/test_cookbook does not contain a cookbook
@bipinbachhao-lilly bipinbachhao-lilly added the Status: Untriaged An issue that has yet to be triaged. label Mar 3, 2020
@jyan-chef jyan-chef added Triage: Confirmed Indicates and issue has been confirmed as described. Type: Bug Does not work as expected. and removed Status: Untriaged An issue that has yet to be triaged. labels Mar 11, 2020
@chef-davin
Copy link

@bipinbachhao-lilly I just wanted to let you know that I did find a workaround for this. If you move your generator cookbook test_code_generator into a different parent directory from the one where you want to create your cookbook, it will work.

For instance, if you were to have your cookbook generator in the below path:

/Users/Workspace/projects/chef/generators/test_code_generator

And then ran the following command to create a cookbook in you cookbooks directory:

chef generate cookbook \
/Users/Workspace/projects/chef/cookbooks/my_new_cookbook \
-g /Users/Workspace/projects/chef/generators/test_code_generator

The generate should work just fine.

For some reason we're still looking into, having the directory for your generator cookbook in the same parent directory where you're hoping to generate your new cookbook, the generate command throws this error. If you've moved the generator cookbook to a different parent directory, or try to create your cookbooks somewhere else, then this will resolve the issue.

I hope this helps.
~Davin

@subhro
Copy link

subhro commented May 22, 2020

I also hit the same bug at a customer place. The workaround Davin posted worked.

@marcparadise marcparadise changed the title cookbook generators produce errors cookbook generator converge fails when run from the same parent directory as new cookbook Feb 9, 2021
@marcparadise
Copy link
Member

Having a cookbook generator in the same location as your repository (. if you don't specify otherwise) means that ChefFS will pick up the new cookbook directory as soon as it's created during generator run; and it will fail on that, because it is expecting a cookbook and finds only an empty directory.

We're considering approaches to change this behavior, but for the time being chef-cli does not support keeping your cookbook generator in the same directory that you are trying to generate cookbooks into.

Reference chef/chef-zero#285, which documents the underlying issue.

@marcparadise
Copy link
Member

One alternative here would be to have chef generate cookbook upload the generator cookbook to a running local chef-zero instance, which would have the effect of not considering the current directory as part of the chef repo - preventing it from scanning/picking up the new cookbook as it is created.

Perhaps the best short term option would be to detect the condition that would lead to error (generator CB shares a parent dir with the to-be-created cookbook), and give instructions on what to do instead of going ahead with the converge

@chef-davin
Copy link

Perhaps the best short term option would be to detect the condition that would lead to error (generator CB shares a parent dir with the to-be-created cookbook), and give instructions on what to do instead of going ahead with the converge

This would be awesome. I think if we can detect this condition and have a sane error message, then it's not a bug and it's just functionality and we can close all this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Triage: Confirmed Indicates and issue has been confirmed as described. Type: Bug Does not work as expected.
Projects
None yet
Development

No branches or pull requests

6 participants