Skip to content

Commit

Permalink
[print-dev-env] Skip cache if lockfile has changed (#1674)
Browse files Browse the repository at this point in the history
## Summary

We need to skip the PrintDevEnvCache in ComputeNixEnv if lockfile is
stale.

`ensurePackagesAreInstalledAndComputeEnv` had made an assumption that
`ensurePackagesAreInstalled` will have called `computeNixEnv` already to
ensure
the PrintDevEnvCache was updated. However, after some recent
refactoring, we
no longer call `computeNixEnv` within `ensurePackagesAreInstalled`.

So, we now need to check the lockfile status, and skip the
PrintDevEnvCache
if lockfile is stale.

## How was it tested?

1. Had `patch_glibc:true` for `hello` package. Did `devbox shell` and
then
saw that `which hello` points to the patch-glibc flake's hello.

2. exited devbox shell, and edited devbox.json to `patch_glibc:false`

3. re-started `devbox shell` and `which hello` now shows the regular nix
`hello`.
Previously, it would still show the patch-glibc hello since `PATH` would
not have
been updated to omit that.
  • Loading branch information
savil authored Dec 8, 2023
1 parent 6b61988 commit 209c839
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ func (d *Devbox) ensurePackagesAreInstalledAndComputeEnv(
// it's ok to use usePrintDevEnvCache=true here always. This does end up
// doing some non-nix work twice if lockfile is not up to date.
// TODO: Improve this to avoid extra work.
return d.computeNixEnv(ctx, true)
return d.computeNixEnv(ctx, true /*usePrintDevEnvCache*/)
}

func (d *Devbox) nixPrintDevEnvCachePath() string {
Expand Down
7 changes: 7 additions & 0 deletions internal/impl/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
return err
}

// Use the printDevEnvCache if we are adding or removing or updating any package,
// AND we are not in the shellenv-enabled environment of the current devbox-project.
usePrintDevEnvCache := mode != ensure && !d.IsEnvEnabled()
if _, err := d.computeNixEnv(ctx, usePrintDevEnvCache); err != nil {
return err
}

// Ensure we clean out packages that are no longer needed.
d.lockfile.Tidy()

Expand Down

0 comments on commit 209c839

Please sign in to comment.