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

Unpredictable "Code generated by" comment when invoking peg with "go run" #129

Open
mark-rushakoff opened this issue Jul 26, 2022 · 2 comments

Comments

@mark-rushakoff
Copy link

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:

peg/main.go

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.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.
mark-rushakoff added a commit to tendermint/tendermint that referenced this issue Jul 26, 2022
The code to generate the pubsub queries was dependent on an unspecified
version of the peg tool. This brings peg into go.mod so it is on a fixed
version. This should also enable dependabot to notify us of future
updates to peg.

The version of query.peg.go generated from the current version of peg
correctly contains the special "Code generated by..." line to indicate
to other tools that the file is automatically generated and should
therefore be excluded from linters, etc.

I removed the make target as there were no git grep results referencing
"gen_query_parser"; directly running "go generate" is a reasonable
expectation in Go projects.

Now that "go run" is module aware, I would typically use "go run" inside
the go:generate directive, but in this case we go build to a gitignore-d
directory in order to work around the nondeterministic output detailed
in pointlander/peg#129.
mark-rushakoff added a commit to tendermint/tendermint that referenced this issue Jul 27, 2022
* libs/pubsub/query: specify peg version in go.mod

The code to generate the pubsub queries was dependent on an unspecified
version of the peg tool. This brings peg into go.mod so it is on a fixed
version. This should also enable dependabot to notify us of future
updates to peg.

The version of query.peg.go generated from the current version of peg
correctly contains the special "Code generated by..." line to indicate
to other tools that the file is automatically generated and should
therefore be excluded from linters, etc.

I removed the make target as there were no git grep results referencing
"gen_query_parser"; directly running "go generate" is a reasonable
expectation in Go projects.

Now that "go run" is module aware, I would typically use "go run" inside
the go:generate directive, but in this case we go build to a gitignore-d
directory in order to work around the nondeterministic output detailed
in pointlander/peg#129.

* libs/pubsub/query: check error from (*QueryParser).Init()

The newly generated peg code returns an error from Init(); the previous
version was niladic.

Co-authored-by: Sam Kleinman <[email protected]>
@matthewmueller
Copy link

+1, I'd love for this to be fixed!

@mrclmr
Copy link
Contributor

mrclmr commented Nov 27, 2024

@mark-rushakoff @matthewmueller

Did #138 fix this? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants