Skip to content

Commit a409b91

Browse files
committed
Improved behaviour of 'lib install --no-overwrite' flag
Previously the --no-overwrite flag would fail to install if a library dependency was already installed but not at the latest version. After this change the already present library may be accepted as a dependency if it match the version constraints of the installed library.
1 parent 3f38cd1 commit a409b91

File tree

8 files changed

+349
-284
lines changed

8 files changed

+349
-284
lines changed

Diff for: arduino/libraries/librariesindex/index.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,24 @@ func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release {
144144
return nil
145145
}
146146

147-
// ResolveDependencies returns the dependencies of a library release.
148-
func (idx *Index) ResolveDependencies(lib *Release) []*Release {
149-
// Create and populate the library resolver
147+
// ResolveDependencies resolve the dependencies of a library release and returns a
148+
// possible solution (the set of library releases to install together with the library).
149+
// An optional "override" releases may be passed if we want to exclude the same
150+
// libraries from the index (for example if we want to keep an installed library).
151+
func (idx *Index) ResolveDependencies(lib *Release, overrides []*Release) []*Release {
150152
resolver := semver.NewResolver[*Release, *Dependency]()
151-
for _, indexLib := range idx.Libraries {
153+
154+
overridden := map[string]bool{}
155+
for _, override := range overrides {
156+
resolver.AddRelease(override)
157+
overridden[override.GetName()] = true
158+
}
159+
160+
// Create and populate the library resolver
161+
for libName, indexLib := range idx.Libraries {
162+
if _, ok := overridden[libName]; ok {
163+
continue
164+
}
152165
for _, indexLibRelease := range indexLib.Releases {
153166
resolver.AddRelease(indexLibRelease)
154167
}

Diff for: arduino/libraries/librariesindex/index_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestIndexer(t *testing.T) {
9090
rtcInexistent2 := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero-blah", Version: semver.MustParse("1.0.0")})
9191
require.Nil(t, rtcInexistent2)
9292

93-
resolve1 := index.ResolveDependencies(alp.Releases["1.2.1"])
93+
resolve1 := index.ResolveDependencies(alp.Releases["1.2.1"], nil)
9494
require.Len(t, resolve1, 2)
9595
require.Contains(t, resolve1, alp.Releases["1.2.1"])
9696
require.Contains(t, resolve1, rtc.Releases["1.6.0"])
@@ -108,7 +108,7 @@ func TestIndexer(t *testing.T) {
108108
require.NotNil(t, http040)
109109
require.Equal(t, "[email protected]", http040.String())
110110

111-
resolve2 := index.ResolveDependencies(oauth010)
111+
resolve2 := index.ResolveDependencies(oauth010, nil)
112112
require.Len(t, resolve2, 4)
113113
require.Contains(t, resolve2, oauth010)
114114
require.Contains(t, resolve2, eccx135)

Diff for: arduino/libraries/librariesmanager/librariesmanager.go

+14
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, in
225225
return alternatives.FilterByVersionAndInstallLocation(libRef.Version, installLocation)
226226
}
227227

228+
// FindAllInstalled returns all the installed libraries
229+
func (lm *LibrariesManager) FindAllInstalled() libraries.List {
230+
var res libraries.List
231+
for _, libAlternatives := range lm.Libraries {
232+
for _, libRelease := range libAlternatives {
233+
if libRelease.InstallDir == nil {
234+
continue
235+
}
236+
res.Add(libRelease)
237+
}
238+
}
239+
return res
240+
}
241+
228242
func (lm *LibrariesManager) clearLibraries() {
229243
for k := range lm.Libraries {
230244
delete(lm.Libraries, k)

Diff for: commands/lib/install.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
4747
}
4848
} else {
4949
res, err := LibraryResolveDependencies(ctx, &rpc.LibraryResolveDependenciesRequest{
50-
Instance: req.GetInstance(),
51-
Name: req.GetName(),
52-
Version: req.GetVersion(),
50+
Instance: req.GetInstance(),
51+
Name: req.GetName(),
52+
Version: req.GetVersion(),
53+
DoNotUpdateInstalledLibraries: req.GetNoOverwrite(),
5354
})
5455
if err != nil {
5556
return err

Diff for: commands/lib/resolve_deps.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/arduino"
2424
"github.com/arduino/arduino-cli/arduino/libraries"
25+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2526
"github.com/arduino/arduino-cli/commands/internal/instances"
2627
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2728
)
@@ -46,7 +47,21 @@ func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDepe
4647
}
4748

4849
// Resolve all dependencies...
49-
deps := lm.Index.ResolveDependencies(reqLibRelease)
50+
var overrides []*librariesindex.Release
51+
if req.GetDoNotUpdateInstalledLibraries() {
52+
libs := lm.FindAllInstalled()
53+
libs = libs.FilterByVersionAndInstallLocation(nil, libraries.User)
54+
for _, lib := range libs {
55+
release := lm.Index.FindRelease(&librariesindex.Reference{
56+
Name: lib.Name,
57+
Version: lib.Version,
58+
})
59+
if release != nil {
60+
overrides = append(overrides, release)
61+
}
62+
}
63+
}
64+
deps := lm.Index.ResolveDependencies(reqLibRelease, overrides)
5065

5166
// If no solution has been found
5267
if len(deps) == 0 {

Diff for: internal/cli/lib/check_deps.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
)
3434

3535
func initDepsCommand() *cobra.Command {
36+
var noOverwrite bool
3637
depsCommand := &cobra.Command{
3738
Use: fmt.Sprintf("deps %s[@%s]...", tr("LIBRARY"), tr("VERSION_NUMBER")),
3839
Short: tr("Check dependencies status for the specified library."),
@@ -41,15 +42,18 @@ func initDepsCommand() *cobra.Command {
4142
" " + os.Args[0] + " lib deps AudioZero # " + tr("for the latest version.") + "\n" +
4243
" " + os.Args[0] + " lib deps [email protected] # " + tr("for the specific version."),
4344
Args: cobra.ExactArgs(1),
44-
Run: runDepsCommand,
45+
Run: func(cmd *cobra.Command, args []string) {
46+
runDepsCommand(args, noOverwrite)
47+
},
4548
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4649
return arguments.GetInstalledLibraries(), cobra.ShellCompDirectiveDefault
4750
},
4851
}
52+
depsCommand.Flags().BoolVar(&noOverwrite, "no-overwrite", false, tr("Do not try to update library dependencies if already installed."))
4953
return depsCommand
5054
}
5155

52-
func runDepsCommand(cmd *cobra.Command, args []string) {
56+
func runDepsCommand(args []string, noOverwrite bool) {
5357
instance := instance.CreateAndInit()
5458
logrus.Info("Executing `arduino-cli lib deps`")
5559
libRef, err := ParseLibraryReferenceArgAndAdjustCase(instance, args[0])
@@ -58,9 +62,10 @@ func runDepsCommand(cmd *cobra.Command, args []string) {
5862
}
5963

6064
deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesRequest{
61-
Instance: instance,
62-
Name: libRef.Name,
63-
Version: libRef.Version,
65+
Instance: instance,
66+
Name: libRef.Name,
67+
Version: libRef.Version,
68+
DoNotUpdateInstalledLibraries: noOverwrite,
6469
})
6570
if err != nil {
6671
feedback.Fatal(tr("Error resolving dependencies for %[1]s: %[2]s", libRef, err), feedback.ErrGeneric)

0 commit comments

Comments
 (0)