Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fixed nil exception on some rare case with multiple libs selection
The closestmatch algorithm may return 'nil' if none of the candidate
libraries has enough matching of the name. In this case just return the
first library in alphanumeric ordering.

See arduino/Arduino#9242
  • Loading branch information
cmaglie committed Nov 8, 2019
commit fc5029dad67029a97e4f64bf7dc3f79bed8ecf4c
8 changes: 8 additions & 0 deletions arduino/libraries/librarieslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (list *List) SortByArchitecturePriority(arch string) {
})
}

// SortByName sorts the libraries by name
func (list *List) SortByName() {
sort.Slice(*list, func(i, j int) bool {
a, b := (*list)[i], (*list)[j]
return a.Name < b.Name
})
}

/*
// HasHigherPriority returns true if library x has higher priority compared to library
// y for the given header and architecture.
Expand Down
11 changes: 7 additions & 4 deletions arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ func (resolver *Cpp) ResolveFor(header, architecture string) *libraries.Library

// If more than one library qualifies use the "closestmatch" algorithm to
// find the best matching one (instead of choosing it randomly)
winner := findLibraryWithNameBestDistance(header, found)
if winner != nil {
logrus.WithField("lib", winner.Name).Info(" library with the best mathing name")
if best := findLibraryWithNameBestDistance(header, found); best != nil {
logrus.WithField("lib", best.Name).Info(" library with the best mathing name")
return best
}
return winner

found.SortByName()
logrus.WithField("lib", found[0].Name).Info(" first library in alphabetic order")
return found[0]
}

func simplify(name string) string {
Expand Down
12 changes: 12 additions & 0 deletions arduino/libraries/librariesresolver/cpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var l5 = &libraries.Library{Name: "Yet Another Calculus Lib Improved", Location:
var l6 = &libraries.Library{Name: "Calculus Unified Lib", Location: libraries.Sketchbook}
var l7 = &libraries.Library{Name: "AnotherLib", Location: libraries.Sketchbook}

func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {
libraryList := libraries.List{}
libraryList.Add(l5)
libraryList.Add(l6)
libraryList.Add(l7)
resolver := NewCppResolver()
resolver.headers["XYZ.h"] = libraryList
res := resolver.ResolveFor("XYZ.h", "xyz")
require.NotNil(t, res)
require.Equal(t, l7, res, "selected library")
}

func TestCppHeaderPriority(t *testing.T) {
r1 := computePriority(l1, "calculus_lib.h", "avr")
r2 := computePriority(l2, "calculus_lib.h", "avr")
Expand Down