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

[skip-changelog] Check if a dependency matches the constraint when --no-overwrite flag is set #2331

Closed
Closed
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
13 changes: 13 additions & 0 deletions commands/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
)

// LibraryInstall resolves the library dependencies, then downloads and installs the libraries into the install location.
Expand Down Expand Up @@ -88,6 +89,9 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
}

if req.GetNoOverwrite() {
if lib.GetVersionConstraint() != "" && verifyConstraint(lib.GetVersionInstalled(), lib.GetVersionConstraint()) {
continue
}
if installTask.ReplacedLib != nil {
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", libRelease, installTask.ReplacedLib))
}
Expand Down Expand Up @@ -160,3 +164,12 @@ func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallRequest, t
taskCB(&rpc.TaskProgress{Message: tr("Library installed"), Completed: true})
return nil
}

func verifyConstraint(installed, constraint string) bool {
s, err := semver.ParseConstraint(constraint)
if err != nil {
return false
}
res := s.Match(semver.MustParse(installed))
return res
}
18 changes: 16 additions & 2 deletions commands/lib/resolve_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,32 @@ func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDepe
return nil, &arduino.LibraryDependenciesResolutionFailedError{}
}

constraints := map[string]string{}
// Map the dependencies' constraints of the library to install
for _, dep := range deps {
if dep.GetName() == req.GetName() {
for _, d := range dep.GetDependencies() {
constraints[d.GetName()] = d.GetConstraint().String()
}
}
}

res := []*rpc.LibraryDependencyStatus{}
for _, dep := range deps {
// ...and add information on currently installed versions of the libraries
installed := ""
if installedLib, has := installedLibs[dep.GetName()]; has {
installed = installedLib.Version.String()
}
res = append(res, &rpc.LibraryDependencyStatus{
depStatus := &rpc.LibraryDependencyStatus{
Name: dep.GetName(),
VersionRequired: dep.GetVersion().String(),
VersionInstalled: installed,
})
}
if constraint, has := constraints[dep.GetName()]; has {
depStatus.VersionConstraint = constraint
}
res = append(res, depStatus)
}
sort.Slice(res, func(i, j int) bool {
return res[i].Name < res[j].Name
Expand Down
7 changes: 7 additions & 0 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ func TestInstallLibraryWithDependencies(t *testing.T) {
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "Arduino_Builtin", "--no-overwrite")
require.Error(t, err)

// Install a library with an outdated dependency using --no-overwrite
// It should not fail because the version of the dependency matches the constraint
_, _, err = cli.Run("lib", "install", "[email protected]")
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "[email protected]", "--no-overwrite")
require.NoError(t, err)
}

func TestInstallNoDeps(t *testing.T) {
Expand Down
Loading