From e26a8c8a3c422434e9f7c5858e73f7c1e79265da Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 28 Oct 2019 11:50:45 -0400 Subject: [PATCH] internal/imports: set ctx.WorkingDir if such a field exists CL 203820 removes an assumption in go/build that srcDir is in the main module, since in general it need not be. That requires the use of some other mechanism for callers to communicate the correct location of the main module. Fortunately, we already have a WorkingDir field on the ProcessEnv struct that does exactly that. We can simply propagate it through if the corresponding field is present on go/build.Context. Updates golang/go#34860 Change-Id: Idabf9ae06d8383a72772d5a589fae1d10f206c01 Reviewed-on: https://go-review.googlesource.com/c/tools/+/203857 Reviewed-by: Heschi Kreinick --- internal/imports/fix.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index 11a076942f..3e6e56df1e 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -17,6 +17,7 @@ import ( "os/exec" "path" "path/filepath" + "reflect" "sort" "strconv" "strings" @@ -701,6 +702,13 @@ func (e *ProcessEnv) buildContext() *build.Context { ctx := build.Default ctx.GOROOT = e.GOROOT ctx.GOPATH = e.GOPATH + + // As of Go 1.14, build.Context has a WorkingDir field + // (see golang.org/issue/34860). + // Populate it only if present. + if wd := reflect.ValueOf(&ctx).Elem().FieldByName("WorkingDir"); wd.IsValid() && wd.Kind() == reflect.String { + wd.SetString(e.WorkingDir) + } return &ctx }