minion
minion

Reputation: 1998

is there a way that go generate will skip unchanged files/ pkg

Is there an easy way to run go generate ./... and in case that all the file in the pkg that call the generate didn't change- that go generate skip generating again this file? (the reason is time consumption on generate) For example:

-a (directory)
  example1.go //contain //go:generate -output generated_example1.go
  example2.go //contain //go:generate -output generated_example2.go
...

lets say i have 7 files to generate, and I had changed only file 'example1.go' so when i run go generate ./... I would like that it will not try to generate all the 7 files but only example1 because only this source has changed

(calling the go generate from inner directory will not solve the issue because there are couple of files to generate from same directory.)

Upvotes: 2

Views: 382

Answers (1)

icza
icza

Reputation: 417402

This is not supported. The tool (command) you run with go generate may support this. See related: How to run go generate only for changed templates?

Some reasoning: go generate may be used to run arbitrary tool, not just source generation / manipulation tools, and running again may not have the same output / side effect. Also, running the tools may fail, and go generate does not keep a database of successful / failed runs, in which case it would not know which commands to run again even if source files didn't change.

All-in-all, your tool should check the existence of the desired output files (and their versions) to decide if (re-)generation is needed. This is easier than one might think though: since generation runs after the source file change, it's enough to compare the last modified timestamps. If the source (containing //go:generate) has a newer timestamp, generation should run. Otherwise it can be skipped. Note that go generate sets the $GOFILE env var to the base name of the file that triggered the command, so it's easy to check this.

Upvotes: 3

Related Questions