Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions lang/golang/parser/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,36 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e
Mode: baseOpts,
Fset: fset,
Dir: dir,
Env: append(os.Environ(), "GOSUMDB=off"),
}

if p.opts.NeedTest {
cfg.Tests = true
}

pkgs, err := packages.Load(cfg, pkgPath)
if err != nil {
return fmt.Errorf("load path '%s' failed: %v", dir, err)
}

hasCGO := false
if len(p.cgoPkgs) > 0 {
hasCGO = true
}
fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s, hasCGO: %v\n", mod.Name, dir, pkgPath, hasCGO)

var pkgs []*packages.Package

if hasCGO {
baseOpts |= packages.NeedCompiledGoFiles
cfg.Mode = baseOpts
pkgs, err = packages.Load(cfg, pkgPath)
if err != nil {
return fmt.Errorf("load path '%s' with CGO failed: %v", dir, err)
}
} else {
pkgs, err = packages.Load(cfg, pkgPath)
if err != nil {
return fmt.Errorf("load path '%s' failed: %v", dir, err)
}
}

fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s, hasCGO: %v\n", mod.Name, dir, pkgPath, hasCGO)

for _, pkg := range pkgs {
if mm := p.repo.Modules[mod.Name]; mm != nil && (*mm).Packages[pkg.ID] != nil {
continue
Expand Down
32 changes: 23 additions & 9 deletions lang/golang/parser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"container/list"
"fmt"
"go/ast"
"go/build"
"go/types"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"

Expand Down Expand Up @@ -112,20 +112,27 @@ func (pc *PackageCache) IsStandardPackage(path string) bool {
return isStd
}

pkg, err := build.Import(path, "", build.FindOnly)
if err != nil {
// Cannot find the package, assume it's not a standard package
pc.set(path, false)
isStd := IsStandardLibrary(path)
pc.set(path, isStd)
return isStd
}

func IsStandardLibrary(pkgPath string) bool {

goroot := runtime.GOROOT()
if goroot == "" {
return false
}

isStd := pkg.Goroot
pc.set(path, isStd)
dir := filepath.Join(goroot, "src", pkgPath)
info, err := os.Stat(dir)
isStd := err == nil && info.IsDir()

return isStd
}

// stdlibCache 缓存 importPath 是否是 system package, 10000 个缓存
var stdlibCache = NewPackageCache(10000)
var stdlibCache = NewPackageCache(100000)

func isSysPkg(importPath string) bool {
return stdlibCache.IsStandardPackage(importPath)
Expand Down Expand Up @@ -311,14 +318,21 @@ func isUpperCase(c byte) bool {
return c >= 'A' && c <= 'Z'
}

var commitHashCache sync.Map

func getCommitHash(dir string) (string, error) {
if val, ok := commitHashCache.Load(dir); ok {
return val.(string), nil
}
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = dir
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get commit hash: %v", err)
}
return strings.TrimSpace(string(output)), nil
hash := strings.TrimSpace(string(output))
commitHashCache.Store(dir, hash)
return hash, nil
}

type workFile struct {
Expand Down
21 changes: 21 additions & 0 deletions lang/golang/parser/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go/parser"
"go/token"
"go/types"
"os"
"slices"
"sync"
"testing"
Expand Down Expand Up @@ -285,3 +286,23 @@ func Test_isSysPkg(t *testing.T) {
assert.False(t, foundOs, "os should have been evicted from the cache")
})
}

func Test_getCommitHash(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

// First call, should execute git command
hash1, err := getCommitHash(wd)
require.NoError(t, err)
require.NotEmpty(t, hash1)

// Second call, should come from cache
hash2, err := getCommitHash(wd)
require.NoError(t, err)
require.Equal(t, hash1, hash2)

// Check cache directly
cached, ok := commitHashCache.Load(wd)
require.True(t, ok)
require.Equal(t, hash1, cached)
}
Loading