From 13249e1c9e8ad7beffd4ee4b9746144e19713e4c Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Fri, 7 Apr 2023 12:38:00 +0200
Subject: [PATCH] fix: library loading errors genereates unneeded compile
 failures

---
 commands/instances.go                         |  6 ++---
 .../integrationtest/compile_3/compile_test.go | 26 +++++++++++++++++++
 legacy/builder/libraries_loader.go            |  6 +++--
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/commands/instances.go b/commands/instances.go
index 2c8c44ade5e..8194f3dbd03 100644
--- a/commands/instances.go
+++ b/commands/instances.go
@@ -458,9 +458,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
 		}
 	}
 
-	for _, err := range lm.RescanLibraries() {
-		s := status.Newf(codes.FailedPrecondition, tr("Loading libraries: %v"), err)
-		responseError(s)
+	for _, status := range lm.RescanLibraries() {
+		logrus.WithError(status.Err()).Warnf("Error loading library")
+		// TODO: report as warning: responseError(err)
 	}
 
 	// Refreshes the locale used, this will change the
diff --git a/internal/integrationtest/compile_3/compile_test.go b/internal/integrationtest/compile_3/compile_test.go
index 11aaeceef5e..8e6aa34296e 100644
--- a/internal/integrationtest/compile_3/compile_test.go
+++ b/internal/integrationtest/compile_3/compile_test.go
@@ -16,6 +16,7 @@
 package compile_test
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/arduino/arduino-cli/internal/integrationtest"
@@ -154,3 +155,28 @@ func TestCompileRelativeLibraryPath(t *testing.T) {
 	require.Contains(t, string(stdout), "Used: "+FooLib.String())
 	require.Contains(t, string(stdout), "Not used: "+cli.SketchbookDir().Join("libraries", "FooLib").String())
 }
+
+func TestCompileWithInvalidLibrary(t *testing.T) {
+	env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
+	defer env.CleanUp()
+
+	_, _, err := cli.Run("core", "install", "arduino:avr")
+	require.NoError(t, err)
+
+	// Make an empty library
+	emptyLibPath := cli.SketchbookDir().Join("libraries", "EmptyLib")
+	require.NoError(t, emptyLibPath.MkdirAll())
+
+	// prepare sketch
+	sketch, err := paths.New("testdata", "bare_minimum").Abs()
+	require.NoError(t, err)
+
+	// Compile must succeed
+	_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", sketch.String())
+	require.NoError(t, err)
+
+	// Verbose compile must report invalid library
+	_, stderr, err := cli.Run("compile", "-v", "-b", "arduino:avr:uno", sketch.String())
+	require.NoError(t, err)
+	require.Contains(t, string(stderr), fmt.Sprintf("loading library from %s: invalid library: no header files found", emptyLibPath))
+}
diff --git a/legacy/builder/libraries_loader.go b/legacy/builder/libraries_loader.go
index b7e7792adc5..b41c49edcb1 100644
--- a/legacy/builder/libraries_loader.go
+++ b/legacy/builder/libraries_loader.go
@@ -56,14 +56,16 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
 			lm.AddLibrariesDir(folder, libraries.User)
 		}
 
-		if errs := lm.RescanLibraries(); len(errs) > 0 {
+		for _, status := range lm.RescanLibraries() {
 			// With the refactoring of the initialization step of the CLI we changed how
 			// errors are returned when loading platforms and libraries, that meant returning a list of
 			// errors instead of a single one to enhance the experience for the user.
 			// I have no intention right now to start a refactoring of the legacy package too, so
 			// here's this shitty solution for now.
 			// When we're gonna refactor the legacy package this will be gone.
-			return errors.WithStack(errs[0].Err())
+			if ctx.Verbose {
+				ctx.Warn(status.Message())
+			}
 		}
 
 		for _, dir := range ctx.LibraryDirs {