diff --git a/pkg/build/build.go b/pkg/build/build.go index b6c27b9..f421978 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -41,7 +41,8 @@ func NewBuild(cmd *cobra.Command, args []string) *Build { func (b *Build) Build() { // 1. 拷贝至临时目录 b.copyProjectToTmp() - // defer b.clean() + defer b.clean() + log.Donef("project copied to temporary directory") // 2. inject cover vars cover.Inject() @@ -50,6 +51,8 @@ func (b *Build) Build() { } func (b *Build) doBuildInTemp() { + log.StartWait("building the injected project") + goflags := config.GocConfig.Goflags // 检查用户是否自定义了 -o oSet := false @@ -64,22 +67,19 @@ func (b *Build) doBuildInTemp() { goflags = append(goflags, "-o", config.GocConfig.CurWd) } - pacakges := config.GocConfig.TmpPkgDir - if config.GocConfig.ContainSpecialPattern { - pacakges = pacakges + "/..." - } + pacakges := config.GocConfig.Packages - goflags = append(goflags, pacakges) + goflags = append(goflags, pacakges...) args := []string{"build"} args = append(args, goflags...) // go 命令行由 go build [-o output] [build flags] [packages] 组成 cmd := exec.Command("go", args...) - cmd.Dir = config.GocConfig.TmpModProjectDir + cmd.Dir = config.GocConfig.TmpWd cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - log.Infof("go build cmd is: %v", cmd.Args) + log.Infof("go build cmd is: %v, in path [%v]", cmd.Args, cmd.Dir) if err := cmd.Start(); err != nil { log.Fatalf("fail to execute go build: %v", err) } @@ -88,5 +88,6 @@ func (b *Build) doBuildInTemp() { } // done + log.StopWait() log.Donef("go build done") } diff --git a/pkg/build/goenv.go b/pkg/build/goenv.go index 910e8df..70d3ae1 100644 --- a/pkg/build/goenv.go +++ b/pkg/build/goenv.go @@ -20,7 +20,7 @@ func (b *Build) readProjectMetaInfo() { config.GocConfig.GOPATH = b.readGOPATH() config.GocConfig.GOBIN = b.readGOBIN() // 获取 [packages] 及其依赖的 package list - pkgs := b.listPackages(config.GocConfig.CurPkgDir) + pkgs := b.listPackages(config.GocConfig.CurWd) // get mod info for _, pkg := range pkgs { @@ -35,8 +35,8 @@ func (b *Build) readProjectMetaInfo() { break } - // 如果包目录不是工程根目录,那再次 go list 一次,获取整个工程的包信息 - if config.GocConfig.CurPkgDir != config.GocConfig.CurModProjectDir { + // 如果当前目录不是工程根目录,那再次 go list 一次,获取整个工程的包信息 + if config.GocConfig.CurWd != config.GocConfig.CurModProjectDir { config.GocConfig.Pkgs = b.listPackages(config.GocConfig.CurModProjectDir) } else { config.GocConfig.Pkgs = pkgs @@ -44,8 +44,8 @@ func (b *Build) readProjectMetaInfo() { // get tmp folder name config.GocConfig.TmpModProjectDir = filepath.Join(os.TempDir(), tmpFolderName(config.GocConfig.CurModProjectDir)) - // get cur pkg dir in the corresponding tmp dir - config.GocConfig.TmpPkgDir = filepath.Join(config.GocConfig.TmpModProjectDir, config.GocConfig.CurPkgDir[len(config.GocConfig.CurModProjectDir):]) + // get working dir in the corresponding tmp dir + config.GocConfig.TmpWd = filepath.Join(config.GocConfig.TmpModProjectDir, config.GocConfig.CurWd[len(config.GocConfig.CurModProjectDir):]) // get GlobalCoverVarImportPath config.GocConfig.GlobalCoverVarImportPath = path.Join(config.GocConfig.ImportPath, tmpFolderName(config.GocConfig.CurModProjectDir)) log.Donef("project meta information parsed") diff --git a/pkg/config/config.go b/pkg/config/config.go index aafc814..e23f541 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,25 +3,24 @@ package config import "time" type gocConfig struct { - Debug bool - CurWd string - Goflags []string - ImportPath string // import path of the project - CurPkgDir string - CurModProjectDir string - TmpModProjectDir string - TmpPkgDir string - ContainSpecialPattern bool // 参数中包含 ... - Pkgs map[string]*Package - GOPATH string - GOBIN string - IsMod bool // deprecated + Debug bool + Host string + Mode string // cover mode + GOPATH string + GOBIN string + CurWd string + TmpWd string + CurModProjectDir string + TmpModProjectDir string + + Goflags []string // command line flags + Packages []string // command line [packages] + + ImportPath string // the whole import path of the project + Pkgs map[string]*Package GlobalCoverVarImportPath string GlobalCoverVarImportPathDir string - - Host string - Mode string // cover mode } // GocConfig 全局变量,存放 goc 的各种元属性 diff --git a/pkg/flag/packages.go b/pkg/flag/packages.go index 491e404..72d8123 100644 --- a/pkg/flag/packages.go +++ b/pkg/flag/packages.go @@ -2,12 +2,12 @@ package flag import ( "fmt" - "log" "os" "path/filepath" "strings" "github.com/qiniu/goc/v2/pkg/config" + "github.com/qiniu/goc/v2/pkg/log" ) // GetPackagesDir parse [pacakges] part of args, it will fatal if error encountered @@ -19,6 +19,7 @@ import ( // 如果 [packages] 非法(即不符合 go 原生的定义),则返回对应错误 // 这里只考虑 go mod 的方式 func GetPackagesDir(patterns []string) { + packages := make([]string, 0) for _, p := range patterns { // patterns 只支持两种格式 // 1. 要么是直接指向某些 .go 文件的相对/绝对路径 @@ -29,33 +30,27 @@ func GetPackagesDir(patterns []string) { log.Fatalf("%v", err) } - // 获取绝对路径 - absp, err := filepath.Abs(p) - if err != nil { - log.Fatalf("%v", err) + // 获取相对于 current working directory 对路径 + for _, p := range patterns { + if filepath.IsAbs(p) { + relPath, err := filepath.Rel(config.GocConfig.CurWd, p) + if err != nil { + log.Fatalf("fail to get [packages] relative path from current working directory: %v", err) + } + packages = append(packages, relPath) + } else { + packages = append(packages, p) + } } + config.GocConfig.Packages = packages - // 获取当前 [packages] 所在的目录位置,供后续插桩使用。 - config.GocConfig.CurPkgDir = filepath.Dir(absp) - // 获取二进制名字 - // config.GocConfig.BinaryName = filepath.Base(absp) return } } } // 2. 要么是 import path - coverWd, err := getDirFromImportPaths(patterns) - if err != nil { - log.Fatalf("%v", err) - } - - config.GocConfig.CurPkgDir = coverWd - - // 是否包含 ... - if strings.Contains(patterns[0], "...") { - config.GocConfig.ContainSpecialPattern = true - } + config.GocConfig.Packages = patterns } // goFilesPackage 对一组 go 文件解析,判断是否合法 diff --git a/pkg/server/common.go b/pkg/server/common.go index b81a894..360ebd2 100644 --- a/pkg/server/common.go +++ b/pkg/server/common.go @@ -8,8 +8,8 @@ import ( ) const ( - PongWait = 20 * time.Second - PingWait = 10 * time.Second + PongWait = 10 * time.Second + PingWait = 5 * time.Second ) type ProfileReq string