Skip to content

Commit 84bfd10

Browse files
authored
Merge pull request github#13589 from owen-mc/go/tolerate-go-mod-not-in-project-root
Go: Deal better with a single go.mod file which is not in the project root
2 parents c8af28c + 5864bdc commit 84bfd10

File tree

105 files changed

+1275
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1275
-66
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 172 additions & 57 deletions
Large diffs are not rendered by default.

go/extractor/diagnostics/diagnostics.go

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,19 @@ func EmitPackageDifferentOSArchitecture(pkgPath string) {
129129
)
130130
}
131131

132+
func plural(n int, singular, plural string) string {
133+
if n == 1 {
134+
return singular
135+
} else {
136+
return plural
137+
}
138+
}
139+
132140
const maxNumPkgPaths = 5
133141

134142
func EmitCannotFindPackages(pkgPaths []string) {
135143
numPkgPaths := len(pkgPaths)
136144

137-
ending := "s"
138-
if numPkgPaths == 1 {
139-
ending = ""
140-
}
141-
142145
numPrinted := numPkgPaths
143146
truncated := false
144147
if numPrinted > maxNumPkgPaths {
@@ -154,7 +157,11 @@ func EmitCannotFindPackages(pkgPaths []string) {
154157
emitDiagnostic(
155158
"go/autobuilder/package-not-found",
156159
"Some packages could not be found",
157-
fmt.Sprintf("%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).", numPkgPaths, ending, secondLine),
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),
158165
severityWarning,
159166
fullVisibility,
160167
noLocation,
@@ -194,6 +201,123 @@ func EmitRelativeImportPaths() {
194201
)
195202
}
196203

204+
// The following diagnostics are telemetry-only.
205+
206+
func EmitBazelBuildFilesFound(bazelPaths []string) {
207+
emitDiagnostic(
208+
"go/autobuilder/bazel-build-file-found",
209+
"Bazel BUILD files were found",
210+
fmt.Sprintf(
211+
"%d bazel BUILD %s found:\n\n`%s`",
212+
len(bazelPaths),
213+
plural(len(bazelPaths), "file was", "files were"),
214+
strings.Join(bazelPaths, "`, `")),
215+
severityNote,
216+
telemetryOnly,
217+
noLocation,
218+
)
219+
}
220+
221+
func EmitGopkgTomlFound() {
222+
emitDiagnostic(
223+
"go/autobuilder/gopkg-toml-found",
224+
"A dep `Gopkg.toml` file was found",
225+
"A dep `Gopkg.toml` file was found",
226+
severityNote,
227+
telemetryOnly,
228+
noLocation,
229+
)
230+
}
231+
232+
func EmitGlideYamlFound() {
233+
emitDiagnostic(
234+
"go/autobuilder/glide-yaml-found",
235+
"A Glide `glide.yaml` file was found",
236+
"A Glide `glide.yaml` file was found",
237+
severityNote,
238+
telemetryOnly,
239+
noLocation,
240+
)
241+
}
242+
243+
func EmitGoWorkFound(goWorkPaths []string) {
244+
emitDiagnostic(
245+
"go/autobuilder/go-work-found",
246+
"`go.work` file found",
247+
fmt.Sprintf(
248+
"%d `go.work` %s found:\n\n`%s`",
249+
len(goWorkPaths),
250+
plural(len(goWorkPaths), "file was", "files were"),
251+
strings.Join(goWorkPaths, "`, `")),
252+
severityNote,
253+
telemetryOnly,
254+
noLocation,
255+
)
256+
}
257+
258+
func EmitGoFilesOutsideGoModules(goModPaths []string) {
259+
emitDiagnostic(
260+
"go/autobuilder/go-files-outside-go-modules",
261+
"Go files were found outside Go modules",
262+
"Go files were found outside of the Go modules corresponding to these `go.mod` files.\n\n`"+strings.Join(goModPaths, "`, `")+"`",
263+
severityNote,
264+
telemetryOnly,
265+
noLocation,
266+
)
267+
}
268+
269+
func EmitMultipleGoModFoundNested(goModPaths []string) {
270+
emitDiagnostic(
271+
"go/autobuilder/multiple-go-mod-found-nested",
272+
"Multiple `go.mod` files were found, all nested under one root `go.mod` file",
273+
fmt.Sprintf(
274+
"%d `go.mod` files were found:\n\n`%s`",
275+
len(goModPaths),
276+
strings.Join(goModPaths, "`, `")),
277+
severityNote,
278+
telemetryOnly,
279+
noLocation,
280+
)
281+
}
282+
283+
func EmitMultipleGoModFoundNotNested(goModPaths []string) {
284+
emitDiagnostic(
285+
"go/autobuilder/multiple-go-mod-found-not-nested",
286+
"Multiple `go.mod` files found, not all nested under one root `go.mod` file",
287+
fmt.Sprintf(
288+
"%d `go.mod` files were found:\n\n`%s`",
289+
len(goModPaths),
290+
strings.Join(goModPaths, "`, `")),
291+
severityNote,
292+
telemetryOnly,
293+
noLocation,
294+
)
295+
}
296+
297+
func EmitSingleRootGoModFound(goModPath string) {
298+
emitDiagnostic(
299+
"go/autobuilder/single-root-go-mod-found",
300+
"A single `go.mod` file was found in the root",
301+
"A single `go.mod` file was found.\n\n`"+goModPath+"`",
302+
severityNote,
303+
telemetryOnly,
304+
noLocation,
305+
)
306+
}
307+
308+
func EmitSingleNonRootGoModFound(goModPath string) {
309+
emitDiagnostic(
310+
"go/autobuilder/single-non-root-go-mod-found",
311+
"A single, non-root `go.mod` file was found",
312+
"A single, non-root `go.mod` file was found.\n\n`"+goModPath+"`",
313+
severityNote,
314+
telemetryOnly,
315+
noLocation,
316+
)
317+
}
318+
319+
// The following diagnostics are related to identifying the build environment.
320+
197321
func EmitNoGoModAndNoGoEnv(msg string) {
198322
emitDiagnostic(
199323
"go/autobuilder/env-no-go-mod-no-go-env",

go/extractor/util/util.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,46 @@ func FindGoFiles(root string) bool {
297297
})
298298
return found
299299
}
300+
301+
func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []string {
302+
paths := make([]string, 0, 1)
303+
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
304+
if err != nil {
305+
return err
306+
}
307+
if d.IsDir() {
308+
for _, dirToSkip := range dirsToSkip {
309+
if path == dirToSkip {
310+
return filepath.SkipDir
311+
}
312+
}
313+
}
314+
if d.Name() == name {
315+
paths = append(paths, path)
316+
}
317+
return nil
318+
})
319+
return paths
320+
}
321+
322+
func AnyGoFilesOutsideDirs(root string, dirsToSkip ...string) bool {
323+
found := false
324+
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
325+
if err != nil {
326+
return err
327+
}
328+
if d.IsDir() {
329+
for _, dirToSkip := range dirsToSkip {
330+
if path == dirToSkip {
331+
return filepath.SkipDir
332+
}
333+
}
334+
}
335+
if filepath.Ext(d.Name()) == ".go" {
336+
found = true
337+
return filepath.SkipAll
338+
}
339+
return nil
340+
})
341+
return found
342+
}

go/ql/integration-tests/all-platforms/go/bazel-sample-1/BUILD.bazel

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"markdownMessage": "1 bazel BUILD file was found:\n\n`BUILD.bazel`",
3+
"severity": "note",
4+
"source": {
5+
"extractorName": "go",
6+
"id": "go/autobuilder/bazel-build-file-found",
7+
"name": "Bazel BUILD files were found"
8+
},
9+
"visibility": {
10+
"cliSummaryTable": false,
11+
"statusPage": false,
12+
"telemetry": true
13+
}
14+
}
15+
{
16+
"markdownMessage": "A single `go.mod` file was found.\n\n`go.mod`",
17+
"severity": "note",
18+
"source": {
19+
"extractorName": "go",
20+
"id": "go/autobuilder/single-root-go-mod-found",
21+
"name": "A single `go.mod` file was found in the root"
22+
},
23+
"visibility": {
24+
"cliSummaryTable": false,
25+
"statusPage": false,
26+
"telemetry": true
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
go 1.14
2+
3+
require (
4+
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c
5+
)
6+
7+
module bazelsample
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c h1:zJ0mtu4jCalhKg6Oaukv6iIkb+cOvDrajDH9DH46Q4M=
3+
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
4+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
6+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
htmlFiles
2+
extractionErrors
3+
| Extraction failed in test.go with error \tother declaration of test | 2 |
4+
| Extraction failed in todel.go with error test redeclared in this block | 2 |
5+
#select
6+
| test.go:0:0:0:0 | test.go |
7+
| todel.go:0:0:0:0 | todel.go |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package bazelsample
2+
3+
import (
4+
"golang.org/x/net/ipv4"
5+
)
6+
7+
func test() {
8+
9+
header := ipv4.Header{}
10+
header.Version = 4
11+
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
3+
from create_database_utils import *
4+
from diagnostics_test_utils import *
5+
6+
run_codeql_database_create([], lang="go")
7+
8+
check_diagnostics()

0 commit comments

Comments
 (0)