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 golang.org/dl 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 golang.org/x/xerrors 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.

¹ https://golang.org/doc/go1.8#gopath

Change-Id: Ia65e6cbb07d69481214708afaee15adb104f782c
Reviewed-on: https://go-review.googlesource.com/c/dl/+/214386
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2 files changed