@@ -159,13 +159,18 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
159159
160160 // Do a post-order traversal and extract the package scope of each package
161161 packages .Visit (pkgs , nil , func (pkg * packages.Package ) {
162- log .Printf ("Processing package %s." , pkg .PkgPath )
162+ // Note that if test extraction is enabled, we will encounter a package twice here:
163+ // once as the main package, and once as the test package (with a package ID like
164+ // "abc.com/pkgname [abc.com/pkgname.test]").
165+ //
166+ // We will extract it both times however, because we need to visit the packages
167+ // in the right order in order to visit used types before their users, and the
168+ // ordering determined by packages.Visit for the main and the test package may differ.
169+ //
170+ // This should only cause some wasted time and not inconsistency because the names for
171+ // objects seen in this process should be the same each time.
163172
164- // If this is a variant of a package that also occurs with a shorter ID, skip it.
165- if pkg .ID != longestPackageIds [pkg .PkgPath ] {
166- log .Printf ("Skipping variant of package %s with ID %s." , pkg .PkgPath , pkg .ID )
167- return
168- }
173+ log .Printf ("Processing package %s." , pkg .PkgPath )
169174
170175 if _ , ok := pkgInfos [pkg .PkgPath ]; ! ok {
171176 pkgInfos [pkg .PkgPath ] = toolchain .GetPkgInfo (pkg .PkgPath , modFlags ... )
@@ -242,9 +247,15 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
242247 // extract AST information for all packages
243248 packages .Visit (pkgs , nil , func (pkg * packages.Package ) {
244249
245- // If this is a variant of a package that also occurs with a longer ID, skip it.
250+ // If this is a variant of a package that also occurs with a longer ID, skip it;
251+ // otherwise we would extract the same file more than once including extracting the
252+ // body of methods twice, causing database inconsistencies.
253+ //
254+ // We prefer the version with the longest ID because that is (so far as I know) always
255+ // the version that defines more entities -- the only case I'm aware of being a test
256+ // variant of a package, which includes test-only functions in addition to the complete
257+ // contents of the main variant.
246258 if pkg .ID != longestPackageIds [pkg .PkgPath ] {
247- // Don't log here; we already mentioned this above.
248259 return
249260 }
250261
0 commit comments