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

Feature-Request: Use inheritance with nested configuration #2028

Open
donnyyung opened this issue Mar 5, 2024 · 1 comment
Open

Feature-Request: Use inheritance with nested configuration #2028

donnyyung opened this issue Mar 5, 2024 · 1 comment

Comments

@donnyyung
Copy link

  • Cookiecutter version: 2.6.0
  • Template project url:
  • Python version: 3.11
  • Operating System: Macos

Description:

I'm trying to use nested configuration files since we have one repo with a multiple templates for users to create an app whether it's python or go or some other language. Some of the files like our README, our circle pipelines, etc. will be the same templates for all the services, so I was also trying to use inheritance to have a set of shared files at the top of the repo that I can use in both templates, the file structure looks like:

main-directory/
├── project-1
│ ├── cookiecutter.json
│ ├── {{cookiecutter.project_slug}}
| │ ├── ...
├── package
│ ├── cookiecutter.json
│ ├── {{cookiecutter.project_slug}}
| │ ├── ...
├── cookiecutter.json
├── templates/
└── README_tpl.md

And then I have a file in project-1/cookiecutter.project_slug/README.md that contains:

{% include "README_tpl.md" %}

What I've run:

And when I try to create an app using cookiecutter from the root dir, I get this error:

Traceback (most recent call last):
  File "/usr/local/bin/cookiecutter", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/cookiecutter/cli.py", line 203, in main
    cookiecutter(
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/cookiecutter/main.py", line 143, in cookiecutter
    return cookiecutter(
           ^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/cookiecutter/main.py", line 182, in cookiecutter
    result = generate_files(
             ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/cookiecutter/generate.py", line 409, in generate_files
    generate_file(
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/cookiecutter/generate.py", line 209, in generate_file
    rendered_file = tmpl.render(**context)
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "README.md", line 1, in top-level template code
  File "/usr/local/Cellar/cookiecutter/2.6.0/libexec/lib/python3.12/site-packages/jinja2/loaders.py", line 204, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: README_tpl.md

Admittedly I haven't looked at the code for cookiecutter (apologies!), so I'm not sure if this is a bug or just something not supported, I suspect the latter. So wanted to confirm that and also ask if this is something you might want to support in the future or if it's something that can't be supported for xyz reason.

Thank you!

@jessebrizzi
Copy link

I am currently attempting something similar and it looks to be not currently supported.

The issue is the jinja2 FileSystemLoader is hardcoding the location for the inheritance folder to ../templates .

A possible solution could be adding support to pass in the location of the inheritance folder as a optional argument to the cookiecutter() call. Or some symlink setup with your multiple template directories to get it working with the current version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants