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
This commit is contained in:
Dev Kumar
2026-08-03 11:37:38 +10:00
committed by GitHub
parent b7a06d6e1f
commit 341734d6ec
2 changed files with 8 additions and 2 deletions
+5 -2
View File
@@ -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)
}
}
+3
View File
@@ -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"))
}