@@ -103,10 +103,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
103
103
extractUniverseScope ()
104
104
log .Println ("Done extracting universe scope." )
105
105
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 )
110
108
// root directories of packages that we want to extract
111
109
wantedRoots := make (map [string ]bool )
112
110
@@ -116,16 +114,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
116
114
}, func (pkg * packages.Package ) {
117
115
log .Printf ("Processing package %s." , pkg .PkgPath )
118
116
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 ... )
129
119
}
130
120
131
121
log .Printf ("Extracting types for package %s." , pkg .PkgPath )
@@ -152,11 +142,14 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
152
142
})
153
143
154
144
for _ , pkg := range pkgs {
155
- if pkgRoots [pkg .PkgPath ] == "" {
145
+ pkgInfo , ok := pkgInfos [pkg .PkgPath ]
146
+ if ! ok || pkgInfo .PkgDir == "" {
156
147
log .Fatalf ("Unable to get a source directory for input package %s." , pkg .PkgPath )
157
148
}
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
+ }
160
153
}
161
154
162
155
log .Println ("Done processing dependencies." )
@@ -174,16 +167,17 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
174
167
return true
175
168
}, func (pkg * packages.Package ) {
176
169
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 )
178
172
if err != nil || noExtractRe .MatchString (relDir ) {
179
173
// if the path can't be made relative or matches the noExtract regexp skip it
180
174
continue
181
175
}
182
176
183
177
extraction .extractPackage (pkg )
184
178
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" )
187
181
if util .FileExists (modPath ) {
188
182
log .Printf ("Extracting %s" , modPath )
189
183
start := time .Now ()
0 commit comments