From 341734d6ecad541532265f85d82ab45e30a9eeb8 Mon Sep 17 00:00:00 2001 From: Dev Kumar Date: Sun, 2 Aug 2026 20:37:38 -0500 Subject: [PATCH] Default to yaml when a file's extension is not a recognised format (#2785) * Default to yaml for unrecognised file extensions in format auto-detection * Add test for unrecognised extension defaulting to yaml --- pkg/yqlib/format.go | 7 +++++-- pkg/yqlib/format_test.go | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/format.go b/pkg/yqlib/format.go index 9cccf367..406ab6dd 100644 --- a/pkg/yqlib/format.go +++ b/pkg/yqlib/format.go @@ -128,8 +128,11 @@ func FormatStringFromFilename(filename string) string { ext := filepath.Ext(filename) if len(ext) >= 2 && ext[0] == '.' { format := strings.ToLower(ext[1:]) - GetLogger().Debugf("detected format '%s'", format) - return format + if _, err := FormatFromString(format); err == nil { + GetLogger().Debugf("detected format '%s'", format) + return format + } + GetLogger().Debugf("extension '%s' is not a recognised format, defaulting to yaml", format) } } diff --git a/pkg/yqlib/format_test.go b/pkg/yqlib/format_test.go index 6060ccdf..2a88faac 100644 --- a/pkg/yqlib/format_test.go +++ b/pkg/yqlib/format_test.go @@ -59,4 +59,7 @@ func TestFormatStringFromFilename(t *testing.T) { test.AssertResult(t, "json", FormatStringFromFilename("TEST.JSON")) test.AssertResult(t, "yaml", FormatStringFromFilename("test.json/foo")) test.AssertResult(t, "yaml", FormatStringFromFilename("")) + // unrecognised extensions should default to yaml instead of being passed through verbatim + // (see https://github.com/mikefarah/yq/issues/1608) + test.AssertResult(t, "yaml", FormatStringFromFilename("test.tfstate")) }