@@ -419,10 +419,11 @@ func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile st
419419
420420 // Use 'go list' command instead of depth.Resolve()
421421 if parser .ParseDependency > 0 {
422+ allDir := append ([]string {filepath .Dir (absMainAPIFilePath )}, searchDirs ... )
422423 if parser .parseGoList {
423- pkgs , err := listPackages (context .Background (), filepath . Dir ( absMainAPIFilePath ) , nil , "-deps" )
424+ pkgs , err := listPackages (context .Background (), allDir , nil , "-deps" )
424425 if err != nil {
425- return fmt . Errorf ( "pkg %s cannot find all dependencies, %s" , filepath . Dir ( absMainAPIFilePath ), err )
426+ return err
426427 }
427428
428429 length := len (pkgs )
@@ -433,23 +434,35 @@ func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile st
433434 }
434435 }
435436 } else {
436- var t depth.Tree
437- t .ResolveInternal = true
438- t .MaxDepth = parseDepth
439-
440- pkgName , err := getPkgName (filepath .Dir (absMainAPIFilePath ))
441- if err != nil {
442- return err
437+ dirImported := make (map [string ]struct {}) // for deduplication
438+ for _ , dir := range allDir { // ignore search dir (have been parsed)
439+ absDir , err := filepath .Abs (dir )
440+ if err == nil {
441+ dirImported [absDir ] = struct {}{}
442+ }
443443 }
444+ for index , dir := range allDir {
445+ var t depth.Tree
446+ t .ResolveInternal = true
447+ t .MaxDepth = parseDepth
444448
445- err = t .Resolve (pkgName )
446- if err != nil {
447- return fmt .Errorf ("pkg %s cannot find all dependencies, %s" , pkgName , err )
448- }
449- for i := 0 ; i < len (t .Root .Deps ); i ++ {
450- err := parser .getAllGoFileInfoFromDeps (& t .Root .Deps [i ], parser .ParseDependency )
449+ pkgName , err := getPkgName (dir )
451450 if err != nil {
452- return err
451+ if index == 0 { // ignore error when load search dir
452+ return err
453+ }
454+ continue
455+ }
456+
457+ err = t .Resolve (pkgName )
458+ if err != nil {
459+ return fmt .Errorf ("pkg %s cannot find all dependencies, %s" , pkgName , err )
460+ }
461+ for i := 0 ; i < len (t .Root .Deps ); i ++ {
462+ err := parser .getAllGoFileInfoFromDeps (& t .Root .Deps [i ], parser .ParseDependency , dirImported )
463+ if err != nil {
464+ return err
465+ }
453466 }
454467 }
455468 }
@@ -1867,7 +1880,7 @@ func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error {
18671880 })
18681881}
18691882
1870- func (parser * Parser ) getAllGoFileInfoFromDeps (pkg * depth.Pkg , parseFlag ParseFlag ) error {
1883+ func (parser * Parser ) getAllGoFileInfoFromDeps (pkg * depth.Pkg , parseFlag ParseFlag , dirImported map [ string ] struct {} ) error {
18711884 ignoreInternal := pkg .Internal && ! parser .ParseInternal
18721885 if ignoreInternal || ! pkg .Resolved { // ignored internal and not resolved dependencies
18731886 return nil
@@ -1883,6 +1896,10 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg, parseFlag ParseFl
18831896 }
18841897
18851898 srcDir := pkg .Raw .Dir
1899+ if _ , ok := dirImported [srcDir ]; ok {
1900+ return nil
1901+ }
1902+ dirImported [srcDir ] = struct {}{}
18861903
18871904 files , err := os .ReadDir (srcDir ) // only parsing files in the dir(don't contain sub dir files)
18881905 if err != nil {
@@ -1901,7 +1918,7 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg, parseFlag ParseFl
19011918 }
19021919
19031920 for i := 0 ; i < len (pkg .Deps ); i ++ {
1904- if err := parser .getAllGoFileInfoFromDeps (& pkg .Deps [i ], parseFlag ); err != nil {
1921+ if err := parser .getAllGoFileInfoFromDeps (& pkg .Deps [i ], parseFlag , dirImported ); err != nil {
19051922 return err
19061923 }
19071924 }
0 commit comments