Skip to content

Commit 05a4fdf

Browse files
committed
Put all package-not-found errors into one diagnostic
1 parent a4c9120 commit 05a4fdf

File tree

5 files changed

+371
-12
lines changed

5 files changed

+371
-12
lines changed

go/extractor/diagnostics/diagnostics.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"strings"
89
"time"
910
)
1011

@@ -124,10 +125,32 @@ func EmitPackageDifferentOSArchitecture(pkgPath string) {
124125
)
125126
}
126127

127-
func EmitCannotFindPackage(pkgPath string) {
128-
emitDiagnostic("go/autobuilder/package-not-found",
129-
"Package "+pkgPath+" could not be found",
130-
"Check that the path is correct. If it is a private package, make sure it can be accessed. If it is contained in the repository then you may need a [custom build command](https://docs.github.com/en/github-ae@latest/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-language).",
128+
const maxNumPkgPaths = 5
129+
130+
func EmitCannotFindPackages(pkgPaths []string) {
131+
numPkgPaths := len(pkgPaths)
132+
133+
ending := "s"
134+
if numPkgPaths == 1 {
135+
ending = ""
136+
}
137+
138+
numPrinted := numPkgPaths
139+
truncated := false
140+
if numPrinted > maxNumPkgPaths {
141+
numPrinted = maxNumPkgPaths
142+
truncated = true
143+
}
144+
145+
secondLine := "`" + strings.Join(pkgPaths[0:numPrinted], "`, `") + "`"
146+
if truncated {
147+
secondLine += fmt.Sprintf(" and %d more", numPkgPaths-maxNumPkgPaths)
148+
}
149+
150+
emitDiagnostic(
151+
"go/autobuilder/package-not-found",
152+
fmt.Sprintf("%d package%s could not be found", numPkgPaths, ending),
153+
"The following packages could not be found. Check that the paths are correct and make sure any private packages can be accessed. If any of the packages are present in the repository then you may need a [custom build command](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages).\n\n"+secondLine,
131154
severityError, false,
132155
true, true, true,
133156
"", 0, 0, 0, 0,

go/extractor/extractor.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
128128
log.Printf("Done running go list deps: resolved %d packages.", len(pkgInfos))
129129
}
130130

131+
pkgsNotFound := make([]string, 0, len(pkgs))
132+
131133
// Do a post-order traversal and extract the package scope of each package
132134
packages.Visit(pkgs, func(pkg *packages.Package) bool {
133135
return true
@@ -162,14 +164,19 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
162164
diagnostics.EmitPackageDifferentOSArchitecture(pkg.PkgPath)
163165
} else if strings.Contains(errString, "cannot find package") ||
164166
strings.Contains(errString, "no required module provides package") {
165-
diagnostics.EmitCannotFindPackage(pkg.PkgPath)
167+
pkgsNotFound = append(pkgsNotFound, pkg.PkgPath)
166168
}
167169
extraction.extractError(tw, err, lbl, i)
168170
}
169171
}
172+
170173
log.Printf("Done extracting types for package %s.", pkg.PkgPath)
171174
})
172175

176+
if len(pkgsNotFound) > 0 {
177+
diagnostics.EmitCannotFindPackages(pkgsNotFound)
178+
}
179+
173180
for _, pkg := range pkgs {
174181
pkgInfo, ok := pkgInfos[pkg.PkgPath]
175182
if !ok || pkgInfo.PkgDir == "" {

go/ql/integration-tests/all-platforms/go/diagnostics/package-not-found-with-go-mod/diagnostics.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"internal": false,
3-
"markdownMessage": "Check that the path is correct. If it is a private package, make sure it can be accessed. If it is contained in the repository then you may need a [custom build command](https://docs.github.com/en/github-ae@latest/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-language).",
3+
"markdownMessage": "The following packages could not be found. Check that the paths are correct and make sure any private packages can be accessed. If any of the packages are present in the repository then you may need a [custom build command](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages).\n\n`github.com/nosuchorg/nosuchrepo000`, `github.com/nosuchorg/nosuchrepo001`, `github.com/nosuchorg/nosuchrepo002`, `github.com/nosuchorg/nosuchrepo003`, `github.com/nosuchorg/nosuchrepo004` and 105 more",
44
"severity": "error",
55
"source": {
66
"extractorName": "go",
77
"id": "go/autobuilder/package-not-found",
8-
"name": "Package github.com/linode/linode-docs-theme could not be found"
8+
"name": "110 packages could not be found"
99
},
1010
"visibility": {
1111
"cliSummaryTable": true,

0 commit comments

Comments
 (0)