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

directory names rendered to empty string raise exception or overwrite parent directories #1518

Open
dusktreader opened this issue Apr 16, 2021 · 1 comment
Assignees
Labels
bug This issue/PR relates to a bug.

Comments

@dusktreader
Copy link

  • Cookiecutter version: 1.7.2
  • Template project url: n/a
  • Python version: 3.8.7
  • Operating System: Ubuntu 20.04 (via WSL)

Description:

If you have a directory whose template resolve to an empty string, it will do one of two bad things:

  1. If you are running normally, it will give an error about the directory already existing
  2. If you run with overwrite-if-exists, bad stuff happens culminating in a permission denied error

This is happening, because the render_and_create_dir method joins the generate path with its parent to produce a new one. If the generated path is empty, the resulting path is the same as its parent. This behavior is clearly undesirable.

I think an empty directory name (and filename) for that matter should be omitted entirely, perhaps with a warning

What I've run:

$ cat dump.txt
$ tree
.
├── cookiecutter.json
└── {{cookiecutter.project_name}}
    ├── README.md
    └── {{cookiecutter.foolish}}
        └── BOOM.md

2 directories, 3 files

$ cat cookiecutter.json
{
    "project_name": "boom-test",
    "foolish": ""
}

$ cookiecutter --output-dir=.. .
project_name [boom-test]:
foolish []:
Error: "/home/dusktreader/boom-test" directory already exists

$ cookiecutter --output-dir=.. . -f
project_name [boom-test]:
foolish []:
Traceback (most recent call last):
  File "/home/dusktreader/.pyenv/versions/3.8.7/bin/cookiecutter", line 8, in <module>
    sys.exit(main())
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/cookiecutter/cli.py", line 140, in main
    cookiecutter(
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/cookiecutter/main.py", line 101, in cookiecutter
    result = generate_files(
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/cookiecutter/generate.py", line 352, in generate_files
    generate_file(
  File "/home/dusktreader/.pyenv/versions/3.8.7/lib/python3.8/site-packages/cookiecutter/generate.py", line 179, in generate_file
    with io.open(outfile, 'w', encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: '/BOOM.md'
@simobasso
Copy link
Member

Nice @dusktreader good catch!

The permission denied is because cookiecutter wanna write BOOM.md on the root.

I'll try to look into the issue.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

2 participants