Skip to content

fix(chezmoi): escape config values so prompt answers can't break TOML - #197

Merged
aguil merged 1 commit into
masterfrom
fix/chezmoi-config-escape-values
Nov 23, 2026
Merged

aguil merged 1 commit into
masterfrom
fix/chezmoi-config-escape-values

Conversation

@aguil

@aguil aguil commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Why the change

chezmoi init failed with a TOML parse error when a prompt answer contained a quote, such as a 1Password reference copied as "op://…". Now every value is escaped and prompted answers lose surrounding quotes, so any answer produces a valid config.

Special things to note

  • With every prompt at its default, the rendered config is byte-for-byte unchanged for both personal and work, so existing machines see no difference on their next init.
  • Trimming applies only to prompted answers. Env vars and values already in the config are escaped but otherwise kept as-is.
  • No automated test: CI doesn't install chezmoi. The template was checked by hand with chezmoi execute-template --init against an empty config (see below).

Change outline

Values used to be built inline inside literal quotes. They're now worked out into variables first, in key order so the prompts keep their order, and written with toToml:

  [data.codeberg]
-   tokenOpRef = "{{ if ne (env …) "" }}{{ env … }}{{ else if … }}{{ promptStringOnce . "codebergTokenOpRefPersonal" … }}{{ end }}"
+   tokenOpRef = {{ $codebergTokenOpRef | toToml }}
+ {{- $codebergTokenOpRef := env "CHEZMOI_CODEBERG_TOKEN_OP_REF" -}}
+ {{- if ne $codebergTokenOpRef "" -}}
+ {{- else if ne $existingCodebergTokenOpRef "" -}}{{- $codebergTokenOpRef = $existingCodebergTokenOpRef -}}
+ {{- else if eq $profile "personal" -}}
+ {{- $codebergTokenOpRef = promptStringOnce … | trim | trimAll `"` | trimAll `'` -}}
+ {{- end -}}

The same applies to every interpolated key in [data], [data.gh], [data.codeberg], [data.ssh.*], [data.komorebi] and [data.git.personal], and to the earlier gitPersonal* prompts.

Manual check, prompts answered with awkward values against an empty config:

Answer Before After
"op://Private/Codeberg/token" TOML parse error op://Private/Codeberg/token
'op://Private/Codeberg SSH/private key' stored with quotes quotes stripped
C:\keys\my "odd" key TOML parse error stored exactly
github.com stored with spaces github.com

🤖 Generated with Claude Code

.chezmoi.toml.tmpl wrapped each value in literal double quotes without
escaping it. An answer that itself started with a quote, such as a
1Password reference copied as "op://Private/Codeberg/token", rendered
as tokenOpRef = ""op://..." and chezmoi init failed with
"expected a top-level item to end with a newline, comment, or EOF".
A backslash or quote anywhere in a value (a Windows key path, an
author name) had the same effect.

Work out each value into a variable first, in the same order as the
keys so the prompts keep their order, and write it with toToml, which
quotes and escapes it. Prompted answers are also trimmed of surrounding
whitespace and quote characters, so the copied reference is stored
without its quotes.

With every prompt at its default, the rendered config is byte-for-byte
the same as before for both profiles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aguil
aguil marked this pull request as ready for review November 23, 2026 03:44
@aguil
aguil merged commit 0ce3ceb into master Nov 23, 2026
1 check passed
@aguil
aguil deleted the fix/chezmoi-config-escape-values branch November 23, 2026 03:45
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.

1 participant