Skip to content

Commit 97451d8

Browse files
committed
Simplified error reporting in library detection
There is no need to duplicate the preprocessResult/Err variables. This also simplifies naming making it more straighforward.
1 parent 4601b98 commit 97451d8

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

internal/arduino/builder/internal/detector/detector.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
356356
}
357357

358358
var preprocErr error
359-
var preprocFirstResult *runner.Result
359+
var preprocResult *runner.Result
360360

361361
var missingIncludeH string
362362
if entry := l.cache.Peek(); unchanged && entry != nil && entry.MissingIncludeH != nil {
@@ -366,20 +366,20 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
366366
}
367367
first = false
368368
} else {
369-
preprocFirstResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
369+
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
370370
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
371-
l.logger.WriteStdout(preprocFirstResult.Stdout)
371+
l.logger.WriteStdout(preprocResult.Stdout)
372372
}
373373
// Unwrap error and see if it is an ExitError.
374374
var exitErr *exec.ExitError
375375
if preprocErr == nil {
376376
// Preprocessor successful, done
377377
missingIncludeH = ""
378-
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || len(preprocFirstResult.Stderr) == 0 {
378+
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || len(preprocResult.Stderr) == 0 {
379379
// Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
380380
return preprocErr
381381
} else {
382-
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr))
382+
missingIncludeH = IncludesFinderWithRegExp(string(preprocResult.Stderr))
383383
if missingIncludeH == "" && l.logger.VerbosityLevel() == logger.VerbosityVerbose {
384384
l.logger.Info(i18n.Tr("Error while detecting libraries included by %[1]s", sourcePath))
385385
}
@@ -396,25 +396,23 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
396396
library := l.resolveLibrary(missingIncludeH, platformArch)
397397
if library == nil {
398398
// Library could not be resolved, show error
399-
if preprocErr == nil || len(preprocFirstResult.Stderr) == 0 {
400-
// Filename came from cache, so run preprocessor to obtain error to show
401-
result, err := preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
399+
400+
// If preprocess result came from cache, run the preprocessor to obtain the actual error to show
401+
if preprocErr == nil || len(preprocResult.Stderr) == 0 {
402+
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
402403
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
403-
l.logger.WriteStdout(result.Stdout)
404+
l.logger.WriteStdout(preprocResult.Stdout)
404405
}
405-
if err == nil {
406+
if preprocErr == nil {
406407
// If there is a missing #include in the cache, but running
407408
// gcc does not reproduce that, there is something wrong.
408409
// Returning an error here will cause the cache to be
409410
// deleted, so hopefully the next compilation will succeed.
410411
return errors.New(i18n.Tr("Internal error in cache"))
411412
}
412-
l.diagnosticStore.Parse(result.Args, result.Stderr)
413-
l.logger.WriteStderr(result.Stderr)
414-
return err
415413
}
416-
l.diagnosticStore.Parse(preprocFirstResult.Args, preprocFirstResult.Stderr)
417-
l.logger.WriteStderr(preprocFirstResult.Stderr)
414+
l.diagnosticStore.Parse(preprocResult.Args, preprocResult.Stderr)
415+
l.logger.WriteStderr(preprocResult.Stderr)
418416
return preprocErr
419417
}
420418

0 commit comments

Comments
 (0)