Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/gofmt: unexpected formatting of comments #22334

Open
hughdavenport opened this issue Oct 19, 2017 · 7 comments
Open

cmd/gofmt: unexpected formatting of comments #22334

hughdavenport opened this issue Oct 19, 2017 · 7 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hughdavenport
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/xxx/go"
GORACE=""
GOROOT="/usr/lib64/go"
GOTOOLDIR="/usr/lib64/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="x86_64-pc-linux-gnu-gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build052197988=/tmp/go-build -gno-record-gcc-switches"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

The following code (https://play.golang.org/p/yWr8ET9ZUg) is laid out with comments as I would expect:

package main

import "os/exec"

func main() {
	exec.Command("nmap",
		"-sV",          // Get version information
		"-oA", "blah",  // Save all versions as output
		"-oG", "-",     // Output Greppable format to stdout
		"-Pn",          // Skip Ping scan to force scanning everything
		"-p-",          // Scan all ports
		"-v",           // Increase verbosity (shows down hosts now)
	)
}

However, the output of gofmt gives the following (https://play.golang.org/p/My_W9bkZrY):

package main

import "os/exec"

func main() {
	exec.Command("nmap",
		"-sV",         // Get version information
		"-oA", "blah", // Save all versions as output
		"-oG", "-", // Output Greppable format to stdout
		"-Pn", // Skip Ping scan to force scanning everything
		"-p-", // Scan all ports
		"-v",  // Increase verbosity (shows down hosts now)
	)
}

It seems to appear that the first two lines are treated as one block, the third line by itself, and the last three lines are treated as another block.

What did you expect to see?

I would expect no change in the formating of the comments

What did you see instead?

The comments changed, as shown above.

First time bug for me, so hopefully this is OK. Let me know if there is anything more you need. I'm also keen to help fix it, but I'm also new to go so may take a while :)

Cheers,

Hugh

@ianlancetaylor ianlancetaylor changed the title Unexpected formatting of comments in gofmt cmd/gofmt; unexpected formatting of comments Oct 19, 2017
@ianlancetaylor ianlancetaylor changed the title cmd/gofmt; unexpected formatting of comments cmd/gofmt: unexpected formatting of comments Oct 19, 2017
@ianlancetaylor
Copy link
Contributor

This is just how it works.

I don't think gofmt is anybody's favorite formatter. It's just good enough to be something that everyone can agree on.

@griesemer
Copy link
Contributor

griesemer commented Oct 19, 2017

Interesting. This is actually working as intended. The problem is that the usual alignment computation breaks down when there's more then one entry per line. For comparison, see the result of this example where I made the lines all have one entry but have the same overall length: https://play.golang.org/p/4qanGJEhr1 . This time it works.

@ianlancetaylor is correct, this is working as intended, even though it's unexpected. I will leave this open for now, perhaps there's a not too difficult way to fix this.

@griesemer griesemer added this to the Unreleased milestone Oct 19, 2017
@hughdavenport
Copy link
Author

Hey,

That makes sense, I wouldn't normally do two things per line like this, but felt like it made sense for this code. Filed the bug purely because I thought the output looked like badly formatted code.

When I get a free chunk of time I'll take a look at how it does it and see whether there is an easy and sane way to make it look nicer.

Cheers,

Hugh

@yogesh-desai
Copy link

Also, what about the normal code formatting as below? Currently, gofmt does the work if it is a declaration block inside a var ( ) block. But it will be really good to see the normal code as well. In my opinion, it makes the code more readable.
Giving following example to understand more,
What is currently,

	aasdf := 1234
	a := 1234567
	xsadf := "asdfghjkl"

What is expected,


	aasdf := 1234
	a     := 1234567
	xsadf := "asdfghjkl"

Is this type of formatting possible with gofmt?

@griesemer
Copy link
Contributor

@yogesh-desai gofmt doesn't do this now. It's possible to do it, but it requires a change. It's not a high priority.

@yogesh-desai
Copy link

@griesemer I would like to help to solve it. Please let me know so can take care of this change.

@griesemer
Copy link
Contributor

@yogesh-desai Thanks for your offer, but not now.

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants