/
update.go
214 lines (188 loc) · 6.05 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package devbox
import (
"context"
"fmt"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/featureflag"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/lock"
"go.jetpack.io/devbox/internal/nix"
"go.jetpack.io/devbox/internal/nix/nixprofile"
"go.jetpack.io/devbox/internal/plugin"
"go.jetpack.io/devbox/internal/searcher"
"go.jetpack.io/devbox/internal/shellgen"
"go.jetpack.io/devbox/internal/ux"
)
func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error {
inputs, err := d.inputsToUpdate(opts)
if err != nil {
return err
}
pendingPackagesToUpdate := []*devpkg.Package{}
for _, pkg := range inputs {
if pkg.IsLegacy() {
fmt.Fprintf(d.stderr, "Updating %s -> %s\n", pkg.Raw, pkg.LegacyToVersioned())
// Get the package from the config to get the Platforms and ExcludedPlatforms later
cfgPackage, ok := d.cfg.Root.GetPackage(pkg.Raw)
if !ok {
return fmt.Errorf("package %s not found in config", pkg.Raw)
}
if err := d.Remove(ctx, pkg.Raw); err != nil {
return err
}
// Calling Add function with the original package names, since
// Add will automatically append @latest if search is able to handle that.
// If not, it will fallback to the nixpkg format.
if err := d.Add(ctx, []string{pkg.Raw}, devopt.AddOpts{
Platforms: cfgPackage.Platforms,
ExcludePlatforms: cfgPackage.ExcludedPlatforms,
}); err != nil {
return err
}
} else {
pendingPackagesToUpdate = append(pendingPackagesToUpdate, pkg)
}
}
for _, pkg := range pendingPackagesToUpdate {
if _, _, isVersioned := searcher.ParseVersionedPackage(pkg.Raw); !isVersioned {
if err = d.attemptToUpgradeFlake(pkg); err != nil {
return err
}
} else {
if err = d.updateDevboxPackage(pkg); err != nil {
return err
}
}
}
if err := d.ensureStateIsUpToDate(ctx, update); err != nil {
return err
}
// I'm not entirely sure this is even needed, so ignoring the error.
// It's definitely not needed for non-flakes. (which is 99.9% of packages)
// It will return an error if .devbox/gen/flake is missing
// TODO: Remove this if it's not needed.
_ = nix.FlakeUpdate(shellgen.FlakePath(d))
return plugin.Update()
}
func (d *Devbox) inputsToUpdate(
opts devopt.UpdateOpts,
) ([]*devpkg.Package, error) {
if len(opts.Pkgs) == 0 {
return d.AllPackages(), nil
}
var pkgsToUpdate []*devpkg.Package
for _, pkg := range opts.Pkgs {
found, err := d.findPackageByName(pkg)
if opts.IgnoreMissingPackages && errors.Is(err, searcher.ErrNotFound) {
continue
} else if err != nil {
return nil, err
}
pkgsToUpdate = append(pkgsToUpdate, found)
}
return pkgsToUpdate, nil
}
func (d *Devbox) updateDevboxPackage(pkg *devpkg.Package) error {
resolved, err := d.lockfile.FetchResolvedPackage(pkg.Raw)
if err != nil {
return err
}
if resolved == nil {
return nil
}
return d.mergeResolvedPackageToLockfile(pkg, resolved, d.lockfile)
}
func (d *Devbox) mergeResolvedPackageToLockfile(
pkg *devpkg.Package,
resolved *lock.Package,
lockfile *lock.File,
) error {
existing := lockfile.Packages[pkg.Raw]
if existing == nil {
ux.Finfo(d.stderr, "Resolved %s to %[1]s %[2]s\n", pkg, resolved.Resolved)
lockfile.Packages[pkg.Raw] = resolved
return nil
}
if existing.Version != resolved.Version {
if existing.LastModified > resolved.LastModified {
ux.Fwarning(
d.stderr,
"Resolved version for %s has older last_modified time. Not updating\n",
pkg,
)
return nil
}
ux.Finfo(d.stderr, "Updating %s %s -> %s\n", pkg, existing.Version, resolved.Version)
useResolvedPackageInLockfile(lockfile, pkg, resolved, existing)
return nil
}
// Add any missing system infos for packages whose versions did not change.
if featureflag.RemoveNixpkgs.Enabled() {
if lockfile.Packages[pkg.Raw].Systems == nil {
lockfile.Packages[pkg.Raw].Systems = map[string]*lock.SystemInfo{}
}
userSystem := nix.System()
updated := false
for sysName, newSysInfo := range resolved.Systems {
// Check whether we are actually updating any system info.
if sysName == userSystem {
// The resolved pkg has a system info for the user's system, so add/overwrite it.
if !newSysInfo.Equals(existing.Systems[userSystem]) {
// We only guard this so that the ux messaging is accurate. We could overwrite every time.
updated = true
}
} else {
// Add other system infos if they don't exist, or if we have a different StorePath. This may
// overwrite an existing StorePath, but to ensure correctness we should ensure that all StorePaths
// come from the same package version.
existingSysInfo, exists := existing.Systems[sysName]
if !exists || !existingSysInfo.Equals(newSysInfo) {
updated = true
}
}
}
if updated {
// if we are updating the system info, then we should also update the other fields
useResolvedPackageInLockfile(lockfile, pkg, resolved, existing)
ux.Finfo(d.stderr, "Updated system information for %s\n", pkg)
return nil
}
}
ux.Finfo(d.stderr, "Already up-to-date %s %s\n", pkg, existing.Version)
return nil
}
// attemptToUpgradeFlake attempts to upgrade a flake using `nix profile upgrade`
// and prints an error if it fails, but does not propagate upgrade errors.
func (d *Devbox) attemptToUpgradeFlake(pkg *devpkg.Package) error {
profilePath, err := d.profilePath()
if err != nil {
return err
}
ux.Finfo(
d.stderr,
"Attempting to upgrade %s using `nix profile upgrade`\n",
pkg.Raw,
)
err = nixprofile.ProfileUpgrade(profilePath, pkg, d.lockfile)
if err != nil {
ux.Fwarning(
d.stderr,
"Failed to upgrade %s using `nix profile upgrade`: %s\n",
pkg.Raw,
err,
)
}
return nil
}
func useResolvedPackageInLockfile(
lockfile *lock.File,
pkg *devpkg.Package,
resolved *lock.Package,
existing *lock.Package,
) {
lockfile.Packages[pkg.Raw] = resolved
lockfile.Packages[pkg.Raw].AllowInsecure = existing.AllowInsecure
}