@@ -108,7 +108,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
108
108
}
109
109
}
110
110
111
- // Resolving here ensures we allow insecure before running ensurePackagesAreInstalled
111
+ // Resolving here ensures we allow insecure before running ensureDevboxEnvIsUpToDate
112
112
// which will call print-dev-env. Resolving does not save the lockfile, we
113
113
// save at the end when everything has succeeded.
114
114
if d .allowInsecureAdds {
@@ -126,7 +126,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
126
126
}
127
127
}
128
128
129
- if err := d .ensurePackagesAreInstalled (ctx , install ); err != nil {
129
+ if err := d .ensureDevboxEnvIsUpToDate (ctx , install ); err != nil {
130
130
return usererr .WithUserMessage (err , "There was an error installing nix packages" )
131
131
}
132
132
@@ -190,28 +190,35 @@ func (d *Devbox) Remove(ctx context.Context, pkgs ...string) error {
190
190
}
191
191
192
192
// this will clean up the now-extra package from nix profile and the lockfile
193
- if err := d .ensurePackagesAreInstalled (ctx , uninstall ); err != nil {
193
+ if err := d .ensureDevboxEnvIsUpToDate (ctx , uninstall ); err != nil {
194
194
return err
195
195
}
196
196
197
197
return d .saveCfg ()
198
198
}
199
199
200
- // installMode is an enum for helping with ensurePackagesAreInstalled implementation
200
+ // installMode is an enum for helping with ensureDevboxEnvIsUpToDate implementation
201
201
type installMode string
202
202
203
203
const (
204
204
install installMode = "install"
205
205
uninstall installMode = "uninstall"
206
- ensure installMode = "ensure"
206
+ // update is both install new package version and uninstall old package version
207
+ update installMode = "update"
208
+ ensure installMode = "ensure"
207
209
)
208
210
209
- // ensurePackagesAreInstalled ensures that the nix profile has the packages specified
210
- // in the config (devbox.json). The `mode` is used for user messaging to explain
211
- // what operations are happening, because this function may take time to execute.
212
- // TODO we should rename this to ensureDevboxEnvironmentIsUpToDate since it does
213
- // much more than ensuring packages are installed.
214
- func (d * Devbox ) ensurePackagesAreInstalled (ctx context.Context , mode installMode ) error {
211
+ // ensureDevboxEnvIsUpToDate ensures:
212
+ // 1. Packages are installed, in nix-profile or runx.
213
+ // Extraneous packages are removed (references purged, not uninstalled).
214
+ // 2. Files for devbox shellenv are generated
215
+ // 3. Env-vars for shellenv are computed
216
+ // 4. Lockfile is synced
217
+ //
218
+ // The `mode` is used for:
219
+ // 1. Skipping certain operations that may not apply.
220
+ // 2. User messaging to explain what operations are happening, because this function may take time to execute.
221
+ func (d * Devbox ) ensureDevboxEnvIsUpToDate (ctx context.Context , mode installMode ) error {
215
222
defer trace .StartRegion (ctx , "ensurePackages" ).End ()
216
223
defer debug .FunctionTimer ().End ()
217
224
@@ -248,9 +255,15 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
248
255
return err
249
256
}
250
257
251
- fmt .Fprintf (d .stderr , "Recomputing the devbox environment.\n " )
258
+ // We can use the print-dev-env cache if we are adding or removing or updating any package,
259
+ // AND we are not in the shellenv-enabled environment of the current devbox-project.
260
+ usePrintDevEnvCache := mode != ensure && ! d .IsEnvEnabled ()
261
+ if ! usePrintDevEnvCache {
262
+ fmt .Fprintf (d .stderr , "Recomputing the devbox environment.\n " )
263
+ }
264
+
252
265
// Force print-dev-env cache to be recomputed.
253
- nixEnv , err := d .computeNixEnv (ctx , false /*use cache*/ )
266
+ nixEnv , err := d .computeNixEnv (ctx , usePrintDevEnvCache )
254
267
if err != nil {
255
268
return err
256
269
}
0 commit comments