Skip to content

Commit 11f82c3

Browse files
committed
Fix Go extractor incorrectly excluding packages with .. in relative paths
The Go extractor was incorrectly excluding valid packages when their relative path from a wantedRoot contained '..' (parent directory references). This occurred because the extractor checked each package against all wantedRoots, and most sibling package directories resulted in relative paths like '../../package/path' which matched the exclusion regex. This fix introduces a priority root system that checks the module root and package directory first before falling back to other roots. This ensures packages are evaluated against the most relevant directories, avoiding false positives from the '..' exclusion pattern. Fixes packages being skipped during extraction when located in subdirectories of a Go module, particularly affecting projects with vendor directories and multiple nested package structures.
1 parent 5d2ddbf commit 11f82c3

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

go/extractor/extractor.go

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,33 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
273273
return
274274
}
275275

276-
for root := range wantedRoots {
277-
pkgInfo := pkgInfos[pkg.PkgPath]
276+
pkgInfo := pkgInfos[pkg.PkgPath]
277+
matched := false
278+
279+
// Prioritize checking against module root and package directory.
280+
// This avoids incorrectly excluding packages due to relative paths containing ".."
281+
// when checking against unrelated sibling package directories.
282+
priorityRoots := []string{}
283+
if pkgInfo.ModDir != "" {
284+
priorityRoots = append(priorityRoots, pkgInfo.ModDir)
285+
}
286+
priorityRoots = append(priorityRoots, pkgInfo.PkgDir)
287+
288+
for _, root := range priorityRoots {
289+
if !wantedRoots[root] {
290+
continue
291+
}
292+
278293
relDir, err := filepath.Rel(root, pkgInfo.PkgDir)
279-
if err != nil || noExtractRe.MatchString(relDir) {
280-
// if the path can't be made relative or matches the noExtract regexp skip it
294+
if err != nil {
295+
continue
296+
}
297+
298+
if relDir != "." && noExtractRe.MatchString(relDir) {
281299
continue
282300
}
283301

302+
matched = true
284303
extraction.extractPackage(pkg)
285304

286305
modDir := pkgInfo.ModDir
@@ -306,7 +325,56 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
306325
return
307326
}
308327

309-
log.Printf("Skipping dependency package %s.", pkg.PkgPath)
328+
// If priority roots didn't match, fall back to checking all other roots
329+
if !matched {
330+
for root := range wantedRoots {
331+
// Skip priority roots we already checked
332+
isPriorityRoot := false
333+
for _, pr := range priorityRoots {
334+
if root == pr {
335+
isPriorityRoot = true
336+
break
337+
}
338+
}
339+
if isPriorityRoot {
340+
continue
341+
}
342+
343+
relDir, err := filepath.Rel(root, pkgInfo.PkgDir)
344+
if err != nil || noExtractRe.MatchString(relDir) {
345+
continue
346+
}
347+
348+
matched = true
349+
extraction.extractPackage(pkg)
350+
351+
modDir := pkgInfo.ModDir
352+
if modDir == "" {
353+
modDir = pkgInfo.PkgDir
354+
}
355+
if modDir != "" {
356+
modPath := filepath.Join(modDir, "go.mod")
357+
if util.FileExists(modPath) {
358+
log.Printf("Extracting %s", modPath)
359+
start := time.Now()
360+
361+
err := extraction.extractGoMod(modPath)
362+
if err != nil {
363+
log.Printf("Failed to extract go.mod: %s", err.Error())
364+
}
365+
366+
end := time.Since(start)
367+
log.Printf("Done extracting %s (%dms)", modPath, end.Nanoseconds()/1000000)
368+
}
369+
}
370+
371+
return
372+
}
373+
}
374+
375+
if !matched {
376+
log.Printf("Skipping dependency package %s.", pkg.PkgPath)
377+
}
310378
})
311379

312380
extraction.WaitGroup.Wait()

0 commit comments

Comments
 (0)