Skip to content
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

Add suggestions when a wrong FQBN is provided (installed platforms only) #1367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/schollz/closestmatch"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
)
Expand Down Expand Up @@ -129,6 +130,22 @@ func (pm *PackageManager) FindBoardWithFQBN(fqbnIn string) (*cores.Board, error)
return board, err
}

func (pm *PackageManager) findClosestMatchFqbn(fqbn *cores.FQBN) string {
// Create closestmatch DB
wordsToTest := []string{}
name := fqbn.StringWithoutConfig()
for _, board := range pm.InstalledBoards() {
wordsToTest = append(wordsToTest, board.FQBN())
}
// Choose a set of bag sizes, more is more accurate but slower
bagSizes := []int{2}

// Create a closestmatch object and find the best matching name
cm := closestmatch.New(wordsToTest, bagSizes)
closestName := cm.Closest(name)
return closestName
}

// ResolveFQBN returns, in order:
//
// - the Package pointed by the fqbn
Expand Down Expand Up @@ -174,8 +191,10 @@ func (pm *PackageManager) ResolveFQBN(fqbn *cores.FQBN) (
// Find board
board := platformRelease.Boards[fqbn.BoardID]
if board == nil {
// Try looking for the closest match; if found, suggest it
suggestion := pm.findClosestMatchFqbn(fqbn)
return targetPackage, platformRelease, nil, nil, nil,
fmt.Errorf("board %s:%s not found", platformRelease, fqbn.BoardID)
fmt.Errorf("board %s not found, did you mean %s ?", fqbn.StringWithoutConfig(), suggestion)
}

buildProperties, err := board.GetBuildProperties(fqbn.Configs)
Expand Down