@@ -103,10 +103,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
103103 extractUniverseScope ()
104104 log .Println ("Done extracting universe scope." )
105105
106- // a map of package path to package root directory (currently the module root or the source directory)
107- pkgRoots := make (map [string ]string )
108- // a map of package path to source code directory
109- pkgDirs := make (map [string ]string )
106+ // a map of package path to source directory and module root directory
107+ pkgInfos := make (map [string ]util.PkgInfo )
110108 // root directories of packages that we want to extract
111109 wantedRoots := make (map [string ]bool )
112110
@@ -116,16 +114,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
116114 }, func (pkg * packages.Package ) {
117115 log .Printf ("Processing package %s." , pkg .PkgPath )
118116
119- if _ , ok := pkgRoots [pkg .PkgPath ]; ! ok {
120- mdir := util .GetModDir (pkg .PkgPath , modFlags ... )
121- pdir := util .GetPkgDir (pkg .PkgPath , modFlags ... )
122- // GetModDir returns the empty string if the module directory cannot be determined, e.g. if the package
123- // is not using modules. If this is the case, fall back to the package directory
124- if mdir == "" {
125- mdir = pdir
126- }
127- pkgRoots [pkg .PkgPath ] = mdir
128- pkgDirs [pkg .PkgPath ] = pdir
117+ if _ , ok := pkgInfos [pkg .PkgPath ]; ! ok {
118+ pkgInfos [pkg .PkgPath ] = util .GetPkgInfo (pkg .PkgPath , modFlags ... )
129119 }
130120
131121 log .Printf ("Extracting types for package %s." , pkg .PkgPath )
@@ -152,11 +142,14 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
152142 })
153143
154144 for _ , pkg := range pkgs {
155- if pkgRoots [pkg .PkgPath ] == "" {
145+ pkgInfo , ok := pkgInfos [pkg .PkgPath ]
146+ if ! ok || pkgInfo .PkgDir == "" {
156147 log .Fatalf ("Unable to get a source directory for input package %s." , pkg .PkgPath )
157148 }
158- wantedRoots [pkgRoots [pkg.PkgPath ]] = true
159- wantedRoots [pkgDirs [pkg.PkgPath ]] = true
149+ wantedRoots [pkgInfo .PkgDir ] = true
150+ if pkgInfo .ModDir != "" {
151+ wantedRoots [pkgInfo .ModDir ] = true
152+ }
160153 }
161154
162155 log .Println ("Done processing dependencies." )
@@ -174,16 +167,17 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
174167 return true
175168 }, func (pkg * packages.Package ) {
176169 for root , _ := range wantedRoots {
177- relDir , err := filepath .Rel (root , pkgDirs [pkg .PkgPath ])
170+ pkgInfo := pkgInfos [pkg .PkgPath ]
171+ relDir , err := filepath .Rel (root , pkgInfo .PkgDir )
178172 if err != nil || noExtractRe .MatchString (relDir ) {
179173 // if the path can't be made relative or matches the noExtract regexp skip it
180174 continue
181175 }
182176
183177 extraction .extractPackage (pkg )
184178
185- if pkgRoots [ pkg . PkgPath ] != "" {
186- modPath := filepath .Join (pkgRoots [ pkg . PkgPath ] , "go.mod" )
179+ if pkgInfo . ModDir != "" {
180+ modPath := filepath .Join (pkgInfo . ModDir , "go.mod" )
187181 if util .FileExists (modPath ) {
188182 log .Printf ("Extracting %s" , modPath )
189183 start := time .Now ()
0 commit comments