From 757fd9ba55ff84f088e4cb037ea0a49b7f4211d0 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 10 Dec 2024 01:48:29 -0600 Subject: [PATCH] go.work file fixes --- build.go | 21 ++++++++++++++++----- goSrcFind.go | 15 +++++++++++++++ init.go | 11 ++++++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/build.go b/build.go index 8fee1be..cf1f518 100644 --- a/build.go +++ b/build.go @@ -19,6 +19,7 @@ import ( "strings" "time" + "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -36,10 +37,14 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err log.Warn("forge.doBuild repo == nil") return errors.New("forge.doBuild repo == nil") } - // always assume all sources have been downloaded - // todo: detect when in ~/go/src vs go.work mode - os.Setenv("GO111MODULE", "off") - defer os.Unsetenv("GO111MODULE") + if f.IsGoWork() { + // when building using a go.work file, never use GO111MODULE=off + os.Unsetenv("GO111MODULE") + } else { + // when building from ~/go/src, always use GO111MODULE=off + os.Setenv("GO111MODULE", "off") + defer os.Unsetenv("GO111MODULE") + } // get the version version := repo.GetCurrentBranchVersion() @@ -94,6 +99,12 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err cmd = append(cmd, "-ldflags", "-X "+flag) } + testenv := os.Getenv("GO111MODULE") + if testenv == "off" { + log.Info("GO111MODULE=off", "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc()) + } else { + log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc()) + } log.Info("running:", cmd) result := repo.RunRealtime(cmd) if result.Exit == 0 { @@ -107,7 +118,7 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err log.Warn("go build failed", cmd) pwd, _ := os.Getwd() log.Warn("go build pwd", pwd) - res2 := repo.RunRealtime(cmd) + res2 := shell.RunEcho(cmd) if res2.Exit == 0 { log.Info("again failed", res2.Exit) log.Info("again failed cmd", strings.Join(cmd, "a")) diff --git a/goSrcFind.go b/goSrcFind.go index b19e858..e776cf0 100644 --- a/goSrcFind.go +++ b/goSrcFind.go @@ -53,6 +53,21 @@ func useGoSrc() (string, error) { return pwd, nil } +func (f *Forge) goWorkExists() bool { + var err error + workFilePath := filepath.Join(f.GetGoSrc(), "go.work") + if _, err = os.Stat(workFilePath); err == nil { + log.Info("f.goWorkExists() found", workFilePath) + return true + } else if !os.IsNotExist(err) { + log.Info("f.goWorkExists() missing", workFilePath) + return false + } + // probably false, but some other error + log.Info("f.goWorkExists() os.Stat() error", err, workFilePath) + return false +} + func digup(path string) (string, error) { for { workFilePath := filepath.Join(path, "go.work") diff --git a/init.go b/init.go index 618bf60..ac4e092 100644 --- a/init.go +++ b/init.go @@ -34,9 +34,12 @@ func Init() *Forge { fullpath := filepath.Join(homeDir, ".config/forge") os.Setenv("FORGE_CONFIG", fullpath) } + if f.goWorkExists() { + f.goWork = true + } log.Info("forgepbb.Init() ~/go/src ", f.goSrc) log.Info("forgepbb.Init() 2 FORGE_CONFIG", os.Getenv("FORGE_CONFIG")) - log.Info("forgepbb.Init() 2 FORGE_GOSRC", os.Getenv("FORGE_GOSRC")) + log.Info("forgepbb.Init() 2 FORGE_GOSRC", os.Getenv("FORGE_GOSRC"), "f.goWork =", f.IsGoWork()) // cache.go has Do() // f.initOnce.Do(f.initWork) @@ -63,5 +66,11 @@ func Init() *Forge { f.ScanGoSrc() end := f.Repos.Len() log.Info("forge.ScanGoSrc() Found", end-start, "new repos in", f.goSrc) + testenv := os.Getenv("GO111MODULE") + if testenv == "off" { + log.Info("GO111MODULE=off", "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc()) + } else { + log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc()) + } return f }