@@ -18,13 +18,13 @@ import (
1818 "container/list"
1919 "fmt"
2020 "go/ast"
21- "go/build"
2221 "go/types"
2322 "os"
2423 "os/exec"
2524 "path"
2625 "path/filepath"
2726 "regexp"
27+ "runtime"
2828 "strings"
2929 "sync"
3030
@@ -112,20 +112,27 @@ func (pc *PackageCache) IsStandardPackage(path string) bool {
112112 return isStd
113113 }
114114
115- pkg , err := build .Import (path , "" , build .FindOnly )
116- if err != nil {
117- // Cannot find the package, assume it's not a standard package
118- pc .set (path , false )
115+ isStd := IsStandardLibrary (path )
116+ pc .set (path , isStd )
117+ return isStd
118+ }
119+
120+ func IsStandardLibrary (pkgPath string ) bool {
121+
122+ goroot := runtime .GOROOT ()
123+ if goroot == "" {
119124 return false
120125 }
121126
122- isStd := pkg .Goroot
123- pc .set (path , isStd )
127+ dir := filepath .Join (goroot , "src" , pkgPath )
128+ info , err := os .Stat (dir )
129+ isStd := err == nil && info .IsDir ()
130+
124131 return isStd
125132}
126133
127134// stdlibCache 缓存 importPath 是否是 system package, 10000 个缓存
128- var stdlibCache = NewPackageCache (10000 )
135+ var stdlibCache = NewPackageCache (100000 )
129136
130137func isSysPkg (importPath string ) bool {
131138 return stdlibCache .IsStandardPackage (importPath )
@@ -311,14 +318,21 @@ func isUpperCase(c byte) bool {
311318 return c >= 'A' && c <= 'Z'
312319}
313320
321+ var commitHashCache sync.Map
322+
314323func getCommitHash (dir string ) (string , error ) {
324+ if val , ok := commitHashCache .Load (dir ); ok {
325+ return val .(string ), nil
326+ }
315327 cmd := exec .Command ("git" , "rev-parse" , "HEAD" )
316328 cmd .Dir = dir
317329 output , err := cmd .Output ()
318330 if err != nil {
319331 return "" , fmt .Errorf ("failed to get commit hash: %v" , err )
320332 }
321- return strings .TrimSpace (string (output )), nil
333+ hash := strings .TrimSpace (string (output ))
334+ commitHashCache .Store (dir , hash )
335+ return hash , nil
322336}
323337
324338type workFile struct {
0 commit comments