Skip to content

Commit 02b4ab8

Browse files
committed
[per-OS Packages] fix devbox update for unversioned packages with platform/excluded-platform
1 parent fc76ec0 commit 02b4ab8

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

internal/devconfig/packages.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func (pkgs *Packages) VersionedNames() []string {
4444
return result
4545
}
4646

47+
// Get returns the package with the given versionedName
48+
func (pkgs *Packages) Get(versionedName string) (*Package, bool) {
49+
name, version := parseVersionedName(versionedName)
50+
for _, pkg := range pkgs.Collection {
51+
if pkg.name == name && pkg.Version == version {
52+
return &pkg, true
53+
}
54+
}
55+
return nil, false
56+
}
57+
4758
// Add adds a package to the list of packages
4859
func (pkgs *Packages) Add(versionedName string) {
4960
name, version := parseVersionedName(versionedName)

internal/devpkg/package.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ func (p *Package) normalizePackageAttributePath() (string, error) {
341341
}
342342

343343
if nix.PkgExistsForAnySystem(query) {
344-
return "", usererr.New(
344+
return "", usererr.WithUserMessage(
345+
ErrCannotBuildPackageOnSystem,
345346
"Package \"%s\" was found, but we're unable to build it for your system."+
346347
" You may need to choose another version or write a custom flake.",
347348
p.String(),
@@ -351,6 +352,8 @@ func (p *Package) normalizePackageAttributePath() (string, error) {
351352
return "", usererr.New("Package \"%s\" was not found", p.String())
352353
}
353354

355+
var ErrCannotBuildPackageOnSystem = errors.New("unable to build for system")
356+
354357
func (p *Package) urlWithoutFragment() string {
355358
u := p.URL // get copy
356359
u.Fragment = ""

internal/impl/packages.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
7070
versionedPkg := devpkg.PackageFromString(pkg.Versioned(), d.lockfile)
7171

7272
packageNameForConfig := pkg.Raw
73-
if ok, err := versionedPkg.ValidateExists(); err == nil && ok {
74-
// Only use versioned if it exists in search.
73+
ok, err := versionedPkg.ValidateExists()
74+
if (err == nil && ok) || errors.Is(err, devpkg.ErrCannotBuildPackageOnSystem) {
75+
// Only use versioned if it exists in search. We can disregard the error
76+
// about not building on the current system, since user's can continue
77+
// via --exclude-platform flag.
7578
packageNameForConfig = pkg.Versioned()
7679
} else if !versionedPkg.IsDevboxPackage() {
7780
// This means it didn't validate and we don't want to fallback to legacy

internal/impl/update.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ func (d *Devbox) Update(ctx context.Context, pkgs ...string) error {
2828
for _, pkg := range inputs {
2929
if pkg.IsLegacy() {
3030
fmt.Fprintf(d.writer, "Updating %s -> %s\n", pkg.Raw, pkg.LegacyToVersioned())
31+
32+
// Get the package from the config to get the Platforms and ExcludedPlatforms later
33+
cfgPackage, ok := d.cfg.Packages.Get(pkg.Raw)
34+
if !ok {
35+
return fmt.Errorf("package %s not found in config", pkg.Raw)
36+
}
37+
3138
if err := d.Remove(ctx, pkg.Raw); err != nil {
3239
return err
3340
}
3441
// Calling Add function with the original package names, since
3542
// Add will automatically append @latest if search is able to handle that.
3643
// If not, it will fallback to the nixpkg format.
37-
if err := d.Add(ctx, nil /*platforms*/, nil /*excludePlatforms*/, pkg.Raw); err != nil {
44+
if err := d.Add(ctx, cfgPackage.Platforms, cfgPackage.ExcludedPlatforms, pkg.Raw); err != nil {
3845
return err
3946
}
4047
} else {

0 commit comments

Comments
 (0)