Skip to content

Commit 172452d

Browse files
authored
[shellenv] PrintEnv should generate just in case (#830)
## Summary Fixes #829 `run` and `shell` always call generate but using `shell --print-env` and `shellenv` don't. This means that sometimes the `.devbox` directory might be out of date (e.g. when we turned on flakes, not everyone has `flake.nix` file). Since not all workflows involve `shell` and `run`, some users never update it. For performance reasons, we only update if the devbox version doesn't match whatever version created the `.devbox` dir. This change may not be sufficient to fix all cases, but it fixes the attached issue. ## How was it tested? ```bash rm .devbox/gen/flake/flake.nix devbox shellenv > /dev/null rm .devbox/gen/flake/flake.nix devbox shell --print-env > /dev/nul `` no error (previously this gave me same error as issue)
1 parent e6426e3 commit 172452d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

internal/impl/devbox.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ func (d *Devbox) PrintEnv() (string, error) {
256256
ctx, task := trace.NewTask(context.Background(), "devboxPrintEnv")
257257
defer task.End()
258258

259+
// generate in case user has old .devbox dir and is missing any files.
260+
if !d.isDotDevboxVersionCurrent() {
261+
if err := d.Generate(); err != nil {
262+
return "", err
263+
}
264+
}
265+
259266
envs, err := d.cachedComputeNixEnv(ctx)
260267
if err != nil {
261268
return "", err

internal/impl/generate.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"text/template"
1616

1717
"github.com/pkg/errors"
18+
"go.jetpack.io/devbox/internal/build"
1819
"go.jetpack.io/devbox/internal/cuecfg"
1920
"go.jetpack.io/devbox/internal/debug"
2021
"go.jetpack.io/devbox/internal/planner/plansdk"
@@ -60,7 +61,7 @@ func generateForShell(rootPath string, plan *plansdk.ShellPlan, pluginManager *p
6061
}
6162
}
6263

63-
return nil
64+
return os.WriteFile(".devbox/version", []byte(build.Version), 0644)
6465
}
6566

6667
// Cache and buffers for generating templated files.
@@ -209,3 +210,12 @@ func isProjectInGitRepo(dir string) bool {
209210
// we still haven't found what we're looking for.
210211
return false
211212
}
213+
214+
func (d *Devbox) isDotDevboxVersionCurrent() bool {
215+
b, err := os.ReadFile(filepath.Join(d.projectDir, ".devbox/version"))
216+
if err != nil {
217+
return false
218+
}
219+
220+
return strings.TrimSpace(string(b)) == build.Version
221+
}

0 commit comments

Comments
 (0)