From ae4c19a7a3e4c6f68b9bbad75580958b906e951a Mon Sep 17 00:00:00 2001 From: Ilya Kiselev Date: Mon, 23 Mar 2026 18:21:28 +0300 Subject: [PATCH] fix(engine): make toToml return empty string on error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `toToml` was returning `err.Error()` on marshal failure, inconsistent with `toYaml` and `toJson` — both of which swallow errors and return an empty string to avoid embedding raw error text in rendered templates. Align `toToml` with the same convention. Charts relying on the old behavior (checking for an error string in the output) should use `mustToToml` (available since #XXXX) to get an explicit failure instead. Closes #31430 Signed-off-by: Ilya Kiselev --- pkg/engine/funcs.go | 6 ++++-- pkg/engine/funcs_test.go | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index 431f82f63..d73f7b979 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -169,7 +169,8 @@ func fromYAMLArray(str string) []any { } // toTOML takes an interface, marshals it to toml, and returns a string. -// On marshal error it returns the error string. +// On marshal error it returns an empty string (errors are swallowed), +// consistent with toYAML and toJSON. // // This is designed to be called from a template. Use mustToToml if you need // the template to fail hard on marshal errors. @@ -178,7 +179,8 @@ func toTOML(v any) string { e := toml.NewEncoder(b) err := e.Encode(v) if err != nil { - return err.Error() + // Swallow errors inside of a template. + return "" } return b.String() } diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index cf6a8d5c9..21bc17e2b 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -180,6 +180,10 @@ keyInElement1 = "valueInElement1"`, tpl: `{{ mustToToml . }}`, expect: "foo = \"bar\"\n", // should succeed and return TOML string vars: map[string]string{"foo": "bar"}, + }, { + tpl: `{{ toToml . }}`, + expect: "", // should return empty string and swallow error (not err.Error()) + vars: map[int]string{1: "one"}, // non-string key is invalid in TOML }, }