internal/genv: find directory to write output with module mode in mind

The goal of this change is to prevent genv from potentially writing
its output in the wrong location.

genv has been relying on os.Getenv("GOPATH") to figure out where to
write its output. This stopped working reliably after Go 1.8 started
having a default GOPATH value.¹ Back then, it was possible to start
using 'go env GOPATH' instead. However, with the introduction of
module mode in Go 1.11, there isn't a way to reliably pinpoint the
canonical location of a development copy of a module, because that
module can be placed in anywhere on the system.

So, start requiring that genv is run inside the module.
At that point, it is possible to find the module root reliably.

Add a Go 1.13+ build constraint in order to use errors.As without
adding a requirement to module. genv is a tool
used only by contributors to this module, so it doesn't need to
support Go 1.12.

Also add a missing copyright header to a test file.


Change-Id: Ia65e6cbb07d69481214708afaee15adb104f782c
Run-TryBot: Dmitri Shuralyov <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Alexander Rakoczy <>
Reviewed-by: Carlos Amedee <>
2 files changed