Skip to content

Commit b6d0b20

Browse files
authored
[perf] skip forcing printDevEnv when add/rm/update outside shellenv (#1540)
## Summary In this PR, we skip forcing `computeNixEnv` if a user is adding/removing/updating packages, AND if we are not in a shellenv-enabled environment from that devbox project. Also: - ~rename `ensurePackagesAreInstalled` to `ensureDevboxEnvIsUpToDate`~ - some minor comment cleanups ## How was it tested? did `devbox add vim` when: 1. in devbox project's directory, but direnv disabled => did not print "Recomputing Devbox environment." 2. in devbox project's directory with direnv enabled => did print "Recomputing Devbox environment". 3. in devbox shell => did print again Repeated the same exercise with `devbox global`: 1. with `devbox global shellenv | source` run in shellrc => did print "Recomputing Devbox environment". 2. after commenting out that line in shellrc => did not print NOTE: there's no special devbox-global specific code change here.
1 parent 35ed72e commit b6d0b20

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

internal/impl/devbox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,22 @@ type installMode string
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.
211+
// ensurePackagesAreInstalled 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+
// TODO: Rename method since it does more than just ensure packages are installed.
214222
func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMode) error {
215223
defer trace.StartRegion(ctx, "ensurePackages").End()
216224
defer debug.FunctionTimer().End()
@@ -248,8 +256,10 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
248256
return err
249257
}
250258

251-
// Force print-dev-env cache to be recomputed.
252-
nixEnv, err := d.computeNixEnv(ctx, false /*use cache*/)
259+
// Use the printDevEnvCache if we are adding or removing or updating any package,
260+
// AND we are not in the shellenv-enabled environment of the current devbox-project.
261+
usePrintDevEnvCache := mode != ensure && !d.IsEnvEnabled()
262+
nixEnv, err := d.computeNixEnv(ctx, usePrintDevEnvCache)
253263
if err != nil {
254264
return err
255265
}

internal/impl/shell_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"go.jetpack.io/devbox/internal/shellgen"
1818
)
1919

20-
// update overwrites golden files with the new test results.
21-
var update = flag.Bool("update", false, "update the golden files with the test results")
20+
// updateFlag overwrites golden files with the new test results.
21+
var updateFlag = flag.Bool("update", false, "update the golden files with the test results")
2222

2323
func TestWriteDevboxShellrc(t *testing.T) {
2424
testdirs, err := filepath.Glob("testdata/shellrc/*")
@@ -83,7 +83,7 @@ func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
8383
// Overwrite the golden file if the -update flag was
8484
// set, and then proceed normally. The test should
8585
// always pass in this case.
86-
if *update {
86+
if *updateFlag {
8787
err = os.WriteFile(test.goldShellrcPath, gotShellrc, 0o666)
8888
if err != nil {
8989
t.Error("Error updating golden files:", err)

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.ensurePackagesAreInstalled(ctx, update); err != nil {
6666
return err
6767
}
6868

0 commit comments

Comments
 (0)