Skip to content

Commit f837d90

Browse files
committed
Improve go/autobuilder/package-not-found diagnostic message
1 parent fdc5ae3 commit f837d90

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

go/extractor/diagnostics/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/diagnostics/diagnostics.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"strings"
99
"time"
10+
11+
"github.com/github/codeql-go/extractor/util"
1012
)
1113

1214
type sourceStruct struct {
@@ -154,14 +156,42 @@ func EmitCannotFindPackages(pkgPaths []string) {
154156
secondLine += fmt.Sprintf(" and %d more", numPkgPaths-maxNumPkgPaths)
155157
}
156158

159+
message := fmt.Sprintf(
160+
"%d package%s could not be found:\n\n%s.\n\n"+
161+
"CodeQL is able to analyze your code without those packages, but definitions from them may not be recognized and "+
162+
"source files that use them may only be partially analyzed.\n\n"+
163+
"To ensure that you have comprehensive alert coverage, check that the paths are correct and make sure any private packages can be accessed by CodeQL. ",
164+
numPkgPaths,
165+
plural(len(pkgPaths), "", "s"),
166+
secondLine,
167+
)
168+
169+
// Depending on the environment we are running in, provide a different message for how to configure access to private registries.
170+
if util.IsDynamicActionsWorkflow() {
171+
// For GitHub-managed (dynamic) workflows, we offer built-in support for private registries that customers can set up.
172+
message = message +
173+
"Organizations [can grant access to private registries for GitHub security products](https://docs.github.com/en/code-security/how-tos/secure-at-scale/configure-organization-security/manage-usage-and-access/giving-org-access-private-registries). "
174+
} else {
175+
if util.IsActionsWorkflow() {
176+
// For custom workflows, users can add a workflow step to set up credentials or environment variables.
177+
message = message +
178+
"To set up access to a private registry, add a step to your workflow which sets up the necessary credentials and environment variables. "
179+
} else {
180+
// Otherwise, we are running locally or in some other CI system.
181+
message = message +
182+
"To set up access to private registries, ensure that the necessary credentials and environment variables are set up for `go` to use. "
183+
}
184+
185+
// This should be less likely since we improved Go project discovery. We only include it in the message if we are not running in a
186+
// GitHub-managed workflow, since users would not be able to act on this there.
187+
message = message +
188+
"If any of the packages are already present in the repository, but were not found, then you may need a [custom build command](https://docs.github.com/en/code-security/how-tos/scan-code-for-vulnerabilities/manage-your-configuration/codeql-code-scanning-for-compiled-languages)."
189+
}
190+
157191
emitDiagnostic(
158192
"go/autobuilder/package-not-found",
159193
"Some packages could not be found",
160-
fmt.Sprintf(
161-
"%d package%s could not be found:\n\n%s.\n\nDefinitions in those packages may not be recognized by CodeQL, and files that use them may only be partially analyzed.\n\nCheck 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).",
162-
numPkgPaths,
163-
plural(len(pkgPaths), "", "s"),
164-
secondLine),
194+
message,
165195
severityWarning,
166196
fullVisibility,
167197
noLocation,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"markdownMessage": "110 packages could not be found:\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.\n\nDefinitions in those packages may not be recognized by CodeQL, and files that use them may only be partially analyzed.\n\nCheck 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).",
2+
"markdownMessage": "110 packages could not be found:\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.\n\nCodeQL is able to analyze your code without those packages, but definitions from them may not be recognized and source files that use them may only be partially analyzed.\n\nTo ensure that you have comprehensive alert coverage, check that the paths are correct and make sure any private packages can be accessed by CodeQL. To set up access to a private registry, add a step to your workflow which sets up the necessary credentials and environment variables. If any of the packages are already present in the repository, but were not found, then you may need a [custom build command](https://docs.github.com/en/code-security/how-tos/scan-code-for-vulnerabilities/manage-your-configuration/codeql-code-scanning-for-compiled-languages).",
33
"severity": "warning",
44
"source": {
55
"extractorName": "go",

go/ql/integration-tests/diagnostics/package-not-found-with-go-mod/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
def test(codeql, go, check_build_environment):
55
check_build_environment.source_root = "work"
66
os.environ["LGTM_INDEX_IMPORT_PATH"] = "test"
7+
8+
# The diagnostic message depends on the environment we are running in. To ensure consistent
9+
# output, we set `GITHUB_ACTIONS` to `true` if we are not actually running in a workflow.
10+
if (os.environ.get("GITHUB_ACTIONS", "") != "true"):
11+
os.environ["GITHUB_ACTIONS"] = "true"
12+
713
codeql.database.create(source_root="work")

go/ql/integration-tests/diagnostics/package-not-found-without-go-mod/diagnostics.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"markdownMessage": "1 package could not be found:\n\n`github.com/linode/linode-docs-theme`.\n\nDefinitions in those packages may not be recognized by CodeQL, and files that use them may only be partially analyzed.\n\nCheck 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).",
2+
"markdownMessage": "1 package could not be found:\n\n`github.com/linode/linode-docs-theme`.\n\nCodeQL is able to analyze your code without those packages, but definitions from them may not be recognized and source files that use them may only be partially analyzed.\n\nTo ensure that you have comprehensive alert coverage, check that the paths are correct and make sure any private packages can be accessed by CodeQL. To set up access to a private registry, add a step to your workflow which sets up the necessary credentials and environment variables. If any of the packages are already present in the repository, but were not found, then you may need a [custom build command](https://docs.github.com/en/code-security/how-tos/scan-code-for-vulnerabilities/manage-your-configuration/codeql-code-scanning-for-compiled-languages).",
33
"severity": "warning",
44
"source": {
55
"extractorName": "go",

go/ql/integration-tests/diagnostics/package-not-found-without-go-mod/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
def test(codeql, go, check_build_environment):
55
check_build_environment.source_root = "work"
66
os.environ["LGTM_INDEX_IMPORT_PATH"] = "test"
7+
8+
# The diagnostic message depends on the environment we are running in. To ensure consistent
9+
# output, we set `GITHUB_ACTIONS` to `true` if we are not actually running in a workflow.
10+
if (os.environ.get("GITHUB_ACTIONS", "") != "true"):
11+
os.environ["GITHUB_ACTIONS"] = "true"
12+
713
codeql.database.create(source_root="work")

0 commit comments

Comments
 (0)