@@ -128,8 +128,19 @@ func compileImage(
128128 return nil , err
129129 }
130130
131+ // Validate that there is a single result for all files.
132+ if len (results ) != 1 {
133+ return nil , fmt .Errorf ("expected a single result from query, instead got: %d" , len (results ))
134+ }
135+
136+ // Check for fatal errors before processing diagnostics so that callers can
137+ // inspect typed errors (e.g. DuplicateProtoPathError) via errors.As. Spanless
138+ // diagnostics that accompany these fatal errors are suppressed in the loop below.
139+ if results [0 ].Fatal != nil {
140+ return nil , results [0 ].Fatal
141+ }
142+
131143 var fileAnnotations []bufanalysis.FileAnnotation
132- var rawErrors []error
133144 for _ , diagnostic := range diagnostics .Diagnostics {
134145 if diagnostic .Level () > report .Error {
135146 // We only surface [report.Error] level or more severe diagnostics as build errors.
@@ -138,12 +149,9 @@ func compileImage(
138149 }
139150 primary := diagnostic .Primary ()
140151 if primary .IsZero () {
141- // Diagnostics without a source span (e.g. file-open errors such as
142- // DuplicateProtoPathError) cannot be represented as FileAnnotations.
143- // Surface them as raw errors so callers see the full message.
144- if diagnostic .File () != "" {
145- rawErrors = append (rawErrors , errors .New (diagnostic .Message ()))
146- }
152+ // Spanless diagnostics (e.g. from file-open errors like DuplicateProtoPathError)
153+ // are companion messages to fatal errors already handled above. Suppress them here
154+ // so their typed errors are preserved for callers rather than being lost as strings.
147155 continue
148156 }
149157 start := primary .Location (primary .Start , length .Bytes )
@@ -174,21 +182,9 @@ func compileImage(
174182 ),
175183 )
176184 }
177- if len (rawErrors ) > 0 {
178- return nil , errors .Join (rawErrors ... )
179- }
180185 if len (fileAnnotations ) > 0 {
181186 return nil , bufanalysis .NewFileAnnotationSet (fileAnnotations ... )
182187 }
183-
184- // Validate that there is a single result for all files
185- if len (results ) != 1 {
186- return nil , fmt .Errorf ("expected a single result from query, instead got: %d" , len (results ))
187- }
188-
189- if results [0 ].Fatal != nil {
190- return nil , results [0 ].Fatal
191- }
192188 fds , resolver , err := resolverForFDS (results [0 ].Value )
193189 if err != nil {
194190 return nil , err
0 commit comments