copier-org / copier

Library and command-line utility for rendering projects templates.
https://readthedocs.org/projects/copier/
MIT License
2.03k stars 181 forks source link

ProcessExecutionError when a file is un-ignored across template versions #1773

Open fresh2dev opened 1 month ago

fresh2dev commented 1 month ago

Describe the problem

In one tagged release of my copier template, I added an entry to a templated .gitignore to ignore the file .env. In a later tagged release, I removed this entry from the templated .gitignore file. Now, when upgrading templated projects, I get this error:

ProcessExecutionError: | Unexpected exit code: 1
Stderr:                | The following paths are ignored by one of your .gitignore files:
                       | .env

Template

Currently must remain private. =/

To Reproduce

  1. Within the template, add an entry to the templated .gitignore
  2. Commit and tag the template repo.
  3. Update templated project(s).
  4. Within the template, remove the entry from the templated .gitignore
  5. Commit and tag the template repo.
  6. Update templated project(s) and observe the error.

Logs

ProcessExecutionError: | Unexpected exit code: 1
Stderr:                | The following paths are ignored by one of your .gitignore files:
                       | .env

The stack trace points to the git add command here:

https://github.com/copier-org/copier/blob/v9.3.1/copier/main.py#L1002

Expected behavior

Copier should force-add the file and proceed without error.

Screenshots/screencasts/logs

No response

Operating system

macOS

Operating system distribution and version

Sonoma 14.6.1

Copier version

9.3.1

Python version

Python 3.12

Installation method

pipx+pypi

Additional context

The stack trace points to the git add command here:

https://github.com/copier-org/copier/blob/v9.3.1/copier/main.py#L1002

FWIW, git add has a flag -f, --force to "Allow adding otherwise ignored files".

As a workaround, the update completes successfully when I exclude the un-ignored file from the update: copier update --exclude .env

sisp commented 1 month ago

Thanks for reporting this problem, @fresh2dev! :+1:

You've traced the problem to its root, and I think adding the -f, --force flag to the git add command is correct. I'm just trying to better understand your scenario to write a meaningful test. Could you please provide a minimal reproducible example? Your reproduction steps are not complete, there must be a step where you add the file to be git-ignored for this problem to occur.

fresh2dev commented 2 hours ago

I created an example repo to reproduce the issue:

https://github.com/fresh2dev/copier-repro-1773

Looking at the commits on this example repo, the steps that cause this problem involve:

  1. init the repo
  2. later, add a file to a templated .gitignore
  3. later, remove a file from the templated .gitignore

Here are the exact steps I followed to repro the issue from this repo:

  1. Create a new project from the initial template commit.
copier copy -r 1.0.0 https://github.com/fresh2dev/copier-repro-1773 hello-world
  1. Commit everything.
cd hello-world
git init
git add .
git commit -am 'init'
  1. Update the template to the next commit (this adds .env to .gitignore) and commit.
copier update -fr 2.0.0

git commit -am 'update to v2'
  1. Update the template to the next commit (this removes .env from .gitignore)
copier update -fr 3.0.0
  1. The errors out with
Stderr:       | The following paths are ignored by one of your .gitignore files:
              | .env
              | hint: Use -f if you really want to add them.
              | hint: Turn this message off by running
              | hint: "git config advice.addIgnoredFile false"