1
1
package impl
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"os"
6
7
"os/exec"
7
8
"path/filepath"
9
+ "runtime/trace"
8
10
"sort"
9
11
"strings"
10
12
@@ -24,6 +26,9 @@ import (
24
26
25
27
// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this devbox project
26
28
func (d * Devbox ) Add (pkgs ... string ) error {
29
+ ctx , task := trace .NewTask (context .Background (), "devboxAdd" )
30
+ defer task .End ()
31
+
27
32
original := d .cfg .RawPackages
28
33
// Check packages are valid before adding.
29
34
for _ , pkg := range pkgs {
@@ -45,7 +50,7 @@ func (d *Devbox) Add(pkgs ...string) error {
45
50
}
46
51
47
52
d .pluginManager .ApplyOptions (plugin .WithAddMode ())
48
- if err := d .ensurePackagesAreInstalled (install ); err != nil {
53
+ if err := d .ensurePackagesAreInstalled (ctx , install ); err != nil {
49
54
// if error installing, revert devbox.json
50
55
// This is not perfect because there may be more than 1 package being
51
56
// installed and we don't know which one failed. But it's better than
@@ -77,6 +82,8 @@ func (d *Devbox) Add(pkgs ...string) error {
77
82
78
83
// Remove removes the `pkgs` from the config (i.e. devbox.json) and nix profile for this devbox project
79
84
func (d * Devbox ) Remove (pkgs ... string ) error {
85
+ ctx , task := trace .NewTask (context .Background (), "devboxRemove" )
86
+ defer task .End ()
80
87
81
88
// First, save which packages are being uninstalled. Do this before we modify d.cfg.RawPackages below.
82
89
uninstalledPackages := lo .Intersect (d .cfg .RawPackages , pkgs )
@@ -99,11 +106,11 @@ func (d *Devbox) Remove(pkgs ...string) error {
99
106
return err
100
107
}
101
108
102
- if err := d .removePackagesFromProfile (uninstalledPackages ); err != nil {
109
+ if err := d .removePackagesFromProfile (ctx , uninstalledPackages ); err != nil {
103
110
return err
104
111
}
105
112
106
- if err := d .ensurePackagesAreInstalled (uninstall ); err != nil {
113
+ if err := d .ensurePackagesAreInstalled (ctx , uninstall ); err != nil {
107
114
return err
108
115
}
109
116
@@ -122,7 +129,9 @@ const (
122
129
// ensurePackagesAreInstalled ensures that the nix profile has the packages specified
123
130
// in the config (devbox.json). The `mode` is used for user messaging to explain
124
131
// what operations are happening, because this function may take time to execute.
125
- func (d * Devbox ) ensurePackagesAreInstalled (mode installMode ) error {
132
+ func (d * Devbox ) ensurePackagesAreInstalled (ctx context.Context , mode installMode ) error {
133
+ defer trace .StartRegion (ctx , "ensurePackages" ).End ()
134
+
126
135
if err := d .generateShellFiles (); err != nil {
127
136
return err
128
137
}
@@ -131,7 +140,7 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
131
140
}
132
141
133
142
if featureflag .Flakes .Enabled () {
134
- if err := d .addPackagesToProfile (mode ); err != nil {
143
+ if err := d .addPackagesToProfile (ctx , mode ); err != nil {
135
144
return err
136
145
}
137
146
@@ -145,7 +154,7 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
145
154
}
146
155
147
156
// We need to re-install the packages
148
- if err := d .installNixProfile (); err != nil {
157
+ if err := d .installNixProfile (ctx ); err != nil {
149
158
fmt .Fprintln (d .writer )
150
159
return errors .Wrap (err , "apply Nix derivation" )
151
160
}
@@ -206,7 +215,9 @@ func (d *Devbox) printPackageUpdateMessage(
206
215
207
216
// installNixProfile installs or uninstalls packages to or from this
208
217
// devbox's Nix profile so that it matches what's in development.nix
209
- func (d * Devbox ) installNixProfile () (err error ) {
218
+ func (d * Devbox ) installNixProfile (ctx context.Context ) (err error ) {
219
+ defer trace .StartRegion (ctx , "installNixProfile" ).End ()
220
+
210
221
profileDir , err := d .profilePath ()
211
222
if err != nil {
212
223
return err
@@ -255,15 +266,17 @@ func (d *Devbox) profilePath() (string, error) {
255
266
// addPackagesToProfile inspects the packages in devbox.json, checks which of them
256
267
// are missing from the nix profile, and then installs each package individually into the
257
268
// nix profile.
258
- func (d * Devbox ) addPackagesToProfile (mode installMode ) error {
269
+ func (d * Devbox ) addPackagesToProfile (ctx context.Context , mode installMode ) error {
270
+ defer trace .StartRegion (ctx , "addNixProfilePkgs" ).End ()
271
+
259
272
if featureflag .Flakes .Disabled () {
260
273
return nil
261
274
}
262
275
if mode == uninstall {
263
276
return nil
264
277
}
265
278
266
- pkgs , err := d .pendingPackagesForInstallation ()
279
+ pkgs , err := d .pendingPackagesForInstallation (ctx )
267
280
if err != nil {
268
281
return err
269
282
}
@@ -317,7 +330,9 @@ func (d *Devbox) addPackagesToProfile(mode installMode) error {
317
330
return nil
318
331
}
319
332
320
- func (d * Devbox ) removePackagesFromProfile (pkgs []string ) error {
333
+ func (d * Devbox ) removePackagesFromProfile (ctx context.Context , pkgs []string ) error {
334
+ defer trace .StartRegion (ctx , "removeNixProfilePkgs" ).End ()
335
+
321
336
if ! featureflag .Flakes .Enabled () {
322
337
return nil
323
338
}
@@ -367,7 +382,9 @@ func (d *Devbox) removePackagesFromProfile(pkgs []string) error {
367
382
return nil
368
383
}
369
384
370
- func (d * Devbox ) pendingPackagesForInstallation () ([]string , error ) {
385
+ func (d * Devbox ) pendingPackagesForInstallation (ctx context.Context ) ([]string , error ) {
386
+ defer trace .StartRegion (ctx , "pendingPackages" ).End ()
387
+
371
388
if featureflag .Flakes .Disabled () {
372
389
return nil , errors .New ("Not implemented for legacy non-flakes devbox" )
373
390
}
0 commit comments