Skip to content

Commit

Permalink
Merge pull request #195 from kylecarbs/master
Browse files Browse the repository at this point in the history
Reduce build.Import calls when importing files from relative root
  • Loading branch information
shogo82148 authored May 25, 2021
2 parents 4edeb4b + bd04b32 commit 021e47b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
/goveralls
/vendor
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/mattn/goveralls

go 1.11

require golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375
require (
golang.org/x/mod v0.2.0
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375
)
25 changes: 23 additions & 2 deletions gocover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/cover"
)

func findFile(file string) (string, error) {
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
}
}

dir, file := filepath.Split(file)
pkg, err := build.Import(dir, ".", build.FindOnly)
if err != nil {
Expand Down Expand Up @@ -102,9 +113,19 @@ func mergeTwoProfBlock(left, right []cover.ProfileBlock) []cover.ProfileBlock {

// toSF converts profiles to sourcefiles for coveralls.
func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
rootDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("get working dir: %w", err)
}
modPath := filepath.Join(rootDirectory, "go.mod")
rootPackage := ""
if content, err := ioutil.ReadFile(modPath); err == nil {
rootPackage = modfile.ModulePath(content)
}

var rv []*SourceFile
for _, prof := range profs {
path, err := findFile(prof.FileName)
path, err := findFile(rootPackage, rootDirectory, prof.FileName)
if err != nil {
return nil, fmt.Errorf("cannot find file %q: %v", prof.FileName, err)
}
Expand Down

0 comments on commit 021e47b

Please sign in to comment.