Skip to content

Commit 04cb693

Browse files
committed
Use RealName instead of (dir) Name as key in librarymanager.Library map
1 parent b8187b4 commit 04cb693

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

arduino/libraries/librariesmanager/install.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ var (
4949
// install path, where the library should be installed and the possible library that is already
5050
// installed on the same folder and it's going to be replaced by the new one.
5151
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release, installLocation libraries.LibraryLocation) (*paths.Path, *libraries.Library, error) {
52-
saneName := utils.SanitizeName(indexLibrary.Library.Name)
53-
5452
var replaced *libraries.Library
55-
if installedLibs, have := lm.Libraries[saneName]; have {
53+
name := indexLibrary.Library.Name
54+
if installedLibs, have := lm.Libraries[name]; have {
5655
for _, installedLib := range installedLibs {
5756
if installedLib.Location != installLocation {
5857
continue
@@ -72,7 +71,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
7271
return nil, nil, fmt.Errorf(tr("Builtin libraries directory not set"))
7372
}
7473

75-
libPath := libsDir.Join(saneName)
74+
libPath := libsDir.Join(utils.SanitizeName(indexLibrary.Library.Name))
7675
if replaced != nil && replaced.InstallDir.EquivalentTo(libPath) {
7776

7877
} else if libPath.IsDir() {
@@ -99,9 +98,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
9998
return fmt.Errorf(tr("removing lib directory: %s"), err)
10099
}
101100

102-
alternatives := lm.Libraries[lib.Name]
101+
alternatives := lm.Libraries[lib.RealName]
103102
alternatives.Remove(lib)
104-
lm.Libraries[lib.Name] = alternatives
103+
lm.Libraries[lib.RealName] = alternatives
105104
return nil
106105
}
107106

@@ -191,8 +190,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
191190

192191
// InstallGitLib installs a library hosted on a git repository on the specified path.
193192
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
194-
libsDir := lm.getLibrariesDir(libraries.User)
195-
if libsDir == nil {
193+
installDir := lm.getLibrariesDir(libraries.User)
194+
if installDir == nil {
196195
return fmt.Errorf(tr("User directory not set"))
197196
}
198197

@@ -204,10 +203,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
204203
return err
205204
}
206205

207-
installPath := libsDir.Join(libraryName)
208-
209206
// Deletes libraries folder if already installed
210-
if _, ok := lm.Libraries[libraryName]; ok {
207+
installPath := installDir.Join(libraryName)
208+
if installPath.IsDir() {
211209
if !overwrite {
212210
return fmt.Errorf(tr("library %s already installed"), libraryName)
213211
}
@@ -217,6 +215,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
217215
Trace("Deleting library")
218216
installPath.RemoveAll()
219217
}
218+
if installPath.Exist() {
219+
return fmt.Errorf(tr("could not create directory %s: a file with the same name exists!", installPath))
220+
}
220221

221222
logrus.
222223
WithField("library name", libraryName).

arduino/libraries/librariesmanager/librariesmanager.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/arduino/arduino-cli/arduino/cores"
2323
"github.com/arduino/arduino-cli/arduino/libraries"
2424
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
25-
"github.com/arduino/arduino-cli/arduino/utils"
2625
"github.com/arduino/arduino-cli/i18n"
2726
paths "github.com/arduino/go-paths-helper"
2827
"github.com/pmylund/sortutil"
@@ -173,9 +172,9 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*
173172
continue
174173
}
175174
library.ContainerPlatform = librariesDir.PlatformRelease
176-
alternatives := lm.Libraries[library.Name]
175+
alternatives := lm.Libraries[library.RealName]
177176
alternatives.Add(library)
178-
lm.Libraries[library.Name] = alternatives
177+
lm.Libraries[library.RealName] = alternatives
179178
}
180179

181180
return statuses
@@ -194,18 +193,17 @@ func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location
194193
return fmt.Errorf(tr("loading library from %[1]s: %[2]s"), libRootDir, err)
195194
}
196195

197-
alternatives := lm.Libraries[library.Name]
196+
alternatives := lm.Libraries[library.RealName]
198197
alternatives.Add(library)
199-
lm.Libraries[library.Name] = alternatives
198+
lm.Libraries[library.RealName] = alternatives
200199
return nil
201200
}
202201

203202
// FindByReference return the installed library matching the Reference
204203
// name and version or, if the version is nil, the library installed
205204
// in the User folder.
206205
func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, installLocation libraries.LibraryLocation) *libraries.Library {
207-
saneName := utils.SanitizeName(libRef.Name)
208-
alternatives, have := lm.Libraries[saneName]
206+
alternatives, have := lm.Libraries[libRef.Name]
209207
if !have {
210208
return nil
211209
}

commands/lib/install.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
144144
if err := lm.Install(libRelease, libPath, installLocation); err != nil {
145145
return &arduino.FailedLibraryInstallError{Cause: err}
146146
}
147-
147+
if libReplaced != nil && !libReplaced.InstallDir.EquivalentTo(libPath) {
148+
if err := lm.Uninstall(libReplaced); err != nil {
149+
return fmt.Errorf("%s: %s", tr("could not remove old library"), err)
150+
}
151+
}
148152
taskCB(&rpc.TaskProgress{Message: tr("Installed %s", libRelease), Completed: true})
149153
return nil
150154
}

0 commit comments

Comments
 (0)