Skip to content

Commit db9134f

Browse files
authored
bug fix: FillNarInfoCache for versioned packages when adding package (#1437)
## Summary The call to `versionedPkg.ValidateExists()` would error because the NarInfoCache was not pre-filled before we called `IsInBinaryCache` in it. Now, we call FillNarInfoCache on the versioned packages as well. Did some minor refactoring so that we only do FillNarInfoCache for packages that are to-be-added (thereby omitting existing packages) to be just a bit efficient. ## How was it tested? ``` export DEVBOX_FEATURE_REMOVE_NIXPKGS=1 devbox add vim # BEFORE: this would add `vim` to devbox.json # AFTER: this adds `vim@latest` to devbox.json ```
1 parent e44103a commit db9134f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

internal/impl/packages.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,28 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
5353
// to know the exact name to mark as allowed insecure later on.
5454
addedPackageNames := []string{}
5555
existingPackageNames := d.PackageNames()
56+
newPackages := []*devpkg.Package{}
5657
for _, pkg := range pkgs {
57-
// If exact versioned package is already in the config, skip.
58+
// If exact versioned package is already in the config, we can skip the
59+
// next loop that only deals with newPackages.
5860
if slices.Contains(existingPackageNames, pkg.Versioned()) {
61+
// But we still need to add to addedPackageNames. See its comment.
5962
addedPackageNames = append(addedPackageNames, pkg.Versioned())
60-
continue
63+
} else {
64+
newPackages = append(newPackages, pkg)
6165
}
66+
}
67+
68+
// Fill in the narinfo cache for versioned newPackages as well
69+
versionedPackages := map[string]*devpkg.Package{}
70+
for _, pkg := range newPackages {
71+
versionedPackages[pkg.Versioned()] = devpkg.PackageFromString(pkg.Versioned(), d.lockfile)
72+
}
73+
if err := devpkg.FillNarInfoCache(ctx, lo.Values(versionedPackages)...); err != nil {
74+
return err
75+
}
76+
77+
for _, pkg := range newPackages {
6278

6379
// On the other hand, if there's a package with same canonical name, replace
6480
// it. Ignore error (which is either missing or more than one). We search by
@@ -73,7 +89,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
7389

7490
// validate that the versioned package exists in the search endpoint.
7591
// if not, fallback to legacy vanilla nix.
76-
versionedPkg := devpkg.PackageFromString(pkg.Versioned(), d.lockfile)
92+
versionedPkg := versionedPackages[pkg.Versioned()]
7793

7894
packageNameForConfig := pkg.Raw
7995
ok, err := versionedPkg.ValidateExists()

0 commit comments

Comments
 (0)