Skip to content

fix: outdated cmd wasn't check for real new versions #2528

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

Open
wants to merge 1 commit into
base: main
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
60 changes: 48 additions & 12 deletions internal/devbox/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ import (
"path/filepath"
"runtime/trace"
"slices"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
"github.com/samber/lo"

"go.jetify.com/devbox/internal/boxcli/usererr"
"go.jetify.com/devbox/internal/debug"
"go.jetify.com/devbox/internal/devbox/devopt"
"go.jetify.com/devbox/internal/devbox/providers/nixcache"
"go.jetify.com/devbox/internal/devconfig"
"go.jetify.com/devbox/internal/devconfig/configfile"
"go.jetify.com/devbox/internal/devpkg"
"go.jetify.com/devbox/internal/devpkg/pkgtype"
"go.jetify.com/devbox/internal/lock"
"go.jetify.com/devbox/internal/nix"
"go.jetify.com/devbox/internal/plugin"
"go.jetify.com/devbox/internal/searcher"
"go.jetify.com/devbox/internal/setup"
"go.jetify.com/devbox/internal/shellgen"
"go.jetify.com/devbox/internal/telemetry"
"go.jetify.com/devbox/internal/ux"
"go.jetify.com/devbox/nix/flake"
"go.jetify.com/pkg/auth"

"go.jetify.com/devbox/internal/boxcli/usererr"
"go.jetify.com/devbox/internal/debug"
"go.jetify.com/devbox/internal/nix"
"go.jetify.com/devbox/internal/plugin"
"go.jetify.com/devbox/internal/ux"
)

const StateOutOfDateMessage = "Your devbox environment may be out of date. Run %s to update it.\n"
Expand All @@ -60,17 +62,25 @@ func (d *Devbox) Outdated(ctx context.Context) (map[string]UpdateVersion, error)
continue
}

lockPackage, err := lockfile.FetchResolvedPackage(pkg.Versioned())
result, err := searcher.Client().Search(ctx, pkg.CanonicalName())
if err != nil {
warnings = append(warnings, fmt.Sprintf("Note: unable to check updates for %s", pkg.CanonicalName()))
continue
}
existingLockPackage := lockfile.Packages[pkg.Raw]
if lockPackage.Version == existingLockPackage.Version {
continue
}

outdatedPackages[pkg.Versioned()] = UpdateVersion{Current: existingLockPackage.Version, Latest: lockPackage.Version}
for _, p := range result.Packages {
if p.Name != pkg.CanonicalName() {
continue
}

for _, v := range p.Versions {
existingLockPackage := lockfile.Packages[pkg.Raw]
if isGreater(v.Version, existingLockPackage.Version) {
outdatedPackages[pkg.Versioned()] = UpdateVersion{Current: existingLockPackage.Version, Latest: v.Version}
break
}
}
}
}

for _, warning := range warnings {
Expand All @@ -80,6 +90,32 @@ func (d *Devbox) Outdated(ctx context.Context) (map[string]UpdateVersion, error)
return outdatedPackages, nil
}

// isGreater returns true if v1 is greater than v2
func isGreater(v1, v2 string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish it was this simple :(

Unfortunately, nix package versions don't follow any convention. Some (many) use semver, but many do not.

We have some pretty complicated logic to try to do this on the search server. I can look into open sourcing it so you can use it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know, for now this can be enough for doing this dirty job :)

parts1 := strings.Split(v1, ".")
parts2 := strings.Split(v2, ".")

maxLen := max(len(parts2), len(parts1))

for i := range maxLen {
var num1, num2 int
if i < len(parts1) {
num1, _ = strconv.Atoi(parts1[i])
}
if i < len(parts2) {
num2, _ = strconv.Atoi(parts2[i])
}

if num1 > num2 {
return true
} else if num1 < num2 {
return false
}
}

return false
}

// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this
// devbox project
func (d *Devbox) Add(ctx context.Context, pkgsNames []string, opts devopt.AddOpts) error {
Expand Down