Skip to content

Commit 2ab18ae

Browse files
committed
[perf] skip forcing printDevEnv when add/rm/update outside shellenv
1 parent b2a8835 commit 2ab18ae

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

internal/impl/devbox.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (d *Devbox) Shell(ctx context.Context) error {
168168
ctx, task := trace.NewTask(ctx, "devboxShell")
169169
defer task.End()
170170

171-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
171+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
172172
return err
173173
}
174174
fmt.Fprintln(d.stderr, "Starting a devbox shell...")
@@ -210,7 +210,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
210210
ctx, task := trace.NewTask(ctx, "devboxRun")
211211
defer task.End()
212212

213-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
213+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
214214
return err
215215
}
216216

@@ -260,7 +260,7 @@ func (d *Devbox) Install(ctx context.Context) error {
260260
ctx, task := trace.NewTask(ctx, "devboxInstall")
261261
defer task.End()
262262

263-
return d.ensurePackagesAreInstalled(ctx, ensure)
263+
return d.ensureDevboxEnvIsUpToDate(ctx, ensure)
264264
}
265265

266266
func (d *Devbox) ListScripts() []string {
@@ -278,7 +278,7 @@ func (d *Devbox) NixEnv(ctx context.Context, includeHooks bool) (string, error)
278278
ctx, task := trace.NewTask(ctx, "devboxNixEnv")
279279
defer task.End()
280280

281-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
281+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
282282
return "", err
283283
}
284284

@@ -302,7 +302,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
302302
defer task.End()
303303
// this only returns env variables for the shell environment excluding hooks
304304
// and excluding "export " prefix in "export key=value" format
305-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
305+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
306306
return nil, err
307307
}
308308

@@ -466,7 +466,7 @@ func (d *Devbox) GenerateEnvrcFile(ctx context.Context, force bool, envFlags dev
466466
}
467467

468468
// generate all shell files to ensure we can refer to them in the .envrc script
469-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
469+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
470470
return err
471471
}
472472

@@ -1156,10 +1156,10 @@ func (d *Devbox) parseEnvAndExcludeSpecialCases(currentEnv []string) (map[string
11561156
if ignoreCurrentEnvVar[key] {
11571157
continue
11581158
}
1159-
// handling special cases to for pure shell
1160-
// Passing HOME for pure shell to leak through otherwise devbox binary won't work
1161-
// We also include PATH to find the nix installation. It is cleaned for pure mode below
1162-
// TERM leaking through is to enable colored text in the pure shell
1159+
// handling special cases for pure shell
1160+
// - HOME required for devbox binary to work
1161+
// - PATH to find the nix installation. It is cleaned for pure mode below.
1162+
// - TERM to enable colored text in the pure shell
11631163
if !d.pure || key == "HOME" || key == "PATH" || key == "TERM" {
11641164
env[key] = val
11651165
}

internal/impl/envvars.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func markEnvsAsSetByDevbox(envs ...map[string]string) {
6969
}
7070
}
7171

72-
// IsEnvEnabled checks if the devbox environment is enabled. We use the ogPathKey
73-
// as a proxy for this. This allows us to differentiate between global and
72+
// IsEnvEnabled checks if the devbox environment is enabled.
73+
// This allows us to differentiate between global and
7474
// individual project shells.
7575
func (d *Devbox) IsEnvEnabled() bool {
7676
fakeEnv := map[string]string{}

internal/impl/packages.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
108108
}
109109
}
110110

111-
// Resolving here ensures we allow insecure before running ensurePackagesAreInstalled
111+
// Resolving here ensures we allow insecure before running ensureDevboxEnvIsUpToDate
112112
// which will call print-dev-env. Resolving does not save the lockfile, we
113113
// save at the end when everything has succeeded.
114114
if d.allowInsecureAdds {
@@ -126,7 +126,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
126126
}
127127
}
128128

129-
if err := d.ensurePackagesAreInstalled(ctx, install); err != nil {
129+
if err := d.ensureDevboxEnvIsUpToDate(ctx, install); err != nil {
130130
return usererr.WithUserMessage(err, "There was an error installing nix packages")
131131
}
132132

@@ -190,28 +190,35 @@ func (d *Devbox) Remove(ctx context.Context, pkgs ...string) error {
190190
}
191191

192192
// 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 {
194194
return err
195195
}
196196

197197
return d.saveCfg()
198198
}
199199

200-
// installMode is an enum for helping with ensurePackagesAreInstalled implementation
200+
// installMode is an enum for helping with ensureDevboxEnvIsUpToDate implementation
201201
type installMode string
202202

203203
const (
204204
install installMode = "install"
205205
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"
207209
)
208210

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 {
215222
defer trace.StartRegion(ctx, "ensurePackages").End()
216223
defer debug.FunctionTimer().End()
217224

@@ -248,9 +255,15 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
248255
return err
249256
}
250257

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+
252265
// Force print-dev-env cache to be recomputed.
253-
nixEnv, err := d.computeNixEnv(ctx, false /*use cache*/)
266+
nixEnv, err := d.computeNixEnv(ctx, usePrintDevEnvCache)
254267
if err != nil {
255268
return err
256269
}

internal/impl/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error {
6262
}
6363
}
6464

65-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
65+
if err := d.ensureDevboxEnvIsUpToDate(ctx, update); err != nil {
6666
return err
6767
}
6868

0 commit comments

Comments
 (0)