@@ -2,6 +2,7 @@ package diagnostics
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"log"
6
7
"os"
7
8
"time"
@@ -46,14 +47,18 @@ type diagnostic struct {
46
47
}
47
48
48
49
var diagnosticsEmitted , diagnosticsLimit uint = 0 , 100
50
+ var noDiagnosticDirPrinted bool = false
49
51
50
52
func 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 {
52
54
diagnosticsEmitted += 1
53
55
54
56
diagnosticDir := os .Getenv ("CODEQL_EXTRACTOR_GO_DIAGNOSTIC_DIR" )
55
57
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
+ }
57
62
return
58
63
}
59
64
@@ -73,6 +78,18 @@ func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagn
73
78
optLoc ,
74
79
}
75
80
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
+
76
93
content , err := json .Marshal (d )
77
94
if err != nil {
78
95
log .Println (err )
@@ -83,7 +100,12 @@ func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagn
83
100
log .Println ("Failed to create temporary file for diagnostic: " )
84
101
log .Println (err )
85
102
}
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
+ }()
87
109
88
110
_ , err = targetFile .Write (content )
89
111
if err != nil {
0 commit comments