diff --git a/arduino/libraries/librariesmanager/install.go b/arduino/libraries/librariesmanager/install.go index 5a3db0ff646..ba187af5879 100644 --- a/arduino/libraries/librariesmanager/install.go +++ b/arduino/libraries/librariesmanager/install.go @@ -64,6 +64,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(name string, version *semve return nil, err } + lm.RescanLibraries() libs := lm.FindByReference(&librariesindex.Reference{Name: name}, installLocation) if len(libs) > 1 { diff --git a/arduino/libraries/librariesmanager/librariesmanager.go b/arduino/libraries/librariesmanager/librariesmanager.go index 30dbf69d644..943b4fdf77a 100644 --- a/arduino/libraries/librariesmanager/librariesmanager.go +++ b/arduino/libraries/librariesmanager/librariesmanager.go @@ -132,6 +132,7 @@ func (lm *LibrariesManager) AddPlatformReleaseLibrariesDir(plaftormRelease *core // RescanLibraries reload all installed libraries in the system. func (lm *LibrariesManager) RescanLibraries() []*status.Status { + lm.clearLibraries() statuses := []*status.Status{} for _, dir := range lm.LibrariesDir { if errs := lm.LoadLibrariesFromDir(dir); len(errs) > 0 { @@ -217,3 +218,9 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, in } return alternatives.FilterByVersionAndInstallLocation(libRef.Version, installLocation) } + +func (lm *LibrariesManager) clearLibraries() { + for k := range lm.Libraries { + delete(lm.Libraries, k) + } +} diff --git a/arduino/libraries/librariesmanager/librariesmanager_test.go b/arduino/libraries/librariesmanager/librariesmanager_test.go new file mode 100644 index 00000000000..a987462b6b2 --- /dev/null +++ b/arduino/libraries/librariesmanager/librariesmanager_test.go @@ -0,0 +1,32 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. +package librariesmanager + +import ( + "testing" + + "github.com/arduino/arduino-cli/arduino/libraries" + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" +) + +func Test_RescanLibrariesCallClear(t *testing.T) { + baseDir := paths.New(t.TempDir()) + lm := NewLibraryManager(baseDir.Join("index_dir"), baseDir.Join("downloads_dir")) + lm.Libraries["testLibA"] = libraries.List{} + lm.Libraries["testLibB"] = libraries.List{} + lm.RescanLibraries() + require.Len(t, lm.Libraries, 0) +} diff --git a/internal/integrationtest/daemon/daemon_test.go b/internal/integrationtest/daemon/daemon_test.go index 8685a7c0750..779eeb44ab2 100644 --- a/internal/integrationtest/daemon/daemon_test.go +++ b/internal/integrationtest/daemon/daemon_test.go @@ -382,3 +382,36 @@ func TestDaemonBundleLibInstall(t *testing.T) { } } } + +func TestDaemonLibrariesRescanOnInstall(t *testing.T) { + /* + Ensures that the libraries are rescanned prior to installing a new one, + to avoid clashes with libraries installed after the daemon initialization. + To perform the check: + - the daemon is run and a gprc instance initialized + - a library is installed through the cli + - an attempt to install a new version of the library is done + with the gprc instance + The last attempt is expected to not raise an error + */ + env, cli := createEnvForDaemon(t) + defer env.CleanUp() + + grpcInst := cli.Create() + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { + fmt.Printf("INIT> %v\n", ir.GetMessage()) + })) + cli.Run("lib", "install", "SD@1.2.3") + + instCl, err := grpcInst.LibraryInstall(context.Background(), "SD", "1.2.4", false, false, true) + + require.NoError(t, err) + for { + _, err := instCl.Recv() + if err == io.EOF { + break + } + require.NoError(t, err) + } + +}