go: proposal: cmd/gofmt: add option to insert missing end-of-line commas in program
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
go version go1.7.4 darwin/amd64
What operating system and processor architecture are you using (go env)?
GOARCH=“amd64” GOBIN=“” GOEXE=“” GOHOSTARCH=“amd64” GOHOSTOS=“darwin” GOOS=“darwin” GOPATH=“/Users/rhysd/.go” GORACE=“” GOROOT=“/usr/local/Cellar/go/1.7.4_1/libexec” GOTOOLDIR=“/usr/local/Cellar/go/1.7.4_1/libexec/pkg/tool/darwin_amd64” CC=“clang” GOGCCFLAGS=“-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sv/mr3y8ytd7gzfdww8gxx568z80000gn/T/go-build764880956=/tmp/go-build -gno-record-gcc-switches -fno-common” CXX=“clang++” CGO_ENABLED=“1”
What did you do?
If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best.
First, save below code as blah.go
package main
func main() {
a := []int{
1,
2
}
}
Then execute below command
$ gofmt blah.go
What did you expect to see?
Automatically fix trailing comma at 2 (line 6, column 9) and formatting has been done successfully.
What did you see instead?
Formatting failed with below error
blah.go:6:4: missing ',' before newline in composite literal
I believe this is because gofmt can’t format a code which contains syntax error. Trailing comma such as line 6 in above is a syntax error in Go. So gofmt cannot fix this. However, we tend to forget adding trailing comma when expose one line code to multi-line code. If gofmt can fix it automatically, we no longer need to take care about trailing commas.
I guess implementing this is not so difficult. When parsing failed, then fix the source and retry to parse it again.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 24 (24 by maintainers)
gofmtis a code formatter, I don’t think it should be required to handle invalid go code, much less change it to correct errors.It also goes down a slippery slope. What syntax errors should be fixed? Where do you draw the line?
If the input said:
then inserting a comma after 2 could possibly be breaking the program instead of helpfully fixing it. This seems undeniably convenient, but also dangerous. And it opens a slippery slope for other “help”.
Also, gofmt is the “cat” of Go programs. There can be other programs that process them instead. Not everything has to go into gofmt.
I’ve been assuming that editor integration would make it easy to jump from the error message to the exact place noted in the error message, and the message itself says “put a comma here”. That seems pretty direct and easy. If any editor integrations do not make it easy to jump from error messages to the named locations, please file bugs against them.
-rsc for @golang/proposal-review
It will be more approval than this proposal, I guess. 👍
Oh, that’s true. I didn’t know it’s valid syntax because I’m always using gofmt before compile.
But my main point is, if go/parser can correctly created ast node, why not using it instead of discarding it with trivial error.
How about adding a new option to ignore parse error if no bad node is in AST? I tried modifying gofmt code as below.
https://github.com/rhysd/gofmtrlx
https://github.com/rhysd/gofmtrlx/commit/76c93b220c40631f1e8e7385ecb4685923526c21
I found that there is no bad node in AST even if trailing comma error occurs. So, as gofmt is formatter, it can ignore the parse error, just format the code even if parse error is contained when there is no bad node in AST. How do you think?
I agree that adding new tool like this is bike-shedding… But is there any chance to add more generic tool like
go fix? For example,go fix importsdoes the same asgoimports.go fix trailling-commafixes commas, …