Skip to content

Commit 6d7eb36

Browse files
authored
[wrappers] Improve wrappers in multi-project env (#1067)
## Summary This is a possible cause of #881 (comment) bin wrappers belong to a specific project so we should not have a single global hash. ## How was it tested? `devbox shell` called binaries, inspected wrappers `devbox add curl` called binaries, inspected wrappers
1 parent 20e598b commit 6d7eb36

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

internal/impl/devbox.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,12 @@ func (d *Devbox) ShellEnvHash(ctx context.Context) (string, error) {
305305
return "", err
306306
}
307307

308-
return envs[devboxShellEnvHashVarName], nil
308+
return envs[d.ShellEnvHashKey()], nil
309+
}
310+
311+
func (d *Devbox) ShellEnvHashKey() string {
312+
// Don't make this a const so we don't use it by itself accidentally
313+
return "__DEVBOX_SHELLENV_HASH_" + d.projectDirHash()
309314
}
310315

311316
func (d *Devbox) Info(pkg string, markdown bool) error {
@@ -827,7 +832,7 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
827832
os.Getenv("XDG_DATA_DIRS"),
828833
)
829834

830-
return env, addHashToEnv(env)
835+
return env, d.addHashToEnv(env)
831836
}
832837

833838
var nixEnvCache map[string]string
@@ -1113,10 +1118,10 @@ func (d *Devbox) projectDirHash() string {
11131118
return hash
11141119
}
11151120

1116-
func addHashToEnv(env map[string]string) error {
1121+
func (d *Devbox) addHashToEnv(env map[string]string) error {
11171122
hash, err := cuecfg.Hash(env)
11181123
if err == nil {
1119-
env[devboxShellEnvHashVarName] = hash
1124+
env[d.ShellEnvHashKey()] = hash
11201125
}
11211126
return err
11221127
}

internal/impl/envvars.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
)
1212

1313
const devboxSetPrefix = "__DEVBOX_SET_"
14-
const devboxShellEnvHashVarName = "__DEVBOX_SHELLENV_HASH"
1514

1615
func mapToPairs(m map[string]string) []string {
1716
pairs := []string{}

internal/wrapnix/wrapper.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
type devboxer interface {
2424
NixBins(ctx context.Context) ([]string, error)
2525
ShellEnvHash(ctx context.Context) (string, error)
26+
ShellEnvHashKey() string
2627
ProjectDir() string
2728
Services() (services.Services, error)
2829
}
@@ -53,20 +54,20 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
5354
bashPath := cmdutil.GetPathOrDefault("bash", "/bin/bash")
5455
for _, service := range services {
5556
if err = createWrapper(&createWrapperArgs{
57+
devboxer: devbox,
5658
BashPath: bashPath,
5759
Command: service.Start,
5860
Env: service.Env,
59-
ProjectDir: devbox.ProjectDir(),
6061
ShellEnvHash: shellEnvHash,
6162
destPath: filepath.Join(destPath, service.StartName()),
6263
}); err != nil {
6364
return err
6465
}
6566
if err = createWrapper(&createWrapperArgs{
67+
devboxer: devbox,
6668
BashPath: bashPath,
6769
Command: service.Stop,
6870
Env: service.Env,
69-
ProjectDir: devbox.ProjectDir(),
7071
ShellEnvHash: shellEnvHash,
7172
destPath: filepath.Join(destPath, service.StopName()),
7273
}); err != nil {
@@ -81,9 +82,9 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
8182

8283
for _, bin := range bins {
8384
if err = createWrapper(&createWrapperArgs{
85+
devboxer: devbox,
8486
BashPath: bashPath,
8587
Command: bin,
86-
ProjectDir: devbox.ProjectDir(),
8788
ShellEnvHash: shellEnvHash,
8889
destPath: filepath.Join(destPath, filepath.Base(bin)),
8990
}); err != nil {
@@ -95,10 +96,10 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
9596
}
9697

9798
type createWrapperArgs struct {
99+
devboxer
98100
BashPath string
99101
Command string
100102
Env map[string]string
101-
ProjectDir string
102103
ShellEnvHash string
103104

104105
destPath string

internal/wrapnix/wrapper.sh.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ fi
1414
{{- end }}
1515

1616
{{/*
17-
We use __DEVBOX_SHELLENV_HASH to avoid re-sourcing shellenv. Since wrappers
17+
We use ShellEnvHashKey to avoid re-sourcing shellenv. Since wrappers
1818
call other wrappers and potentially modify the environment, we don't want the
1919
environment to get re-written.
2020

2121
DO_NOT_TRACK=1 can be removed once we optimize segment to queue events.
2222
*/ -}}
2323

24-
if [[ "$__DEVBOX_SHELLENV_HASH" != "{{ .ShellEnvHash }}" ]]; then
24+
if [[ "${{ .ShellEnvHashKey }}" != "{{ .ShellEnvHash }}" ]]; then
2525
eval "$(DO_NOT_TRACK=1 devbox shellenv -c {{ .ProjectDir }})"
2626
fi
2727

0 commit comments

Comments
 (0)