Skip to content

Commit 265f100

Browse files
committed
[perf] skip forcing printDevEnv when add/rm/update outside shellenv
1 parent 35ed72e commit 265f100

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

internal/impl/devbox.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (d *Devbox) Install(ctx context.Context) error {
259259
ctx, task := trace.NewTask(ctx, "devboxInstall")
260260
defer task.End()
261261

262-
return d.ensurePackagesAreInstalled(ctx, ensure)
262+
return d.ensureDevboxEnvIsUpToDate(ctx, ensure)
263263
}
264264

265265
func (d *Devbox) ListScripts() []string {
@@ -477,7 +477,7 @@ func (d *Devbox) GenerateEnvrcFile(ctx context.Context, force bool, envFlags dev
477477
}
478478

479479
// generate all shell files to ensure we can refer to them in the .envrc script
480-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
480+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
481481
return err
482482
}
483483

@@ -931,9 +931,9 @@ func (d *Devbox) ensurePackagesAreInstalledAndComputeEnv(
931931
) (map[string]string, error) {
932932
defer debug.FunctionTimer().End()
933933

934-
// When ensurePackagesAreInstalled is called with ensure=true, it always
934+
// When ensureDevboxEnvIsUpToDate is called with ensure=true, it always
935935
// returns early if the lockfile is up to date. So we don't need to check here
936-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
936+
if err := d.ensureDevboxEnvIsUpToDate(ctx, ensure); err != nil {
937937
return nil, err
938938
}
939939

@@ -1172,10 +1172,10 @@ func (d *Devbox) parseEnvAndExcludeSpecialCases(currentEnv []string) (map[string
11721172
if ignoreCurrentEnvVar[key] {
11731173
continue
11741174
}
1175-
// handling special cases to for pure shell
1176-
// Passing HOME for pure shell to leak through otherwise devbox binary won't work
1177-
// We also include PATH to find the nix installation. It is cleaned for pure mode below
1178-
// TERM leaking through is to enable colored text in the pure shell
1175+
// handling special cases for pure shell
1176+
// - HOME required for devbox binary to work
1177+
// - PATH to find the nix installation. It is cleaned for pure mode below.
1178+
// - TERM to enable colored text in the pure shell
11791179
if !d.pure || key == "HOME" || key == "PATH" || key == "TERM" {
11801180
env[key] = val
11811181
}

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: 22 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,8 +255,10 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
248255
return err
249256
}
250257

251-
// Force print-dev-env cache to be recomputed.
252-
nixEnv, err := d.computeNixEnv(ctx, false /*use cache*/)
258+
// Use the printDevEnvCache 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+
nixEnv, err := d.computeNixEnv(ctx, usePrintDevEnvCache)
253262
if err != nil {
254263
return err
255264
}

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)