go/printer: incorrect *Config.Format results with +build comments and the SourcePos mode #64976
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?8db1310 (tip, as of time of writing)
What did you do?
Here is an example reproducer: https://go.dev/play/p/sphL42iaX_a
I think it's a pretty obscure issue which requires a few things:
// +build
constraints, and no//go:build
comments//+build
comment which offsets position such that the printer has to emit a//line
directiveAs far as I can tell, here's what's going wrong:
printer.*Config.Fprint
will fully print the given node (a File) and then call fixGoBuildLines to consolidate build directives and translate "plus builds" to the current//go:build
formatp.plusBuild
, then the comment is written withp.writeString
SourcePos
is enabled and, if necessary, emits a line directive before writing its arg string to the output bufferAfter printing, fixGoBuildLines turns
//+build
comments into//go:build
by iterating the positions inp.plusBuild
and parsing the constraints at each:Because of SourcePos, the offsets recorded to
p.plusBuild
can incorrectly point to a//line
comment, rather than a valid build constraint, so parsing fails, the loop exits, and subsequent plusBuild lines are ignored and the inserted block will be incomplete/incorrect.What did you expect to see?
//+build
constraints should have been preserved and/or translated to//go:build
What did you see instead?
In the reproducer, one of the build constraints is omitted and a seemingly unnecessary line directive is printed ts the top of the file.
The text was updated successfully, but these errors were encountered: