diff --git a/tools/please_go/ChangeLog b/tools/please_go/ChangeLog index 389c23c..ef6a4ea 100644 --- a/tools/please_go/ChangeLog +++ b/tools/please_go/ChangeLog @@ -1,3 +1,7 @@ +Version 1.26.1 +-------------- + * Make `please_go cover` handle the possiblity that every src is outside the package directory (#385) + Version 1.26.0 -------------- * `generate`: rename `third_part_folder` option to `deps_path`, and remove `t` short option (#379) diff --git a/tools/please_go/VERSION b/tools/please_go/VERSION index 5ff8c4f..dd43a14 100644 --- a/tools/please_go/VERSION +++ b/tools/please_go/VERSION @@ -1 +1 @@ -1.26.0 +1.26.1 diff --git a/tools/please_go/cover/cover.go b/tools/please_go/cover/cover.go index 282f930..cf721c4 100644 --- a/tools/please_go/cover/cover.go +++ b/tools/please_go/cover/cover.go @@ -5,6 +5,7 @@ package cover import ( "bytes" "encoding/json" + "fmt" "go/parser" "go/token" "log" @@ -23,6 +24,14 @@ func WriteCoverage(goTool, coverTool, covercfg, output, pkgConfigFile, pkg strin return err } + // It is entirely possible that all of the sources to a build target are outside the package + // directory (which is usually the result of using the outputs of other targets as srcs). + // `go tool cover` still wants to write the `_covervars.cover.go` file to the package directory, + // so first ensure that it exists. + if err := os.MkdirAll(pkg, 0755); err != nil { + return fmt.Errorf("ensure package directory exists: %w", err) + } + b, _ := json.Marshal(coverConfig{ OutConfig: covercfg, PkgPath: pkg, @@ -38,7 +47,7 @@ func WriteCoverage(goTool, coverTool, covercfg, output, pkgConfigFile, pkg strin // 1.27 needs package-relative paths (https://go.dev/issue/70478) needRelativePaths := needs127RelativePaths(goTool) if coverTool != "" || needRelativePaths || need121CoverVars { - buf.WriteString(filepath.Join(filepath.Dir(srcs[0]), "_covervars.cover.go\n")) + buf.WriteString(filepath.Join(pkg, "_covervars.cover.go\n")) } for _, src := range srcs { buf.WriteString(strings.TrimSuffix(src, ".go") + ".cover.go\n")