Repository: caddyserver/caddy (language: go)

Issue:
using the file ext caddyfile for a config file does not work
```
./caddy run  -c test.caddyfile
2024/05/31 14:25:55.237 INFO    using config from file  {"file": "test.caddyfile"}
Error: config is not valid JSON: invalid character ':' looking for beginning of value; did you mean to use a config adapter (the --adapter flag)?
```

test.caddyfile
```
:2015
respond "Hello, world!"
```


Discussion / hints:

Fix patch:
diff --git a/cmd/main.go b/cmd/main.go
index 31a121aa6ee..1f7d6156ba5 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -163,9 +163,18 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
 	// caddyfile adapter for convenience
 	baseConfig := strings.ToLower(filepath.Base(configFile))
 	baseConfigExt := filepath.Ext(baseConfig)
-	if (strings.HasPrefix(baseConfig, "caddyfile") ||
-		strings.HasSuffix(baseConfig, ".caddyfile")) &&
-		(len(baseConfigExt) == 0 || caddyconfig.GetAdapter(baseConfigExt[1:]) == nil) &&
+	startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
+
+	// If the adapter is not specified, the config file is not starts with "caddyfile", and isn't a JSON file (e.g. Caddyfile.yaml),
+	// then we don't know what the config format is.
+	if adapterName == "" && startsOrEndsInCaddyfile && baseConfigExt != ".caddyfile" && baseConfigExt != ".json" {
+		return nil, "", fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
+	}
+
+	// If the config file starts or ends with "caddyfile",
+	// the extension of the config file is not ".json", AND
+	// the user did not specify an adapter, then we assume it's Caddyfile.
+	if startsOrEndsInCaddyfile &&
 		baseConfigExt != ".json" &&
 		adapterName == "" {
 		adapterName = "caddyfile"


Test patch: