caddy icon indicating copy to clipboard operation
caddy copied to clipboard

`caddy fmt` will silently ignore all but the first file path passed to it

Open alexwlchan opened this issue 1 year ago • 4 comments

If you call caddy fmt with the path to more than one file, it only formats the first file.

For example:

$ caddy fmt 1.Caddyfile 2.Caddyfile --overwrite

then 1.Caddyfile will be reformatted, but 2.Caddyfile will be silently ignored.

If I look in the docs, I can see that caddy fmt only supports passing a single path – but it took me a while to realise that! This is different from other formatting tools I've used (e.g. black, terraform fmt) where you can pass multiple files and format them all together.

I think it would be less confusing if caddy fmt either:

  • Formatted every <path> passed to it, or
  • Threw an error if you tried to pass more than one <path>

Context

I'd written a command to find all the Caddyfiles in my repo and format them:

$ find . -name Caddyfile | xargs --verbose caddy fmt
caddy fmt 1/Caddyfile 2/Caddyfile

and I was getting inconsistent results between different machines – it took me a while to notice that find was returning results in an inconsistent order, and caddy was only formatting the first in the list.

I don't really mind what caddy does, but the current behaviour caused a lot of confusion.

Version

$ caddy --version
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

alexwlchan avatar Nov 22 '24 16:11 alexwlchan

Thanks for the report. Should be an easy fix, I suppose -- anyone is welcome to contribute!

mholt avatar Nov 22 '24 17:11 mholt

Noting in case I have time to pick this up: this looks like the relevant function. https://github.com/caddyserver/caddy/blob/eddbccd298f637c4785c891f5f96dbf103580fa8/cmd/commandfuncs.go#L561-L619

alexwlchan avatar Nov 22 '24 17:11 alexwlchan

Well, you can only have one root Caddyfile, so I don't think it really makes sense to try to format more than one. If you're doing that because you have import etc, then I think we should update fmt to follow imports, possibly, and also format those files (when -w is used).

So I think yeah disallowing more than one file probably makes sense.

Also while we're at it I think we could add --config as a flag (which is the same as the one arg, and if set args can be empty), cause most of Caddy's other commands use --config/-c to specify the config file, ran into that on occasion as a footgun when I'm hitting UP in my terminal to edit the command and run another.

francislavoie avatar Nov 22 '24 17:11 francislavoie

If you're doing that because you have import etc, then I think we should update fmt to follow imports, possibly, and also format those files (when -w is used).

Yeah, imported files is precisely where I ran into this.

alexwlchan avatar Nov 22 '24 17:11 alexwlchan

Is this issue still an issue given the merged pull request?

keystroke3 avatar Jun 02 '25 13:06 keystroke3