From 01c78d57fd55de785fbf5656608ca386dc1f863b Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 17 Dec 2019 08:55:23 -0500 Subject: [PATCH] internal/imports: set the Dir field on the build.Context (instead of WorkingDir) if present Updates golang/go#36168 Change-Id: Ibdb277176a28da72d85a4c7c8ed7c903c278125e Reviewed-on: https://go-review.googlesource.com/c/tools/+/211599 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- internal/imports/fix.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index f531024da93..818d44383fa 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -784,12 +784,20 @@ func (e *ProcessEnv) buildContext() *build.Context { ctx.GOROOT = e.GOROOT ctx.GOPATH = e.GOPATH - // As of Go 1.14, build.Context has a WorkingDir field + // As of Go 1.14, build.Context has a Dir 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) + rc := reflect.ValueOf(&ctx).Elem() + dir := rc.FieldByName("Dir") + if !dir.IsValid() { + // Working drafts of Go 1.14 named the field "WorkingDir" instead. + // TODO(bcmills): Remove this case after the Go 1.14 beta has been released. + dir = rc.FieldByName("WorkingDir") } + if dir.IsValid() && dir.Kind() == reflect.String { + dir.SetString(e.WorkingDir) + } + return &ctx }