twpayne / chezmoi

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

User variables in `chezmoi.toml.tmpl` not available in first `init` run #1213

Closed davidolrik closed 3 years ago

davidolrik commented 3 years ago

Upon first run of sh -c "$(curl -fsLS git.io/chezmoi)" -- init davidolrik --apply I get the following template error:

chezmoi: template: dot_config/private_hub.tmpl:2:14: executing "dot_config/private_hub.tmpl" at <.me.github.username>: map has no entry for key "me"

dot_config/private_hub.tmpl contains this:

github.com:
- user: {{ .me.github.username }}
  protocol: https

and my chezmoi.toml.tmpl has a section with the referenced variable:

[data.me.github]
username = "davidolrik"

After the first run, the contents of chezmoi.toml is correct, and a second run installs the dot_config/private_hub.tmpl as expected.

So it seems that the chezmoi.toml is generated correctly, but not reloaded / made available to other templates during the first init.

twpayne commented 3 years ago

I'm unable to reproduce this either with my own dotfiles or with a test (#1214).

Could you share a way to reproduce this?

davidolrik commented 3 years ago

I will try to create a small test case that exhibits the problem.

The weird thing is, that it's not all templates that can't access the variables. E.g. my .gitconfig also uses data under me and that is interpolated in the first go. Scratch that, that was me not cleaning up everything between runs.

twpayne commented 3 years ago

If I understand correctly, this is now fixed. Please re-open if needed. Thanks!

davidolrik commented 3 years ago

No, I still have the issue - I just thought it was only some of the templates that couldn't access the variables, when it is in fact all of them.

twpayne commented 3 years ago

Any update on a way of reproducing this?

davidolrik commented 3 years ago

Sorry haven't had the time before now, luckily it was quite easy to reproduce.

Here is a very small dotfile repo that exhibits the error: https://github.com/davidolrik/chezmoi-bug.git

And here is the output of a run from a clean state:

sh -c "$(curl -fsLS git.io/chezmoi)" -- init --apply https://github.com/davidolrik/chezmoi-bug.git
info found version 2.0.13 for latest/linux/amd64
info installed ./bin/chezmoi
Cloning into '/home/test/.local/share/chezmoi'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 1.28 KiB | 654.00 KiB/s, done.
chezmoi: template: dot_envrc.tmpl:3:18: executing "dot_envrc.tmpl" at <.me.fullname>: map has no entry for key "me"

$ cat .envrc
cat: .envrc: No such file or directory

$ sh -c "$(curl -fsLS git.io/chezmoi)" -- init --apply https://github.com/davidolrik/chezmoi-bug.git
info found version 2.0.13 for latest/linux/amd64
info installed ./bin/chezmoi

$ cat .envrc
#!/usr/bin/env bash

export name=David Jack Wange Olrik
export email=david.personal@example.com
export signingkey=ECC395B798725570D8699DCB5B3BAADEDDEE156E
twpayne commented 3 years ago

Thanks for the reproduction case - I see what's happening now.

The config file template needs to be called .chezmoi.toml.tmpl in the source directory, not dot_config/chezmoi/chezmoi.toml.tmpl.

davidolrik commented 3 years ago

Ah, I see – I just a chezmoi add --template ~/.config/chezmoi/chezmoi.toml.

Maybe this could be intercepted, and turned into .chezmoi.toml.tmpl?

twpayne commented 3 years ago

Maybe this could be intercepted, and turned into .chezmoi.toml.tmpl?

In practice, I think this would be really tricky to intercept. ~/.config/chezmoi/chezmoi.toml happens to be the default config file location, but this is fully customizable based on the XDG base directory specification and can also be overridden with the --config command line flag.

I think this issue is now resolved, please re-open if needed.