Skip to content

Commit 198fba4

Browse files
committed
remove bool param, and pass as Devbox option
1 parent 53e6022 commit 198fba4

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

internal/boxcli/shellenv.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ func shellEnvFunc(cmd *cobra.Command, flags shellEnvCmdFlags) (string, error) {
6969
return "", err
7070
}
7171
box, err := devbox.Open(&devopt.Opts{
72-
Dir: flags.config.path,
73-
Stderr: cmd.ErrOrStderr(),
74-
Pure: flags.pure,
75-
Env: env,
72+
Dir: flags.config.path,
73+
Stderr: cmd.ErrOrStderr(),
74+
PreservePathStack: flags.preservePathStack,
75+
Pure: flags.pure,
76+
Env: env,
7677
})
7778
if err != nil {
7879
return "", err

internal/impl/devbox.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Devbox struct {
6060
nix nix.Nixer
6161
projectDir string
6262
pluginManager *plugin.Manager
63+
preservePathStack bool
6364
pure bool
6465
allowInsecureAdds bool
6566
customProcessComposeFile string
@@ -90,6 +91,7 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
9091
projectDir: projectDir,
9192
pluginManager: plugin.NewManager(),
9293
stderr: opts.Stderr,
94+
preservePathStack: opts.PreservePathStack,
9395
pure: opts.Pure,
9496
customProcessComposeFile: opts.CustomProcessComposeFile,
9597
allowInsecureAdds: opts.AllowInsecureAdds,
@@ -175,7 +177,7 @@ func (d *Devbox) Shell(ctx context.Context) error {
175177
return err
176178
}
177179

178-
envs, err := d.nixEnv(ctx, false /*pathStackInPlace*/)
180+
envs, err := d.nixEnv(ctx)
179181
if err != nil {
180182
return err
181183
}
@@ -219,7 +221,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
219221
return err
220222
}
221223

222-
env, err := d.nixEnv(ctx, false /*pathStackInPlace*/)
224+
env, err := d.nixEnv(ctx)
223225
if err != nil {
224226
return err
225227
}
@@ -290,7 +292,7 @@ func (d *Devbox) NixEnv(ctx context.Context, includeHooks, pathStackInPlace bool
290292
return "", err
291293
}
292294

293-
envs, err := d.nixEnv(ctx, pathStackInPlace)
295+
envs, err := d.nixEnv(ctx)
294296
if err != nil {
295297
return "", err
296298
}
@@ -314,7 +316,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
314316
return nil, err
315317
}
316318

317-
envs, err := d.nixEnv(ctx, false /*pathStackInPlace*/)
319+
envs, err := d.nixEnv(ctx)
318320
if err != nil {
319321
return nil, err
320322
}
@@ -323,7 +325,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
323325

324326
func (d *Devbox) ShellEnvHash(ctx context.Context) (string, error) {
325327
// TODO savil: correct?
326-
envs, err := d.nixEnv(ctx, false /*pathStackInPlace*/)
328+
envs, err := d.nixEnv(ctx)
327329
if err != nil {
328330
return "", err
329331
}
@@ -766,11 +768,7 @@ func (d *Devbox) StartProcessManager(
766768
// Note that the shellrc.tmpl template (which sources this environment) does
767769
// some additional processing. The computeNixEnv environment won't necessarily
768770
// represent the final "devbox run" or "devbox shell" environments.
769-
func (d *Devbox) computeNixEnv(
770-
ctx context.Context,
771-
usePrintDevEnvCache bool,
772-
pathStackInPlace bool,
773-
) (map[string]string, error) {
771+
func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
774772
defer trace.StartRegion(ctx, "computeNixEnv").End()
775773

776774
// Append variables from current env if --pure is not passed
@@ -897,7 +895,7 @@ func (d *Devbox) computeNixEnv(
897895
})
898896
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
899897

900-
env = pathStack.AddToEnv(env, d.projectDirHash(), nixEnvPath, pathStackInPlace)
898+
env = pathStack.AddToEnv(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
901899
debug.Log("New path stack is: %s", pathStack)
902900

903901
debug.Log("computed environment PATH is: %s", env["PATH"])
@@ -921,7 +919,7 @@ var nixEnvCache map[string]string
921919
// nixEnv is a wrapper around computeNixEnv that caches the result.
922920
// Note that this is in-memory cache of the final environment, and not the same
923921
// as the nix print-dev-env cache which is stored in a file.
924-
func (d *Devbox) nixEnv(ctx context.Context, pathStackInPlace bool) (map[string]string, error) {
922+
func (d *Devbox) nixEnv(ctx context.Context) (map[string]string, error) {
925923
defer debug.FunctionTimer().End()
926924
if nixEnvCache != nil {
927925
return nixEnvCache, nil
@@ -938,7 +936,7 @@ func (d *Devbox) nixEnv(ctx context.Context, pathStackInPlace bool) (map[string]
938936
usePrintDevEnvCache = true
939937
}
940938

941-
return d.computeNixEnv(ctx, usePrintDevEnvCache, pathStackInPlace)
939+
return d.computeNixEnv(ctx, usePrintDevEnvCache)
942940
}
943941

944942
func (d *Devbox) nixPrintDevEnvCachePath() string {
@@ -1125,7 +1123,7 @@ func (d *Devbox) setCommonHelperEnvVars(env map[string]string) {
11251123
// give name. This matches how nix flakes behaves if there are conflicts in
11261124
// buildInputs
11271125
func (d *Devbox) NixBins(ctx context.Context) ([]string, error) {
1128-
env, err := d.nixEnv(ctx, false /*pathStackInPlace*/)
1126+
env, err := d.nixEnv(ctx)
11291127
if err != nil {
11301128
return nil, err
11311129
}

internal/impl/devbox_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestComputeNixEnv(t *testing.T) {
7171
d := devboxForTesting(t)
7272
d.nix = &testNix{}
7373
ctx := context.Background()
74-
env, err := d.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/)
74+
env, err := d.computeNixEnv(ctx, false /*use cache*/)
7575
require.NoError(t, err, "computeNixEnv should not fail")
7676
assert.NotNil(t, env, "computeNixEnv should return a valid env")
7777
}
@@ -80,7 +80,7 @@ func TestComputeNixPathIsIdempotent(t *testing.T) {
8080
devbox := devboxForTesting(t)
8181
devbox.nix = &testNix{"/tmp/my/path"}
8282
ctx := context.Background()
83-
env, err := devbox.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/)
83+
env, err := devbox.computeNixEnv(ctx, false /*use cache*/)
8484
require.NoError(t, err, "computeNixEnv should not fail")
8585
path := env["PATH"]
8686
assert.NotEmpty(t, path, "path should not be nil")
@@ -90,7 +90,7 @@ func TestComputeNixPathIsIdempotent(t *testing.T) {
9090
t.Setenv(envpath.PathStackEnv, env[envpath.PathStackEnv])
9191
t.Setenv(envpath.Key(devbox.projectDirHash()), env[envpath.Key(devbox.projectDirHash())])
9292

93-
env, err = devbox.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/)
93+
env, err = devbox.computeNixEnv(ctx, false /*use cache*/)
9494
require.NoError(t, err, "computeNixEnv should not fail")
9595
path2 := env["PATH"]
9696

@@ -101,7 +101,7 @@ func TestComputeNixPathWhenRemoving(t *testing.T) {
101101
devbox := devboxForTesting(t)
102102
devbox.nix = &testNix{"/tmp/my/path"}
103103
ctx := context.Background()
104-
env, err := devbox.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/)
104+
env, err := devbox.computeNixEnv(ctx, false /*use cache*/)
105105
require.NoError(t, err, "computeNixEnv should not fail")
106106
path := env["PATH"]
107107
assert.NotEmpty(t, path, "path should not be nil")
@@ -113,7 +113,7 @@ func TestComputeNixPathWhenRemoving(t *testing.T) {
113113
t.Setenv(envpath.Key(devbox.projectDirHash()), env[envpath.Key(devbox.projectDirHash())])
114114

115115
devbox.nix.(*testNix).path = ""
116-
env, err = devbox.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/)
116+
env, err = devbox.computeNixEnv(ctx, false /*use cache*/)
117117
require.NoError(t, err, "computeNixEnv should not fail")
118118
path2 := env["PATH"]
119119
assert.NotContains(t, path2, "/tmp/my/path", "path should not contain /tmp/my/path")

internal/impl/devopt/devboxopts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Opts struct {
88
AllowInsecureAdds bool
99
Dir string
1010
Env map[string]string
11+
PreservePathStack bool
1112
Pure bool
1213
IgnoreWarnings bool
1314
CustomProcessComposeFile string

internal/impl/envpath/pathstack.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ func (s *Stack) AddToEnv(
6464
env map[string]string,
6565
projectHash string,
6666
nixEnvPath string,
67-
pathStackInPlace bool,
67+
preservePathStack bool,
6868
) map[string]string {
6969
key := Key(projectHash)
7070

7171
// Add this nixEnvPath to env
7272
env[key] = nixEnvPath
7373

7474
// Common case: ensure this key is at the top of the stack
75-
if !pathStackInPlace ||
76-
// Case pathStackInPlace == true, usually from bin-wrapper or (in future) shell hook.
75+
if !preservePathStack ||
76+
// Case preservePathStack == true, usually from bin-wrapper or (in future) shell hook.
7777
// Add this key only if absent from the stack
7878
!lo.Contains(s.keys, key) {
7979

8080
s.keys = lo.Uniq(slices.Insert(s.keys, 0, key))
8181
}
8282
env[PathStackEnv] = s.String()
8383

84-
// Look up the paths-list for each paths-stack element, and join them together to get the final PATH.
84+
// Look up the paths-list for each Stack element, and join them together to get the final PATH.
8585
pathLists := lo.Map(s.keys, func(part string, idx int) string { return env[part] })
8686
env["PATH"] = JoinPathLists(pathLists...)
8787
return env

internal/impl/packages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
234234
}
235235

236236
// Force print-dev-env cache to be recomputed.
237-
if _, err := d.computeNixEnv(ctx, false /*use cache*/, false /*pathStackInPlace*/); err != nil {
237+
if _, err := d.computeNixEnv(ctx, false /*use cache*/); err != nil {
238238
return err
239239
}
240240

internal/wrapnix/wrapper.sh.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DO_NOT_TRACK=1 can be removed once we optimize segment to queue events.
2121

2222
if [[ "${{ .ShellEnvHashKey }}" != "{{ .ShellEnvHash }}" ]] && [[ -z "${{ .ShellEnvHashKey }}_GUARD" ]]; then
2323
export {{ .ShellEnvHashKey }}_GUARD=true
24-
eval "$(DO_NOT_TRACK=1 devbox shellenv --path-stack-in-place -c {{ .ProjectDir }})"
24+
eval "$(DO_NOT_TRACK=1 devbox shellenv --preserve-path-stack -c {{ .ProjectDir }})"
2525
fi
2626

2727
{{/*

0 commit comments

Comments
 (0)