From 09833da6b4cfb494ba61a398ab2063ab016d3890 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 29 Jun 2020 10:44:56 -0400 Subject: [PATCH] cmd/fix: always format source file before fixing This makes the changes to the file easier to explain. Not all the changes may come from the fixers directly, if the file is not gofmt-ed already. Change-Id: I81776da446a34a1239a3130317d2aae1437d61a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/240555 Trust: Russ Cox Reviewed-by: Jay Conrod --- src/cmd/fix/main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go index e72c66398f8..d19dde6b4ae 100644 --- a/src/cmd/fix/main.go +++ b/src/cmd/fix/main.go @@ -137,6 +137,21 @@ func processFile(filename string, useStdin bool) error { return err } + // Make sure file is in canonical format. + // This "fmt" pseudo-fix cannot be disabled. + newSrc, err := gofmtFile(file) + if err != nil { + return err + } + if !bytes.Equal(newSrc, src) { + newFile, err := parser.ParseFile(fset, filename, newSrc, parserMode) + if err != nil { + return err + } + file = newFile + fmt.Fprintf(&fixlog, " fmt") + } + // Apply all fixes to file. newFile := file fixed := false @@ -180,7 +195,7 @@ func processFile(filename string, useStdin bool) error { // output of the printer run on a standard AST generated by the parser, // but the source we generated inside the loop above is the // output of the printer run on a mangled AST generated by a fixer. - newSrc, err := gofmtFile(newFile) + newSrc, err = gofmtFile(newFile) if err != nil { return err }