]> Cypherpunks repositories - gostls13.git/commit
cmd/gofmt: return a proper error for empty Go files
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 18 Mar 2022 10:57:02 +0000 (10:57 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 24 Mar 2022 09:17:34 +0000 (09:17 +0000)
commit0ed31eb73b13bd57aff727f4ab759c6701d45a01
treeed01b419141ffabe594f2c6f75c34dad49693314
parent9058080319e5e3b8edbde8c5e2406b11bac163ee
cmd/gofmt: return a proper error for empty Go files

I was testing edge cases in gofumpt, a fork of gofmt,
and noticed that gofmt will return a bare io error on empty files,
as demonstrated by the added test case without a fix:

        > ! exec $GOROOT/bin/gofmt empty.go nopackage.go
        [stderr]
        EOF
        nopackage.go:1:1: expected 'package', found not

The problem is the code that detects concurrent modifications.
It relies on ReadFull and correctly deals with io.ErrUnexpectedEOF,
but it did not pay attention to io.EOF, which can happen when size==0.

Change-Id: I6092391721edad4584fb5922d3e3a8fb3da86493
Reviewed-on: https://go-review.googlesource.com/c/go/+/393757
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Trust: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/cmd/go/testdata/script/fmt_load_errors.txt
src/cmd/gofmt/gofmt.go