segmentio / golines

A golang formatter that fixes long lines
MIT License
903 stars 56 forks source link

line break between /* … */ comments and package statement removed #110

Open matthewhughes934 opened 10 months ago

matthewhughes934 commented 10 months ago

Given the following file (file.go):

/*
this is a description of the package
*/
package lib

func SomeLongFuncNameToBreakLineLength() {
}

Then running golines with a length that ensure we do some formatting removes the newline between the closing */ and the package declaration:

$ golines --max-len=20 file.go 
/*
this is a description of the package
*/package lib

func SomeLongFuncNameToBreakLineLength() {
}

If --max-len is not given, golines won't actually try and format anything and the the newline is preserved:

$ golines --max-len=20 file.go 
/*
this is a description of the package
*/
package lib

func SomeLongFuncNameToBreakLineLength() {
}

This appears to be related to an upstream issue in github.com/dave/dst/ https://github.com/dave/dst/issues/69 (but sharing here for visibility). Since golines bails early if there's nothing to format https://github.com/segmentio/golines/blob/d7d11e6ae11721865229b1d55b8862c8bd4ca719/shortener.go#L114 in that case we don't call dst.{Parse/Printf} and hence the issue isn't triggered.

There's a fix for the issue upstream (https://github.com/dave/dst/commit/5fa8d6ebe49a6b04afa15ee8f982d210f8a00b80) but there's not been a new tag to include it. Testing that with go get github.com/dave/dst@5fa8d6ebe49a6b04afa15ee8f982d210f8a00b80 and I see the issue is resolved.

EDIT: upstream has created a new tag, here's a PR to include it https://github.com/segmentio/golines/pull/111