Skip to content

Commit b48c689

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 1060bb5 commit b48c689

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
@@ -328,7 +328,7 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
328328
}
329329

330330
var preprocErr error
331-
var preprocFirstResult *runner.Result
331+
var preprocResult *runner.Result
332332

333333
var missingIncludeH string
334334
if entry := l.cache.Peek(); unchanged && entry != nil && entry.MissingIncludeH != nil {
@@ -338,20 +338,20 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
338338
}
339339
first = false
340340
} else {
341-
preprocFirstResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
341+
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
342342
if l.logger.Verbose() {
343-
l.logger.WriteStdout(preprocFirstResult.Stdout)
343+
l.logger.WriteStdout(preprocResult.Stdout)
344344
}
345345
// Unwrap error and see if it is an ExitError.
346346
var exitErr *exec.ExitError
347347
if preprocErr == nil {
348348
// Preprocessor successful, done
349349
missingIncludeH = ""
350-
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || len(preprocFirstResult.Stderr) == 0 {
350+
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || len(preprocResult.Stderr) == 0 {
351351
// Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
352352
return preprocErr
353353
} else {
354-
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr))
354+
missingIncludeH = IncludesFinderWithRegExp(string(preprocResult.Stderr))
355355
if missingIncludeH == "" && l.logger.Verbose() {
356356
l.logger.Info(i18n.Tr("Error while detecting libraries included by %[1]s", sourcePath))
357357
}
@@ -368,25 +368,23 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
368368
library := l.resolveLibrary(missingIncludeH, platformArch)
369369
if library == nil {
370370
// Library could not be resolved, show error
371-
if preprocErr == nil || len(preprocFirstResult.Stderr) == 0 {
372-
// Filename came from cache, so run preprocessor to obtain error to show
373-
result, err := preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
371+
372+
// If preprocess result came from cache, run the preprocessor to obtain the actual error to show
373+
if preprocErr == nil || len(preprocResult.Stderr) == 0 {
374+
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
374375
if l.logger.Verbose() {
375-
l.logger.WriteStdout(result.Stdout)
376+
l.logger.WriteStdout(preprocResult.Stdout)
376377
}
377-
if err == nil {
378+
if preprocErr == nil {
378379
// If there is a missing #include in the cache, but running
379380
// gcc does not reproduce that, there is something wrong.
380381
// Returning an error here will cause the cache to be
381382
// deleted, so hopefully the next compilation will succeed.
382383
return errors.New(i18n.Tr("Internal error in cache"))
383384
}
384-
l.diagnosticStore.Parse(result.Args, result.Stderr)
385-
l.logger.WriteStderr(result.Stderr)
386-
return err
387385
}
388-
l.diagnosticStore.Parse(preprocFirstResult.Args, preprocFirstResult.Stderr)
389-
l.logger.WriteStderr(preprocFirstResult.Stderr)
386+
l.diagnosticStore.Parse(preprocResult.Args, preprocResult.Stderr)
387+
l.logger.WriteStderr(preprocResult.Stderr)
390388
return preprocErr
391389
}
392390

0 commit comments

Comments
 (0)