Since go run has been made module aware, it is convenient to use go run with //go:generate directives, so that your project is able to trivially use a fixed version of its external dependencies.
I want to write my go:generate directive like this:
$ git grep go:generate
peg.go://go:generate go run github.com/pointlander/peg -inline -switch query.peg
But go run builds the target binary in a temporary directory, and main.go passes the entirety of os.Args to the template, such that os.Args[0] contains the full path to the built peg binary in a random temporary directory:
|
if err = p.Compile(*filename, os.Args, out); err != nil { |
This results in a diff every time go generate has been run:
$ go generate && git grep 'Code generated by'
query.peg.go:// Code generated by /var/folders/_m/h25_32y958gbgk67m97141400000gq/T/go-build1253021897/b001/exe/peg -inline -switch query.peg DO NOT EDIT.
$ go generate && git grep 'Code generated by'
query.peg.go:// Code generated by /var/folders/_m/h25_32y958gbgk67m97141400000gq/T/go-build3041684327/b001/exe/peg -inline -switch query.peg DO NOT EDIT.
(The go-build portion of the directory is different on each invocation above.)
It would be nice if os.Args[0] was just set to peg by default, but if it is important to maintain backwards compatibility, you could add a new flag to peg. I would lean towards something like -fixedname to mean "just set os.Args[0] to peg regardless of its actual value". Another option would be something like -arg0name=peg, but I doubt anyone would need it customized to anything than some arbitrarily fixed name, hence my preference to a simple boolean flag.
In the meantime, I can work around this by changing my //go:generate to just build the binary into a fixed directory:
$ git grep go:generate
peg.go://go:generate go build -o ./.bin/peg github.com/pointlander/peg
peg.go://go:generate ./.bin/peg -inline -switch query.peg
Then, the comment does not change on subsequent go generate calls.
$ go generate && git grep 'Code generated by'
query.peg.go:// Code generated by ./.bin/peg -inline -switch query.peg DO NOT EDIT.
$ go generate && git grep 'Code generated by'
query.peg.go:// Code generated by ./.bin/peg -inline -switch query.peg DO NOT EDIT.
Since
go runhas been made module aware, it is convenient to usego runwith//go:generatedirectives, so that your project is able to trivially use a fixed version of its external dependencies.I want to write my
go:generatedirective like this:But
go runbuilds the target binary in a temporary directory, andmain.gopasses the entirety ofos.Argsto the template, such thatos.Args[0]contains the full path to the builtpegbinary in a random temporary directory:peg/main.go
Line 87 in e7588a8
This results in a diff every time go generate has been run:
(The
go-buildportion of the directory is different on each invocation above.)It would be nice if
os.Args[0]was just set topegby default, but if it is important to maintain backwards compatibility, you could add a new flag to peg. I would lean towards something like-fixednameto mean "just set os.Args[0] topegregardless of its actual value". Another option would be something like-arg0name=peg, but I doubt anyone would need it customized to anything than some arbitrarily fixed name, hence my preference to a simple boolean flag.In the meantime, I can work around this by changing my
//go:generateto just build the binary into a fixed directory:Then, the comment does not change on subsequent
go generatecalls.