Skip to content

[Refactor ensureStateIsUpToDate] split into two modes for when to recompute state #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions internal/devbox/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
defer trace.StartRegion(ctx, "devboxEnsureStateIsUpToDate").End()
defer debug.FunctionTimer().End()

// if mode is install or uninstall, then we need to update the nix-profile
// and lockfile, so we must continue below.
upToDate, err := d.lockfile.IsUpToDateAndInstalled()
if err != nil {
return err
}

// if mode is install or uninstall, then we need to compute some state
// like updating the flake or installing packages locally, so must continue
// below
if mode == ensure {
// if mode is ensure and we are up to date, then we can skip the rest
if upToDate {
Expand All @@ -270,6 +271,48 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
fmt.Fprintln(d.stderr, "Ensuring packages are installed.")
}

recomputeState := mode == ensure || d.IsEnvEnabled()
if recomputeState {
if err := d.recomputeState(ctx, mode); err != nil {
return err
}
} else {
// TODO: in the next PR, we will only `nix build` the packages that are being
// added or updated. For now, we continue to call recomputeState here.
if err := d.recomputeState(ctx, mode); err != nil {
return err
}
}

// If we're in a devbox shell (global or project), then the environment might
// be out of date after the user installs something. If have direnv active
// it should reload automatically so we don't need to refresh.
if d.IsEnvEnabled() && !upToDate && !d.IsDirenvActive() {
ux.Fwarning(
d.stderr,
"Your shell environment may be out of date. Run `%s` to update it.\n",
d.refreshAliasOrCommand(),
)
}

if err := d.lockfile.Save(); err != nil {
return err
}

// If we are recomputing state, then we need to update the local.lock file.
// If not, we leave the local.lock in a stale state.
if recomputeState {
return d.lockfile.UpdateAndSaveLocalLock()
}
return nil
}

// recomputeState updates the local state comprising of:
// - plugins directories
// - devbox.lock file
// - the generated flake
// - the nix-profile
func (d *Devbox) recomputeState(ctx context.Context, mode installMode) error {
// Create plugin directories first because packages might need them
for _, pkg := range d.InstallablePackages() {
if err := d.PluginManager().Create(pkg); err != nil {
Expand Down Expand Up @@ -310,18 +353,7 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
}
}

// If we're in a devbox shell (global or project), then the environment might
// be out of date after the user installs something. If have direnv active
// it should reload automatically so we don't need to refresh.
if d.IsEnvEnabled() && !upToDate && !d.IsDirenvActive() {
ux.Fwarning(
d.stderr,
"Your shell environment may be out of date. Run `%s` to update it.\n",
d.refreshAliasOrCommand(),
)
}

return d.lockfile.Save()
return nil
}

func (d *Devbox) profilePath() (string, error) {
Expand Down
7 changes: 4 additions & 3 deletions internal/lock/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ func (f *File) ForceResolve(pkg string) (*Package, error) {
}

func (f *File) Save() error {
if err := cuecfg.WriteFile(lockFilePath(f.devboxProject), f); err != nil {
return err
}
return cuecfg.WriteFile(lockFilePath(f.devboxProject), f)
}

func (f *File) UpdateAndSaveLocalLock() error {
return updateLocal(f.devboxProject)
}

Expand Down