Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Aohzan
Copy link

@Aohzan Aohzan commented Oct 13, 2025

Hello,

--answers-file must be relative to the destination path, but when this one doesn't exist, the answers file cannot be found, example:

copier copy --answers-file ../../../.copier-answers.yml mytemplate my/dest/path

will not found the .copier-answers.yml file

an other solution (or complementary) will be to search the file in the current dir.

Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case, please?

@Aohzan Aohzan force-pushed the fix/dst_path_not_existing branch from 2ad5958 to c9c1031 Compare October 13, 2025 15:25
@Aohzan
Copy link
Author

Aohzan commented Oct 13, 2025

Could you add a test case, please?

done

Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test! 👍 I've left a few remarks.

Comment on lines +353 to +357
(src / ".copier-answers.yml"): (
"""\
project_name: "test project"
"""
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setup is not representative of a real one.

Suggested change
(src / ".copier-answers.yml"): (
"""\
project_name: "test project"
"""
),
(src / "{{ _copier_conf.answers_file }}.jinja"): (
"{{ _copier_answers|to_nice_yaml }}"
),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is defined the _copier_answers?

Copy link
Member

@sisp sisp Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Jinja variable available in our render context.

https://copier.readthedocs.io/en/stable/creating/#_copier_answers

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but where do I define that project_name: "test project"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can either set it as the default answer in copier.yml

             (src / "copier.yml"): (
                 """\
                 project_name:
                   type: str
+                  default: test project
                 """
             ),

and run

copier.run_copy(..., defaults=True, ...)

(as you're doing already) or instead pass the answer via the data argument:

copier.run_copy(..., data={"project_name": "test project"}, ...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but I don't test my case where I want to be dure that the existing answers file is loaded, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does your use case assume an existing answers file? If so, why? This seems like an unusual pattern. The answers file is generated by Copier to record previous answers plus metadata to enable updating to new template versions. It looks like you're assuming the answers file to exist before the first copier copy call. Are you trying to pass answers to copier copy from a file instead of via interactive prompting or inline answers via the -d,--data flag? If so, perhaps --data-file is what you're looking for.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one of our use case is to have a template for the repository, then we can add terraform stack in subdirs, and we call the initial answer file to not repeat common questions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think --data-file might work for this use case as well although you'd be passing an answers file, which is a superset of a typical data file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or _external_data might be worth checking out.

git_save(src, tag="v1")

# Destination path that doesn't exist yet
dst_dir = src / "my" / "dest" / "path"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result might be same, but this setup is a bit unintuitive because you're generating the project in a subdirectory of the template's sources. I'd follow the usual setup with

src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))

and generate in dst = src / "my" / "dest" / "path" instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean dst_dir = dst / "my" / "dest" / "path" ? or I miss something

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, yes, I mistyped.

)

# Verify the project was created
assert (dst_dir / "README.txt").read_text() == "Project: test project"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also assert that the .copier-answers.yml file exists in the expected location.

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

Successfully merging this pull request may close these issues.

2 participants