Repository navigation
Description
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.pegBut 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:
Line 87 in e7588a8
| 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.pegThen, 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.
Activity
libs/pubsub/query: specify peg version in go.mod
libs/pubsub/query: specify peg version in go.mod (#9099)
matthewmueller commentedon Nov 9, 2024
+1, I'd love for this to be fixed!
mrclmr commentedon Nov 27, 2024
@mark-rushakoff @matthewmueller
Did #138 fix this? :)
lrstanley commentedon Oct 9, 2025
It looks like #138 did fix this problem. Though, I see a new release hasn't been tagged in a long time, so it will continue to be an issue for those targeting
latest, so right now you'd have to tagmaster, which isn't ideal.mrclmr commentedon Oct 9, 2025
@pointlander
dolmen commentedon May 18, 2026
Nowadays we should tell users to register
pegas a tool (go get -tool github.com/pointlander/peg@main), and usego tool pegin//go:generatelines.I'm working on patches...
dolmen commentedon May 21, 2026
The README now (#193) tells to use
pegviago tool peg.