Skip to content

Commit bb44a2f

Browse files
committed
Populate pkgInfoMapping for test packages if relevant
1 parent bcb84a8 commit bb44a2f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

go/extractor/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
131131
if os.Getenv("CODEQL_EXTRACTOR_GO_FAST_PACKAGE_INFO") != "false" {
132132
log.Printf("Running go list to resolve package and module directories.")
133133
// get all packages information
134-
pkgInfos, err = toolchain.GetPkgsInfo(patterns, true, modFlags...)
134+
pkgInfos, err = toolchain.GetPkgsInfo(patterns, true, extractTests, modFlags...)
135135
if err != nil {
136136
log.Fatalf("Error getting dependency package or module directories: %v.", err)
137137
}

go/extractor/toolchain/toolchain.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,18 @@ type PkgInfo struct {
223223
// GetPkgsInfo gets the absolute module and package root directories for the packages matched by the
224224
// patterns `patterns`. It passes to `go list` the flags specified by `flags`. If `includingDeps`
225225
// is true, all dependencies will also be included.
226-
func GetPkgsInfo(patterns []string, includingDeps bool, flags ...string) (map[string]PkgInfo, error) {
226+
func GetPkgsInfo(patterns []string, includingDeps bool, extractTests bool, flags ...string) (map[string]PkgInfo, error) {
227227
// enable module mode so that we can find a module root if it exists, even if go module support is
228228
// disabled by a build
229229
if includingDeps {
230230
// the flag `-deps` causes all dependencies to be retrieved
231231
flags = append(flags, "-deps")
232232
}
233233

234+
if extractTests {
235+
flags = append(flags, "-test")
236+
}
237+
234238
// using -json overrides -f format
235239
output, err := RunList("", patterns, append(flags, "-json")...)
236240
if err != nil {
@@ -272,6 +276,12 @@ func GetPkgsInfo(patterns []string, includingDeps bool, flags ...string) (map[st
272276
PkgDir: pkgAbsDir,
273277
ModDir: modAbsDir,
274278
}
279+
280+
if extractTests && strings.Contains(pkgInfo.ImportPath, " [") {
281+
// Assume " [" is the start of a qualifier, and index the package by its base name
282+
baseImportPath := strings.Split(pkgInfo.ImportPath, " [")[0]
283+
pkgInfoMapping[baseImportPath] = pkgInfoMapping[pkgInfo.ImportPath]
284+
}
275285
}
276286
return pkgInfoMapping, nil
277287
}

0 commit comments

Comments
 (0)