11package impl
22
33import (
4+ "context"
45 "fmt"
56 "os"
67 "os/exec"
78 "path/filepath"
9+ "runtime/trace"
810 "sort"
911 "strings"
1012
@@ -24,6 +26,9 @@ import (
2426
2527// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this devbox project
2628func (d * Devbox ) Add (pkgs ... string ) error {
29+ ctx , task := trace .NewTask (context .Background (), "devboxAdd" )
30+ defer task .End ()
31+
2732 original := d .cfg .RawPackages
2833 // Check packages are valid before adding.
2934 for _ , pkg := range pkgs {
@@ -45,7 +50,7 @@ func (d *Devbox) Add(pkgs ...string) error {
4550 }
4651
4752 d .pluginManager .ApplyOptions (plugin .WithAddMode ())
48- if err := d .ensurePackagesAreInstalled (install ); err != nil {
53+ if err := d .ensurePackagesAreInstalled (ctx , install ); err != nil {
4954 // if error installing, revert devbox.json
5055 // This is not perfect because there may be more than 1 package being
5156 // 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 {
7782
7883// Remove removes the `pkgs` from the config (i.e. devbox.json) and nix profile for this devbox project
7984func (d * Devbox ) Remove (pkgs ... string ) error {
85+ ctx , task := trace .NewTask (context .Background (), "devboxRemove" )
86+ defer task .End ()
8087
8188 // First, save which packages are being uninstalled. Do this before we modify d.cfg.RawPackages below.
8289 uninstalledPackages := lo .Intersect (d .cfg .RawPackages , pkgs )
@@ -99,11 +106,11 @@ func (d *Devbox) Remove(pkgs ...string) error {
99106 return err
100107 }
101108
102- if err := d .removePackagesFromProfile (uninstalledPackages ); err != nil {
109+ if err := d .removePackagesFromProfile (ctx , uninstalledPackages ); err != nil {
103110 return err
104111 }
105112
106- if err := d .ensurePackagesAreInstalled (uninstall ); err != nil {
113+ if err := d .ensurePackagesAreInstalled (ctx , uninstall ); err != nil {
107114 return err
108115 }
109116
@@ -122,7 +129,9 @@ const (
122129// ensurePackagesAreInstalled ensures that the nix profile has the packages specified
123130// in the config (devbox.json). The `mode` is used for user messaging to explain
124131// 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+
126135 if err := d .generateShellFiles (); err != nil {
127136 return err
128137 }
@@ -131,7 +140,7 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
131140 }
132141
133142 if featureflag .Flakes .Enabled () {
134- if err := d .addPackagesToProfile (mode ); err != nil {
143+ if err := d .addPackagesToProfile (ctx , mode ); err != nil {
135144 return err
136145 }
137146
@@ -145,7 +154,7 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
145154 }
146155
147156 // We need to re-install the packages
148- if err := d .installNixProfile (); err != nil {
157+ if err := d .installNixProfile (ctx ); err != nil {
149158 fmt .Fprintln (d .writer )
150159 return errors .Wrap (err , "apply Nix derivation" )
151160 }
@@ -206,7 +215,9 @@ func (d *Devbox) printPackageUpdateMessage(
206215
207216// installNixProfile installs or uninstalls packages to or from this
208217// 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+
210221 profileDir , err := d .profilePath ()
211222 if err != nil {
212223 return err
@@ -255,15 +266,17 @@ func (d *Devbox) profilePath() (string, error) {
255266// addPackagesToProfile inspects the packages in devbox.json, checks which of them
256267// are missing from the nix profile, and then installs each package individually into the
257268// 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+
259272 if featureflag .Flakes .Disabled () {
260273 return nil
261274 }
262275 if mode == uninstall {
263276 return nil
264277 }
265278
266- pkgs , err := d .pendingPackagesForInstallation ()
279+ pkgs , err := d .pendingPackagesForInstallation (ctx )
267280 if err != nil {
268281 return err
269282 }
@@ -317,7 +330,9 @@ func (d *Devbox) addPackagesToProfile(mode installMode) error {
317330 return nil
318331}
319332
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+
321336 if ! featureflag .Flakes .Enabled () {
322337 return nil
323338 }
@@ -367,7 +382,9 @@ func (d *Devbox) removePackagesFromProfile(pkgs []string) error {
367382 return nil
368383}
369384
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+
371388 if featureflag .Flakes .Disabled () {
372389 return nil , errors .New ("Not implemented for legacy non-flakes devbox" )
373390 }
0 commit comments