@@ -29,6 +29,8 @@ type visibilityStruct struct {
2929 Telemetry bool `json:"telemetry"` // True if the message should be sent to telemetry (defaults to false)
3030}
3131
32+ var fullVisibility * visibilityStruct = & visibilityStruct {true , true , true }
33+
3234type locationStruct struct {
3335 File string `json:"file,omitempty"`
3436 StartLine int `json:"startLine,omitempty"`
@@ -37,20 +39,22 @@ type locationStruct struct {
3739 EndColumn int `json:"endColumn,omitempty"`
3840}
3941
42+ var noLocation * locationStruct = nil
43+
4044type diagnostic struct {
41- Timestamp string `json:"timestamp"`
42- Source sourceStruct `json:"source"`
43- MarkdownMessage string `json:"markdownMessage"`
44- Severity string `json:"severity"`
45- Internal bool `json:"internal"`
46- Visibility visibilityStruct `json:"visibility"`
47- Location * locationStruct `json:"location,omitempty"` // Use a pointer so that it is omitted if nil
45+ Timestamp string `json:"timestamp"`
46+ Source sourceStruct `json:"source"`
47+ MarkdownMessage string `json:"markdownMessage"`
48+ Severity string `json:"severity"`
49+ Internal bool `json:"internal"`
50+ Visibility * visibilityStruct `json:"visibility,omitempty"` // Use a pointer so that it is omitted if nil
51+ Location * locationStruct `json:"location,omitempty"` // Use a pointer so that it is omitted if nil
4852}
4953
5054var diagnosticsEmitted , diagnosticsLimit uint = 0 , 100
5155var noDiagnosticDirPrinted bool = false
5256
53- func emitDiagnostic (sourceid , sourcename , markdownMessage string , severity diagnosticSeverity , internal , visibilitySP , visibilityCST , visibilityT bool , file string , startLine , startColumn , endLine , endColumn int ) {
57+ func emitDiagnostic (sourceid , sourcename , markdownMessage string , severity diagnosticSeverity , internal bool , visibility * visibilityStruct , location * locationStruct ) {
5458 if diagnosticsEmitted < diagnosticsLimit {
5559 diagnosticsEmitted += 1
5660
@@ -63,43 +67,39 @@ func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagn
6367 return
6468 }
6569
66- var optLoc * locationStruct
67- if file == "" && startLine == 0 && startColumn == 0 && endLine == 0 && endColumn == 0 {
68- optLoc = nil
69- } else {
70- optLoc = & locationStruct {file , startLine , startColumn , endLine , endColumn }
71- }
72-
7370 timestamp := time .Now ().UTC ().Format ("2006-01-02T15:04:05.000" ) + "Z"
7471
75- d := diagnostic {
76- timestamp ,
77- sourceStruct {sourceid , sourcename , "go" },
78- markdownMessage ,
79- string (severity ),
80- internal ,
81- visibilityStruct {visibilitySP , visibilityCST , visibilityT },
82- optLoc ,
83- }
72+ var d diagnostic
8473
85- if diagnosticsEmitted == diagnosticsLimit {
74+ if diagnosticsEmitted < diagnosticsLimit {
8675 d = diagnostic {
8776 timestamp ,
88- sourceStruct {"go/autobuilder/diagnostic-limit-hit" , "Some diagnostics were dropped" , "go" },
89- fmt .Sprintf ("The number of diagnostics exceeded the limit (%d); the remainder were dropped." , diagnosticsLimit ),
77+ sourceStruct {sourceid , sourcename , "go" },
78+ markdownMessage ,
79+ string (severity ),
80+ internal ,
81+ visibility ,
82+ location ,
83+ }
84+ } else {
85+ d = diagnostic {
86+ timestamp ,
87+ sourceStruct {"go/autobuilder/diagnostic-limit-reached" , "Diagnostics limit exceeded" , "go" },
88+ fmt .Sprintf ("CodeQL has produced more than the maximum number of diagnostics. Only the first %d have been reported." , diagnosticsLimit ),
9089 string (severityWarning ),
9190 false ,
92- visibilityStruct { true , true , true } ,
93- nil ,
91+ visibility ,
92+ location ,
9493 }
9594 }
9695
9796 content , err := json .Marshal (d )
9897 if err != nil {
9998 log .Println (err )
99+ return
100100 }
101101
102- targetFile , err := os .CreateTemp (diagnosticDir , "go-extractor.*.jsonl " )
102+ targetFile , err := os .CreateTemp (diagnosticDir , "go-extractor.*.json " )
103103 if err != nil {
104104 log .Println ("Failed to create temporary file for diagnostic: " )
105105 log .Println (err )
@@ -124,8 +124,8 @@ func EmitPackageDifferentOSArchitecture(pkgPath string) {
124124 "Package " + pkgPath + " is intended for a different OS or architecture" ,
125125 "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)." ,
126126 severityWarning , false ,
127- true , true , true ,
128- "" , 0 , 0 , 0 , 0 ,
127+ fullVisibility ,
128+ noLocation ,
129129 )
130130}
131131
@@ -156,8 +156,8 @@ func EmitCannotFindPackages(pkgPaths []string) {
156156 fmt .Sprintf ("%d package%s could not be found" , numPkgPaths , ending ),
157157 "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 ,
158158 severityError , false ,
159- true , true , true ,
160- "" , 0 , 0 , 0 , 0 ,
159+ fullVisibility ,
160+ noLocation ,
161161 )
162162}
163163
@@ -167,8 +167,8 @@ func EmitNewerGoVersionNeeded() {
167167 "Newer Go version needed" ,
168168 "The detected version of Go is lower than the version specified in `go.mod`. [Install a newer version](https://github.com/actions/setup-go#basic)." ,
169169 severityError , false ,
170- true , true , true ,
171- "" , 0 , 0 , 0 , 0 ,
170+ fullVisibility ,
171+ noLocation ,
172172 )
173173}
174174
@@ -178,7 +178,7 @@ func EmitGoFilesFoundButNotProcessed() {
178178 "Go files were found but not processed" ,
179179 "[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." ,
180180 severityError , false ,
181- true , true , true ,
182- "" , 0 , 0 , 0 , 0 ,
181+ fullVisibility ,
182+ noLocation ,
183183 )
184184}
0 commit comments