@@ -2,6 +2,7 @@ package diagnostics
22
33import (
44 "encoding/json"
5+ "fmt"
56 "log"
67 "os"
78 "time"
@@ -46,14 +47,18 @@ type diagnostic struct {
4647}
4748
4849var diagnosticsEmitted , diagnosticsLimit uint = 0 , 100
50+ var noDiagnosticDirPrinted bool = false
4951
5052func emitDiagnostic (sourceid , sourcename , markdownMessage string , severity diagnosticSeverity , internal , visibilitySP , visibilityCST , visibilityT bool , file string , startLine , startColumn , endLine , endColumn int ) {
51- if diagnosticsEmitted <= diagnosticsLimit {
53+ if diagnosticsEmitted < diagnosticsLimit {
5254 diagnosticsEmitted += 1
5355
5456 diagnosticDir := os .Getenv ("CODEQL_EXTRACTOR_GO_DIAGNOSTIC_DIR" )
5557 if diagnosticDir == "" {
56- log .Println ("No diagnostic directory set, so not emitting diagnostic" )
58+ if ! noDiagnosticDirPrinted {
59+ log .Println ("No diagnostic directory set, so not emitting diagnostic" )
60+ noDiagnosticDirPrinted = true
61+ }
5762 return
5863 }
5964
@@ -73,6 +78,18 @@ func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagn
7378 optLoc ,
7479 }
7580
81+ if diagnosticsEmitted == diagnosticsLimit {
82+ d = diagnostic {
83+ time .Now ().UTC ().Format ("2006-01-02T15:04:05.000" ) + "Z" ,
84+ sourceStruct {"go/diagnostic-limit-hit" , "Some diagnostics were dropped" , "go" },
85+ fmt .Sprintf ("The number of diagnostics exceeded the limit (%d); the remainder were dropped." , diagnosticsLimit ),
86+ string (severityWarning ),
87+ false ,
88+ visibilityStruct {true , true , true },
89+ nil ,
90+ }
91+ }
92+
7693 content , err := json .Marshal (d )
7794 if err != nil {
7895 log .Println (err )
@@ -83,7 +100,12 @@ func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagn
83100 log .Println ("Failed to create temporary file for diagnostic: " )
84101 log .Println (err )
85102 }
86- defer targetFile .Close ()
103+ defer func () {
104+ if err := targetFile .Close (); err != nil {
105+ log .Println ("Failed to close diagnostic file:" )
106+ log .Println (err )
107+ }
108+ }()
87109
88110 _ , err = targetFile .Write (content )
89111 if err != nil {
0 commit comments