Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion gocover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"golang.org/x/mod/modfile"
Expand All @@ -24,6 +25,7 @@ func findFile(rootPackage string, rootDir string, file string) (string, error) {
// If we find a file that is inside the root package, we already know
// where it should be!
if rootPackage != "" {

if relPath, _ := filepath.Rel(rootPackage, file); !strings.HasPrefix(relPath, "..") {
// The file is inside the root package...
return filepath.Join(rootDir, relPath), nil
Expand Down Expand Up @@ -149,7 +151,21 @@ func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
if *uploadSource {
fb, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read source of file %q: %v", path, err)
// Golang module versioning can either be done
// via a subdirectory or updated via a branch
// commit. For more infomation see:
// https://go.dev/wiki/Modules#releasing-modules-v2-or-higher
basePath := regexp.MustCompile(`/v\d+/`).
ReplaceAllString(path, "/")

if basePath != path {
fb, err = ioutil.ReadFile(basePath)
}
if err != nil {
return nil, fmt.Errorf("cannot read "+
"source of file %q: %v", path,
err)
}
}
sf.Source = string(fb)
}
Expand Down