Skip to content

fix: scan libraries on install #2037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions arduino/libraries/librariesmanager/librariesmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
32 changes: 32 additions & 0 deletions arduino/libraries/librariesmanager/librariesmanager_test.go
Original file line number Diff line number Diff line change
@@ -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 [email protected].
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)
}
33 changes: 33 additions & 0 deletions internal/integrationtest/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")

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)
}

}