golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.38k stars 17.7k forks source link

proposal: cmd/go: add a -watch option to go generate #36414

Closed azr closed 4 years ago

azr commented 4 years ago

TLDR: add go generate -watch to watch for changes in a folder to run go generate when a file is changed.

I think go generate is really awesome. In the packer repo that I help maintain, a lot of code and docs are "go generated" and I think it would be nice to not have to think about running go generate every-time I change a file: making my programming a bit less disrupted; moreover this would make performances of go generate less of a problem. Right now a go generate is taking 8 minutes for us.

My gut feeling tells me that making/maintaining this would be way too complicated for the gain but may be I'm wrong !

In the meantime I'll be using https://github.com/fsnotify/fsnotify

ianlancetaylor commented 4 years ago

Thanks for the suggestion. However, the go generate command doesn't have any way to notice which inputs files matter to go generate, so it would have to watch for all changes. I think most people would rather watch just for changes to the file to matter, which would require a custom solution. And for that matter it would be a fairly simple custom solution to wait for any change in a directory and invoke go generate, which is what go generate -watch would do. So I tend to think 1) this would not do what most people want; 2) for the people who do want this, it can be done fairly easily. So to me this doesn't seem worth doing in the go tool.

mvdan commented 4 years ago

I think another reason not to do this is that we'd probably have to do it consistently for go build, go test, and so on. The complexity grows rapidly, in my mind. For example, besides watching for changes in all Go source including dependencies, go test -watch would need to somehow watch all testdata files and directories too.

myitcv commented 4 years ago

Just to add one final comment that it's already possible to use a file watcher (e.g. inotifywait, fswatch etc) to run commands (e.g. go generate) when files change.

Building on @ianlancetaylor's point:

However, the go generate command doesn't have any way to notice which inputs files matter to go generate, so it would have to watch for all changes

This was part of the motivation behind my work on the experimental gogenerate. That way you amortise the cost of repeatedly running gogenerate, because if you have a cache hit no work will be done.

I'd be happy to continue discussion/questions in that repo

azr commented 4 years ago

I understand and agree; closing this one for ya ! Thanks for taking the time to answer ! 🙂