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

Current working directory during copier {copy,recopy,update} should be destination path #1708

Open
sisp opened this issue Jul 23, 2024 · 1 comment
Labels
bug triage Trying to make sure if this is valid or not

Comments

@sisp
Copy link
Member

sisp commented Jul 23, 2024

Describe the problem

While reimplementing jinja-ansible-filters (see #1398) – specifically testing the fileglob filter –, I noticed that the current working directory while rendering template files through copier {copy,recopy,update} is the directory from which the command is executed, but I think it should be the destination path; otherwise, the fileglob filter (and other custom filesystem-based Jinja filters) wouldn't be particularly useful. WDYT?

Template

example.zip

The filesystem layout is as follows:

.
├── cwd.txt
├── dst
│   ├── a.txt
│   └── b.txt
└── src
    └── fileglob.txt.jinja

The example assumes that src/ contains a template from which a subproject will be generated into dst/, which already contains files (e.g., from another Copier template).

To Reproduce

  1. Extract the content of the reproducible example in the ZIP archive above.

  2. Change your current working directory to the root directory where you extracted the archive (i.e. where the file cwd.txt is located).

  3. Run copier copy src/ dst/.

  4. Note the content of the generated file dst/fileglob.txt:

    $ cat dst/fileglob.txt
    ['cwd.txt']

    As you can see, the file cwd.txt is listed as a result of {{ '*.txt' | fileglob }}.

Logs

No response

Expected behavior

I'd expect the following behavior:

$ copier copy src/ dst/
$ cat dst/fileglob.txt
['a.txt', 'b.txt']

I.e., filesystem-related Jinja filters should operate relative to the destination path.

That said, filesystem-related Jinja filters operating on the destination path may lead to unexpected behavior because Copier will generate files in arbitrary order, so the output of the fileglob filter may not be consistent across multiple runs.

Screenshots/screencasts/logs

No response

Operating system

Linux

Operating system distribution and version

Ubuntu 22.04

Copier version

9.3.1

Python version

CPython 3.12

Installation method

pipx+pypi

Additional context

No response

@sisp sisp added bug triage Trying to make sure if this is valid or not labels Jul 23, 2024
@pcastellazzi
Copy link

Something similar happen when you use copier copy ../src/ dst/. After running cd dst ; copier update , ValueError("Local template must be a directory.") is raised because the answers file contains the relative reference, instead of an absolute path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug triage Trying to make sure if this is valid or not
Projects
None yet
Development

No branches or pull requests

2 participants