cmd/cue: check all file I/O errors in fmt
First, we weren't checking any errors while opening and parsing CUE
files for formatting via Decoder.Err. This is somewhat harmless, as
cue/load currently opens all of those files, so any error like "file
does not exist" or "file cannot be read" would likely already be caught.
However, we called Encoder.Close without checking its returned error,
and it turns out that when encoding into a filename on disk,
that ultimately returns the ioutil.WriteFile error.
This meant that we ignored any errors while opening, writing, and
closing the destination file, potentially doing less work silently.
This is what happened in #1791. Before the fix, I can reliably reproduce
the problem by lowering my hard limit on open files like so:
$ ulimit -n 256
With 500 badly formatted CUE files, we would run:
$ cue fmt *.cue
And the result was that, without an error, only 249 files were changed
on disk. After the fix, the error is very clear:
$ cue fmt *.cue
open [...]/250.cue: too many open files
Note that this CL only fixes the ignored errors, not the cause of the
"too many open files" error.
Add a test; with a read-only file containing badly formatted CUE,
`cue fmt` used to hide the fact that it failed to write to the file.
That makes for a fairly simple error test.
We can't test the ignored Decoder error as easily, because as explained
above, most reasonable errors will already be caught by cue/load today.
Updates #1791.
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ic4beed9c6139194d71566722cf0f8749fa8150d6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552624
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>