Skip to content

Conversation

mikeland73
Copy link
Contributor

Summary

This causes the default behavior for devbox global shellenv to no longer recompute environment. It adds a new flag:

devbox global shellenv -r which causes the recompute if needed (i.e. not cached)

Recomputing the environment entails two steps:

  • Ensuring everything is installed
  • Running print-dev-env

It also removed a bunch of ensurePackagesAreInstalled calls that are now handled by nixEnv(). Note that in all cases, we were calling ensurePackagesAreInstalled(ensure) followed by nixEnv()

How was it tested?

  • Modified devbox.json manually
  • Opened new tab
  • Saw message:
Info: Your devbox environment may be out of date. Please run

eval "$(devbox global shellenv -r)"
  • Ran eval "$(devbox global shellenv -r)"
  • Opened another tab and did not see message.

@mikeland73 mikeland73 requested review from savil and ipince October 6, 2023 03:51
Copy link
Collaborator

@savil savil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Just see one of the comments below where I think the current code will run nix print-dev-env twice.

command.AddCommand(setupCmd())
command.AddCommand(shellCmd())
command.AddCommand(shellEnvCmd())
command.AddCommand(shellEnvCmd(lo.ToPtr(false) /*dontRecomputeEnv*/))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reads as dontRecomputeEnv param is set to false. I think this is the opposite of what is happening?

lo.ToPtr(false) /*recomputeEnvIfNeeded*/)

OR

// don't recompute the env in shellenv:
command.AddCommand(shellEnvCmd(lo.ToPtr(false)))


envStr, err := box.NixEnv(cmd.Context(), flags.runInitHook)
envStr, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nbd, but I prefer the recomputeEnvIfNeeded as a param name. Can we reuse that within NixEnvOpts as well?

Alternatively, could it be skipRecomputeEnvironment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I struggled with this as well. Here's why I went with this:

The issue with recomputeEnvIfNeeded is that we would need to default it to true. I prefer if the default is true and only pass in a param to make it false.

The reason that flag is the other way around is because devbox global shellenv --no-recompute=false would be super awkward because of the double negative.

func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
ctx, task := trace.NewTask(ctx, "devboxEnvVars")
defer task.End()
// this only returns env variables for the shell environment excluding hooks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I think this comment was actually for nixEnv, right? If so, could we leave it in place?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its explaining why Mohsen did nixEnv and not NixEnv.

if !usePrintDevEnvCache {
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
return nil, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think this code will result in running nix print-dev-env two times in this scenario.

I believe we can set:

usePrintDevEnvCache = true

after ensurePackagesAreInstalled, because inside ensurePackagesAreInstalled we'll call computeNixEnv(ctx, true).

Or, even better directly hardcode below:
return d.computeNixEnv(ctx, true /*usePrintDevEnvCache*/)


if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
return err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move Starting a devbox shell... to after nixEnv so the printed output is as before:
first, nixEnv output about recomputing devbox environment, etc.
then, "Starting a devbox shell....`
then, any init-hook output or shellrc output

ux.Finfo(
d.stderr,
"Your devbox environment may be out of date. Please run \n\n"+
"eval \"$(devbox global shellenv -r)\"\n\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh two comments on this:

  1. could we say: devbox global shellenv --recompute? I assume users will copy-paste it so not a problem to be a bit more verbose, and does explain better what it'll do.

  2. For fish shells, we need to print a different output:

devbox global shellenv -r | source

@mikeland73 mikeland73 merged commit d564fe2 into main Oct 9, 2023
@mikeland73 mikeland73 deleted the landau/no-ensure-in-global branch October 9, 2023 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants