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

x/tools/cmd/bundle: discards some comments #20548

Closed
dmoklaf opened this issue Jun 1, 2017 · 17 comments
Closed

x/tools/cmd/bundle: discards some comments #20548

dmoklaf opened this issue Jun 1, 2017 · 17 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dmoklaf
Copy link

dmoklaf commented Jun 1, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 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/raphaelgeronimi/Local/Sabrina"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yv/6zwrsm6j6kqgj6vr3hf0_zbw0000gn/T/go-build481562037=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I looked at http/h2_bundle.go code

What did you expect to see?

No empty if...then blocks

What did you see instead?

In http2Server.ServeConn, there is a useless if...then block around lines 3173-3181:

		if sc.tlsState.ServerName == "" {
  
  		}
  
  		if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) {
  
  			sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
  			return
  		}

This empty block looks strange, beyond being useless, I dont know if this could create a security issue. The empty line within the next if...then block right below looks strange too.

This also stresses the need to increase linters usage within the go project itself. There are linters which report that kind of error, it's surprising that they are used in most golang projects but still not used within the core project itself.

@ALTree
Copy link
Member

ALTree commented Jun 1, 2017

That file is autogenerated. The original one has a comment explaining what's going on

https://github.com/golang/net/blob/a8e8f92cd65f5ff48599999fa4001d9b95ecea85/http2/server.go#L400

if sc.tlsState.ServerName == "" {
		// Client must use SNI, but we don't enforce that anymore,
		// since it was causing problems when connecting to bare IP
		// addresses during development.
		//
		// TODO: optionally enforce? Or enforce at the time we receive
		// a new request, and verify the the ServerName matches the :authority?
		// But that precludes proxy situations, perhaps.
		//
		// So for now, do nothing here again.
}

I guess the bundle script removes comments.

@dmoklaf
Copy link
Author

dmoklaf commented Jun 1, 2017 via email

@mvdan
Copy link
Member

mvdan commented Jun 1, 2017

The bundle godoc (https://godoc.org/golang.org/x/tools/cmd/bundle) says that comments may not be preserved. I assume this is to save lines of code, since you can quickly end up with very large files. It does seem to keep comments that serve as godoc for package members, though.

I believe the h2_bundle.go file is not supposed to be human-readable or editable. After all, it sits at nearly 10k lines of code. You should look at the source files in golang.org/x/net/http2 instead.

@ALTree
Copy link
Member

ALTree commented Jun 2, 2017

Closing this, since 1) apparently there's no issue with the code 2) as mvdan says, autogenerated files are not really supposed to be readable and the header points to the original source

@ALTree ALTree closed this as completed Jun 2, 2017
@bradfitz bradfitz changed the title http: empty if...then block x/tools/cmd/bundle: discards some comments but not all Jun 7, 2017
@bradfitz
Copy link
Contributor

bradfitz commented Jun 7, 2017

I'm going to reopen this and repurpose it towards fixing the bundle too.

I think h2_bundle.go should be readable or totally gibberish, not halfway in the middle. But I'd prefer readable since we're 95% of the way there.

@alandonovan, do you know why we're losing some comments but only a few?

@bradfitz bradfitz reopened this Jun 7, 2017
@bradfitz bradfitz added this to the Go1.9Maybe milestone Jun 7, 2017
@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 7, 2017
@dmoklaf
Copy link
Author

dmoklaf commented Jun 7, 2017 via email

@bradfitz
Copy link
Contributor

bradfitz commented Jun 7, 2017

Go doesn't permit circular dependencies so we can't have net/http import http2, since http2 already depends on net/http (for types, etc).

@dmoklaf
Copy link
Author

dmoklaf commented Jun 7, 2017 via email

@alandonovan
Copy link
Contributor

I'm not sure yet why bundle is losing some comments, but the way in which comments are associated with syntax nodes has always been a weak aspect of the design of the go/ast package.

Deleting all comments is not an option because several kinds of special comment have semantics in the compiler.

@alandonovan alandonovan changed the title x/tools/cmd/bundle: discards some comments but not all x/tools/cmd/bundle: discards some comments Jun 7, 2017
@bradfitz
Copy link
Contributor

bradfitz commented Jun 7, 2017

Maybe @griesemer could help.

@griesemer
Copy link
Contributor

x/tools/cmd/bundle's main.go contains a comment:

// TODO(adonovan): this may cause loss of comments
// preceding or associated with the package or import
// declarations or not associated with any declaration.
// Check.

i.e., this is a known problem.

As @adonovan pointed out, comment handling is a weak spot in the go/ast package (which is one of the very first Go packages ever and which is suffering a bit from age). The problem is that comments are not attached to ast nodes, unless they are documenting a declaration. Instead, they are "free-floating" in a list associated with the ast.File node, and must be "interspersed" at the right place based on their positions (and the respective node positions).

I haven't looked at main.go in detail but the call to format.Node (main.go:362) simply takes a declaration node which won't contain any inner comments.

There is (somewhat tedious) way to address this: The [begin, end[ source interval for each declaration node can be used to determine all the comments that belong to the declaration. That list of comments, together with the declaration node, may be be bundled up in a go/printer.CommentedNode, which in turn is provided to the format.Node call. If the node and comment positions are correct (unchanged), the comments should be correctly interspersed.

Leaving for somebody else to pick this up.

@bradfitz
Copy link
Contributor

bradfitz commented Jun 8, 2017

/cc @fatih @hirochachacha (if either of you are interested in helping)

@gopherbot
Copy link

CL https://golang.org/cl/45117 mentions this issue.

@fatih
Copy link
Member

fatih commented Jun 9, 2017

@bradfitz I'll take a look at it.

@fatih
Copy link
Member

fatih commented Jun 9, 2017

oops, seems like @hirochachacha was faster than me :)

@hirochachacha
Copy link
Contributor

It seems printer package already addressed @griesemer 's suggestion.
https://github.com/golang/go/blob/master/src/go/printer/printer.go#L1055-L1081
So the fix could be simple.

@gopherbot
Copy link

CL https://golang.org/cl/45158 mentions this issue.

gopherbot pushed a commit that referenced this issue Jun 9, 2017
The golang.org/x/tools/cmd/bundle tool previously had a bug where it
dropped some comments.

This regenerates it with the fixed version (https://golang.org/cl/45117).

(Upstream is still git rev 3470a06c1, from https://golang.org/cl/44331)

Updates #20548

Change-Id: Ic5d9208a0c8f7facdb7b315c6acab66ace34c0a9
Reviewed-on: https://go-review.googlesource.com/45158
Reviewed-by: Hiroshi Ioka <hirochachacha@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@golang golang locked and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

9 participants