@@ -26,6 +26,7 @@ import (
2626 "strings"
2727 "time"
2828
29+ "github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
2930 "github.com/arduino/arduino-cli/internal/arduino/builder/internal/logger"
3031 "github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor"
3132 "github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
@@ -57,6 +58,7 @@ type SketchLibrariesDetector struct {
5758 librariesResolutionResults map [string ]libraryResolutionResult
5859 includeFolders paths.PathList
5960 logger * logger.BuilderLogger
61+ diagnosticStore * diagnostics.Store
6062}
6163
6264// NewSketchLibrariesDetector todo
@@ -66,6 +68,7 @@ func NewSketchLibrariesDetector(
6668 useCachedLibrariesResolution bool ,
6769 onlyUpdateCompilationDatabase bool ,
6870 logger * logger.BuilderLogger ,
71+ diagnosticStore * diagnostics.Store ,
6972) * SketchLibrariesDetector {
7073 return & SketchLibrariesDetector {
7174 librariesManager : lm ,
@@ -76,6 +79,7 @@ func NewSketchLibrariesDetector(
7679 includeFolders : paths.PathList {},
7780 onlyUpdateCompilationDatabase : onlyUpdateCompilationDatabase ,
7881 logger : logger ,
82+ diagnosticStore : diagnosticStore ,
7983 }
8084}
8185
@@ -337,7 +341,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
337341 }
338342
339343 var preprocErr error
340- var preprocStderr [] byte
344+ var preprocFirstResult preprocessor. Result
341345
342346 var missingIncludeH string
343347 if unchanged && cache .valid {
@@ -346,21 +350,20 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
346350 l .logger .Info (tr ("Using cached library dependencies for file: %[1]s" , sourcePath ))
347351 }
348352 } else {
349- var preprocStdout []byte
350- preprocStdout , preprocStderr , preprocErr = preprocessor .GCC (sourcePath , targetFilePath , includeFolders , buildProperties )
353+ preprocFirstResult , preprocErr = preprocessor .GCC (sourcePath , targetFilePath , includeFolders , buildProperties )
351354 if l .logger .Verbose () {
352- l .logger .WriteStdout (preprocStdout )
355+ l .logger .WriteStdout (preprocFirstResult . Stdout () )
353356 }
354357 // Unwrap error and see if it is an ExitError.
355358 var exitErr * exec.ExitError
356359 if preprocErr == nil {
357360 // Preprocessor successful, done
358361 missingIncludeH = ""
359- } else if isExitErr := errors .As (preprocErr , & exitErr ); ! isExitErr || preprocStderr == nil {
362+ } else if isExitErr := errors .As (preprocErr , & exitErr ); ! isExitErr || preprocFirstResult . Stderr () == nil {
360363 // Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
361364 return preprocErr
362365 } else {
363- missingIncludeH = IncludesFinderWithRegExp (string (preprocStderr ))
366+ missingIncludeH = IncludesFinderWithRegExp (string (preprocFirstResult . Stderr () ))
364367 if missingIncludeH == "" && l .logger .Verbose () {
365368 l .logger .Info (tr ("Error while detecting libraries included by %[1]s" , sourcePath ))
366369 }
@@ -376,22 +379,25 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
376379 library := l .resolveLibrary (missingIncludeH , platformArch )
377380 if library == nil {
378381 // Library could not be resolved, show error
379- if preprocErr == nil || preprocStderr == nil {
382+ if preprocErr == nil || preprocFirstResult . Stderr () == nil {
380383 // Filename came from cache, so run preprocessor to obtain error to show
381- var preprocStdout []byte
382- preprocStdout , preprocStderr , preprocErr = preprocessor .GCC (sourcePath , targetFilePath , includeFolders , buildProperties )
384+ result , err := preprocessor .GCC (sourcePath , targetFilePath , includeFolders , buildProperties )
383385 if l .logger .Verbose () {
384- l .logger .WriteStdout (preprocStdout )
386+ l .logger .WriteStdout (result . Stdout () )
385387 }
386- if preprocErr == nil {
388+ if err == nil {
387389 // If there is a missing #include in the cache, but running
388390 // gcc does not reproduce that, there is something wrong.
389391 // Returning an error here will cause the cache to be
390392 // deleted, so hopefully the next compilation will succeed.
391393 return errors .New (tr ("Internal error in cache" ))
392394 }
395+ l .diagnosticStore .Parse (result .Args (), result .Stderr ())
396+ l .logger .WriteStderr (result .Stderr ())
397+ return err
393398 }
394- l .logger .WriteStderr (preprocStderr )
399+ l .diagnosticStore .Parse (preprocFirstResult .Args (), preprocFirstResult .Stderr ())
400+ l .logger .WriteStderr (preprocFirstResult .Stderr ())
395401 return preprocErr
396402 }
397403
0 commit comments