-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdiagnostics.go
539 lines (481 loc) · 15.9 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package diagnostics
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
)
type sourceStruct struct {
Id string `json:"id"`
Name string `json:"name"`
ExtractorName string `json:"extractorName"`
}
type diagnosticSeverity string
const (
severityError diagnosticSeverity = "error"
severityWarning diagnosticSeverity = "warning"
severityNote diagnosticSeverity = "note"
)
type visibilityStruct struct {
StatusPage bool `json:"statusPage"` // True if the message should be displayed on the status page (defaults to false)
CliSummaryTable bool `json:"cliSummaryTable"` // True if the message should be counted in the diagnostics summary table printed by `codeql database analyze` (defaults to false)
Telemetry bool `json:"telemetry"` // True if the message should be sent to telemetry (defaults to false)
}
var fullVisibility *visibilityStruct = &visibilityStruct{true, true, true}
var telemetryOnly *visibilityStruct = &visibilityStruct{false, false, true}
type locationStruct struct {
File string `json:"file,omitempty"`
StartLine int `json:"startLine,omitempty"`
StartColumn int `json:"startColumn,omitempty"`
EndLine int `json:"endLine,omitempty"`
EndColumn int `json:"endColumn,omitempty"`
}
var noLocation *locationStruct = nil
type diagnostic struct {
Timestamp string `json:"timestamp"`
Source sourceStruct `json:"source"`
MarkdownMessage string `json:"markdownMessage"`
Severity string `json:"severity"`
Visibility *visibilityStruct `json:"visibility,omitempty"` // Use a pointer so that it is omitted if nil
Location *locationStruct `json:"location,omitempty"` // Use a pointer so that it is omitted if nil
}
var diagnosticsEmitted, diagnosticsLimit uint = 0, 100
var noDiagnosticDirPrinted bool = false
func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagnosticSeverity, visibility *visibilityStruct, location *locationStruct) {
if diagnosticsEmitted < diagnosticsLimit {
diagnosticsEmitted += 1
diagnosticDir := os.Getenv("CODEQL_EXTRACTOR_GO_DIAGNOSTIC_DIR")
if diagnosticDir == "" {
if !noDiagnosticDirPrinted {
log.Println("No diagnostic directory set, so not emitting diagnostic")
noDiagnosticDirPrinted = true
}
return
}
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000") + "Z"
var d diagnostic
if diagnosticsEmitted < diagnosticsLimit {
d = diagnostic{
timestamp,
sourceStruct{sourceid, sourcename, "go"},
markdownMessage,
string(severity),
visibility,
location,
}
} else {
d = diagnostic{
timestamp,
sourceStruct{"go/autobuilder/diagnostic-limit-reached", "Diagnostics limit exceeded", "go"},
fmt.Sprintf("CodeQL has produced more than the maximum number of diagnostics. Only the first %d have been reported.", diagnosticsLimit),
string(severityWarning),
fullVisibility,
noLocation,
}
}
content, err := json.Marshal(d)
if err != nil {
log.Println(err)
return
}
targetFile, err := os.CreateTemp(diagnosticDir, "go-extractor.*.json")
if err != nil {
log.Println("Failed to create diagnostic file: ")
log.Println(err)
return
}
defer func() {
if err := targetFile.Close(); err != nil {
log.Println("Failed to close diagnostic file:")
log.Println(err)
}
}()
_, err = targetFile.Write(content)
if err != nil {
log.Println("Failed to write to diagnostic file: ")
log.Println(err)
}
}
}
func EmitPackageDifferentOSArchitecture(pkgPath string) {
emitDiagnostic(
"go/autobuilder/package-different-os-architecture",
"An imported package is intended for a different OS or architecture",
"`"+pkgPath+"` could not be imported. Make sure the `GOOS` and `GOARCH` [environment variables are correctly set](https://docs.github.com/en/actions/learn-github-actions/variables#defining-environment-variables-for-a-single-workflow). Alternatively, [change your OS and architecture](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#using-a-github-hosted-runner).",
severityWarning,
fullVisibility,
noLocation,
)
}
func plural(n int, singular, plural string) string {
if n == 1 {
return singular
} else {
return plural
}
}
const maxNumPkgPaths = 5
func EmitCannotFindPackages(pkgPaths []string) {
numPkgPaths := len(pkgPaths)
numPrinted := numPkgPaths
truncated := false
if numPrinted > maxNumPkgPaths {
numPrinted = maxNumPkgPaths
truncated = true
}
secondLine := "`" + strings.Join(pkgPaths[0:numPrinted], "`, `") + "`"
if truncated {
secondLine += fmt.Sprintf(" and %d more", numPkgPaths-maxNumPkgPaths)
}
emitDiagnostic(
"go/autobuilder/package-not-found",
"Some packages could not be found",
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,
plural(len(pkgPaths), "", "s"),
secondLine),
severityWarning,
fullVisibility,
noLocation,
)
}
func EmitNewerGoVersionNeeded(installedVersion string, requiredVersion string) {
emitDiagnostic(
"go/autobuilder/newer-go-version-needed",
"Newer Go version needed",
"Version `"+installedVersion+"` of Go is installed, but this is lower than `"+requiredVersion+"` required by your project's `go.mod`. [Install a newer version of Go before analyzing your project](https://github.com/actions/setup-go#basic).",
severityError,
fullVisibility,
noLocation,
)
}
func EmitGoFilesFoundButNotProcessed() {
emitDiagnostic(
"go/autobuilder/go-files-found-but-not-processed",
"Go files were found but not processed",
"[Specify 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) that includes one or more `go build` commands to build the `.go` files to be analyzed.",
severityError,
fullVisibility,
noLocation,
)
}
func EmitGoFilesFoundButNotProcessedForDirectory(wd string) {
emitDiagnostic(
"go/autobuilder/go-files-found-but-not-processed-for-directory",
"Go files were found but not processed for a directory",
fmt.Sprintf(
"CodeQL was unable to extract any Go files in %s. If this is unexpected, [specify 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) that includes one or more `go build` commands to build the `.go` files to be analyzed.",
wd,
),
severityWarning,
fullVisibility,
noLocation,
)
}
func EmitRelativeImportPaths() {
emitDiagnostic(
"go/autobuilder/relative-import-paths",
"Some imports use unsupported relative package paths",
"You should replace relative package paths (that contain `.` or `..`) with absolute paths. Alternatively you can [use a Go module](https://go.dev/blog/using-go-modules).",
severityError,
fullVisibility,
noLocation,
)
}
// The following diagnostics are telemetry-only.
func EmitBazelBuildFilesFound(bazelPaths []string) {
emitDiagnostic(
"go/autobuilder/bazel-build-file-found",
"Bazel BUILD files were found",
fmt.Sprintf(
"%d bazel BUILD %s found:\n\n`%s`",
len(bazelPaths),
plural(len(bazelPaths), "file was", "files were"),
strings.Join(bazelPaths, "`, `")),
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGopkgTomlFound() {
emitDiagnostic(
"go/autobuilder/gopkg-toml-found",
"A dep `Gopkg.toml` file was found",
"A dep `Gopkg.toml` file was found",
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGlideYamlFound() {
emitDiagnostic(
"go/autobuilder/glide-yaml-found",
"A Glide `glide.yaml` file was found",
"A Glide `glide.yaml` file was found",
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoWorkFound(goWorkPaths []string) {
emitDiagnostic(
"go/autobuilder/go-work-found",
"`go.work` file found",
fmt.Sprintf(
"%d `go.work` %s found:\n\n`%s`",
len(goWorkPaths),
plural(len(goWorkPaths), "file was", "files were"),
strings.Join(goWorkPaths, "`, `")),
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoFilesOutsideGoModules(goModPaths []string) {
emitDiagnostic(
"go/autobuilder/go-files-outside-go-modules",
"Go files were found outside Go modules",
"Go files were found outside of the Go modules corresponding to these `go.mod` files.\n\n`"+strings.Join(goModPaths, "`, `")+"`",
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitMultipleGoModFoundNested(goModPaths []string) {
emitDiagnostic(
"go/autobuilder/multiple-go-mod-found-nested",
"Multiple `go.mod` files were found, all nested under one root `go.mod` file",
fmt.Sprintf(
"%d `go.mod` files were found:\n\n`%s`",
len(goModPaths),
strings.Join(goModPaths, "`, `")),
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitMultipleGoModFoundNotNested(goModPaths []string) {
emitDiagnostic(
"go/autobuilder/multiple-go-mod-found-not-nested",
"Multiple `go.mod` files found, not all nested under one root `go.mod` file",
fmt.Sprintf(
"%d `go.mod` files were found:\n\n`%s`",
len(goModPaths),
strings.Join(goModPaths, "`, `")),
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitSingleRootGoModFound(goModPath string) {
emitDiagnostic(
"go/autobuilder/single-root-go-mod-found",
"A single `go.mod` file was found in the root",
"A single `go.mod` file was found.\n\n`"+goModPath+"`",
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitSingleNonRootGoModFound(goModPath string) {
emitDiagnostic(
"go/autobuilder/single-non-root-go-mod-found",
"A single, non-root `go.mod` file was found",
"A single, non-root `go.mod` file was found.\n\n`"+goModPath+"`",
severityNote,
telemetryOnly,
noLocation,
)
}
// The following diagnostics are related to identifying the build environment.
func EmitNoGoModAndNoGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-no-go-mod-no-go-env",
"No `go.mod` file found and no Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitNoGoModAndGoEnvUnsupported(msg string) {
emitDiagnostic(
"go/autobuilder/env-no-go-mod-go-env-unsupported",
"No `go.mod` file found and Go version in environment is unsupported",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitNoGoModAndGoEnvSupported(msg string) {
emitDiagnostic(
"go/autobuilder/env-no-go-mod-go-env-supported",
"No `go.mod` file found and Go version in environment is supported",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooHighAndNoGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-high-no-go-env",
"Go version in `go.mod` file above supported range and no Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooHighAndEnvVersionTooHigh(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-high-go-env-too-high",
"Go version in `go.mod` file above supported range and Go version in environment above supported range",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooHighAndEnvVersionTooLow(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-high-go-env-too-low",
"Go version in `go.mod` file above supported range and Go version in environment below supported range",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooHighAndEnvVersionBelowMax(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-high-go-env-below-max",
"Go version in `go.mod` file above supported range and Go version in environment is supported and below the maximum supported version",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooHighAndEnvVersionMax(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-high-go-env-max",
"Go version in `go.mod` file above supported range and Go version in environment is the maximum supported version",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooLowAndNoGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-low-no-go-env",
"Go version in `go.mod` file below supported range and no Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooLowAndEnvVersionUnsupported(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-low-go-env-unsupported",
"Go version in `go.mod` file below supported range and Go version in environment unsupported",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionTooLowAndEnvVersionSupported(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-too-low-go-env-supported",
"Go version in `go.mod` file below supported range and Go version in environment supported",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionSupportedAndNoGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-supported-no-go-env",
"Go version in `go.mod` file in supported range and no Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionSupportedAndGoEnvUnsupported(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-supported-go-env-unsupported",
"Go version in `go.mod` file in supported range and Go version in environment unsupported",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionSupportedHigherGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-supported-higher-than-go-env",
"The Go version in `go.mod` file is supported and higher than the Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitGoModVersionSupportedLowerEqualGoEnv(msg string) {
emitDiagnostic(
"go/autobuilder/env-go-mod-version-supported-lower-than-or-equal-to-go-env",
"The Go version in `go.mod` file is supported and lower than or equal to the Go version in environment",
msg,
severityNote,
telemetryOnly,
noLocation,
)
}
func EmitNewerSystemGoRequired(requiredVersion string) {
emitDiagnostic(
"go/autobuilder/newer-system-go-version-required",
"The Go version installed on the system is too old to support this project",
"At least Go version `"+requiredVersion+"` is required to build this project, but the version installed on the system is older. [Install a newer version](https://github.com/actions/setup-go#basic).",
severityError,
fullVisibility,
noLocation,
)
}
func EmitExtractionFailedForProjects(path []string) {
emitDiagnostic(
"go/autobuilder/extraction-failed-for-project",
"Unable to extract some Go projects",
fmt.Sprintf(
"The following %d Go project%s could not be extracted successfully:\n\n`%s`\n",
len(path),
plural(len(path), "", "s"),
strings.Join(path, "`, `")),
severityWarning,
fullVisibility,
noLocation,
)
}
func EmitInvalidToolchainVersion(goModPath string, version string) {
emitDiagnostic(
"go/autobuilder/invalid-go-toolchain-version",
"Invalid Go toolchain version",
strings.Join([]string{
"As of Go 1.21, toolchain versions [must use the 1.N.P syntax](https://go.dev/doc/toolchain#version).",
fmt.Sprintf("`%s` in `%s` does not match this syntax and there is no additional `toolchain` directive, which may cause some `go` commands to fail.", version, goModPath),
},
"\n\n"),
severityWarning,
fullVisibility,
&locationStruct{File: goModPath},
)
}