twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

Source files with `create_` can create empty files even when `empty_` is not used. #2995

Closed BoltsJ closed 1 year ago

BoltsJ commented 1 year ago

Describe the bug

When a create_ source file or template would result in an empty file, the files is created regardless of whether the file is also empty_.

To reproduce

For a non-tempate: In the source directory

$ touch create_a
$ chezmoi apply

An empty a file will be created.

For a template: In the source directory:

$ cat > create_b.tmpl << EOF
{{- if false -}}
Won't print
{{- end -}}
EOF
$ chezmoi apply

An empty b file will be created

Expected behavior

Files with create_ but not empty_ do not create empty files

Output of command with the --verbose flag

$ chezmoi --verbose $COMMAND
diff --git a/a b/a
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
--- /dev/null
+++ b/a
diff --git a/b b/b
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
--- /dev/null
+++ b/b

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.33.5, commit a06f7661068cb258007e4736290d2b80966f2525, built at 2023-05-11T19:25:30Z, built by goreleaser ok latest-version v2.33.5 ok os-arch linux/amd64 (Fedora Linux 38 (Container Image)) ok uname Linux toolbox 6.2.14-300.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 1 00:55:28 UTC 2023 x86_64 GNU/Linux ok go-version go1.20.4 (gc) ok executable ~/.local/bin/chezmoi ok upgrade-method replace-executable ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2023-05-09T00:17:14-05:00 warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) ok suspicious-entries no suspicious entries warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok umask 022 ok cd-command found ~/.local/share/chezmoi/shell-wrapper ok cd-args ~/.local/share/chezmoi/shell-wrapper info diff-command not set ok edit-command found /usr/bin/emacs ok edit-args /usr/bin/emacs ok git-command found /usr/bin/git, version 2.40.0 ok merge-command found ~/.local/bin/vimdiff ok shell-command found /bin/bash ok shell-args /bin/bash info age-command age not found in $PATH ok gpg-command found ~/.local/bin/gpg, version 2.4.0 info pinentry-command not set info 1password-command op not found in $PATH ok bitwarden-command found ~/.local/bin/bw, version 2023.4.0 info dashlane-command dcli not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info rbw-command rbw not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

Add any other context about the problem here.

4ooc commented 1 year ago

What you’re describing is the default behavior without the empty_ and create_ prefixes, so simply remove the create prefix! The document explains what `create` does: Ensure that the file exists, and create it with contents if it does not

BoltsJ commented 1 year ago

Right, but the problem is that create_ is also creating files with no contents when empty_ is not specified.

4ooc commented 1 year ago

In my understanding, create_ applies the source contents only when the target file does not exist, initializing the file. Whereas empty_ always applies the source contents, managing the content of the file. It’s not possible to use create_ and empty_ together.

BoltsJ commented 1 year ago

Then what is the point of empty_ if the source file is always applied regardless of whether it's empty?