Skip to content
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

feat: Add recompute flag to shellenv command #1963

Closed

Conversation

dax
Copy link

@dax dax commented Apr 5, 2024

Summary

(related issue)
When used with direnv and when regularly switching between branches, Devbox slows down the workflow because it tries to keep the packages in sync with the devbox.lock state with a Ensuring packages are installed.
This PR add the --recompute flag to the shellenv command that allow to set it to --recompute=false and then disable the automatic installation of package in case devbox.json is out of sync.

How was it tested?

Setting the .envrc (ie. the direnv config file) as:

devbox shellenv --init-hook --no-refresh-alias --recompute=false

--install has been removed and --recompute=false compared with what devbox generate direnv --print-envrc generate by default.

Then updating devbox.json does not trigger package installation or removal but the context (ie. the env vars) are correctly updated.

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.

Thanks! I generally like the approach. That said, looking at the implementation I think a nicer fix would be to push the flag inside shellEnvCmd function's implementation.

Right now, we have this awkward parameter in func shellEnvCmd(recomputeEnvIfNeeded *bool) because we wanted it selectively exposed for just global and not regular devbox. If we're seeking to expose the --recompute flag in both global and regular devbox, then no need to expose this parameter. The flag can be handled internally inside shellEnvCmd.

Would you wanna make that change? If you are strapped for time, then happy to approve this PR, land it and I can make a follow up fix for it.

@savil
Copy link
Collaborator

savil commented Apr 8, 2024

would you mind also running devbox run lint to ensure the code is considered "clean"?

@savil
Copy link
Collaborator

savil commented Apr 8, 2024

devbox run fmt should help with formatting concerns

@dax
Copy link
Author

dax commented Apr 9, 2024

Would you wanna make that change?

Thank you @savil for the suggestion. I'll push an update soon (and run the linter and formatter 😄 )

@dax dax force-pushed the add-recompute-flag-to-shellenv-command branch from 3ed1f03 to 50cfc7d Compare April 9, 2024 06:28
@dax
Copy link
Author

dax commented Apr 9, 2024

@savil The PR is now ready 🙏

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

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

I think adding this flags makes sense, but the current implementation will cause a change in behavior in global shellenv (because recompute was previously defaulted to false, and now it is defaulted to true).

I think you may be able to simply override the flag in the global case (but we need some testing to confirm, cobra internals don't always behave intuitively).

@dax
Copy link
Author

dax commented Apr 23, 2024

Thanks @mikeland73 for spotting the regression 🙏
I guess I can close this PR as @savil now have submitted a cleaner one 🙏

@dax dax closed this Apr 23, 2024
savil added a commit that referenced this pull request Apr 25, 2024
…shellenv's recompute flag with default=false (#2013)

## Summary

Credit to @dax and #1963 for pointing out the need for this, and
pioneering this solution.
This PR merely nudges the code to be a bit neater.

## How was it tested?

```
% git diff
diff --git a/internal/boxcli/shellenv.go b/internal/boxcli/shellenv.go
index 991c2bec..8076e125 100644
--- a/internal/boxcli/shellenv.go
+++ b/internal/boxcli/shellenv.go
@@ -78,6 +78,9 @@ func shellEnvFunc(
        cmd *cobra.Command,
        flags shellEnvCmdFlags,
 ) (string, error) {
+       fmt.Printf("shellenv --recompute flag is %v\n", flags.recompute)
+       return "", nil
+
        env, err := flags.Env(flags.config.path)
        if err != nil {
                return "", err
```

compiled via `devbox run build`, and then:

```
 % devbox shellenv
shellenv --recompute flag is true

hash -r
 % devbox global shellenv
shellenv --recompute flag is false

hash -r
```
savil added a commit that referenced this pull request Nov 5, 2024
## Summary

This PR continues the work from #2013 to also add the `--recompute` flag
option for `devbox run` and `devbox shell`.
For some users on bad networks, this can save them annoyance and time
for when they _know_ their devbox environment is up-to-date.

Fixes #2315

## How was it tested?
This PR affects 3 commands: `run`, `shell` and `shellenv`.

1. For `run`:
Added `"hello": "latest",` to devbox.json of this project.

```
devbox run --recompute=false -- echo "hello world"
Warning: Your devbox environment may be out of date. Run with --recompute=true to update it.
hello world
```

then
```
devbox run -- echo "hello world"
Info: Ensuring packages are installed.
✓ Computed the Devbox environment.
hello world
```

2. For `shell`. Ran similar commands as above.

3. For `shellenv`. Followed test plan of #1963.

Changed the `.envrc` to be:
```
.envrc
@@ -1,7 +1,13 @@
 # Automatically sets up your devbox environment whenever you cd into this
 # directory via our direnv integration:

-eval "$(devbox generate direnv --print-envrc)"
+#eval "$(devbox generate direnv --print-envrc)"

+ # output of `devbox generate direnv --print-envrc` to modify it
+use_devbox() {
+    watch_file devbox.json devbox.lock
+    # eval "$(devbox shellenv --init-hook --install --no-refresh-alias)"
+    eval "$(devbox shellenv --init-hook --no-refresh-alias --recompute=false)"
+}
+use devbox
 # check out https://www.jetify.com/devbox/docs/ide_configuration/direnv/
 # for more details
```

Then modified devbox.json and saw the warning get printed.

---------

Co-authored-by: savil <>
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.

3 participants