Skip to content

Commit 29ea183

Browse files
committed
requested changes by Greg and Mike
1 parent 80b2b2f commit 29ea183

File tree

6 files changed

+50
-49
lines changed

6 files changed

+50
-49
lines changed

internal/envir/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package envir
55

66
import (
7+
"fmt"
78
"os"
89
"strconv"
10+
"strings"
911
)
1012

1113
func IsDevboxCloud() bool {
@@ -43,3 +45,23 @@ func GetValueOrDefault(key, def string) string {
4345

4446
return val
4547
}
48+
49+
func MapToPairs(m map[string]string) []string {
50+
pairs := []string{}
51+
for k, v := range m {
52+
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
53+
}
54+
return pairs
55+
}
56+
57+
func PairsToMap(pairs []string) map[string]string {
58+
vars := map[string]string{}
59+
for _, p := range pairs {
60+
k, v, ok := strings.Cut(p, "=")
61+
if !ok {
62+
continue
63+
}
64+
vars[k] = v
65+
}
66+
return vars
67+
}

internal/impl/devbox.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"go.jetpack.io/devbox/internal/searcher"
2626
"go.jetpack.io/devbox/internal/shellgen"
2727
"go.jetpack.io/devbox/internal/telemetry"
28+
"golang.org/x/exp/maps"
2829
"golang.org/x/exp/slices"
2930

3031
"go.jetpack.io/devbox/internal/boxcli/usererr"
@@ -789,8 +790,8 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
789790

790791
debug.Log("current environment PATH is: %s", env["PATH"])
791792

792-
pathStack := envpath.Stack(env)
793-
debug.Log("Original path stack is: %s", pathStack)
793+
originalEnv := make(map[string]string, len(env))
794+
maps.Copy(originalEnv, env)
794795

795796
vaf, err := d.nix.PrintDevEnv(ctx, &nix.PrintDevEnvArgs{
796797
FlakesFilePath: d.nixFlakesFilePath(),
@@ -896,7 +897,10 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
896897
})
897898
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
898899

899-
env = pathStack.PushAndUpdateEnv(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
900+
pathStack := envpath.Stack(originalEnv)
901+
pathStack.Push(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
902+
env["PATH"] = pathStack.Path(env)
903+
900904
debug.Log("New path stack is: %s", pathStack)
901905

902906
debug.Log("computed environment PATH is: %s", env["PATH"])

internal/impl/envpath/stack.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type stack struct {
3232

3333
func Stack(env map[string]string) *stack {
3434
stackEnv, ok := env[PathStackEnv]
35-
if !ok {
35+
if !ok || strings.TrimSpace(stackEnv) == "" {
3636
// if path stack is empty, then push the current PATH, which is the
3737
// external environment prior to any devbox-shellenv being applied to it.
3838
stackEnv = InitPathEnv
@@ -48,29 +48,33 @@ func (s *stack) String() string {
4848
return strings.Join(s.keys, ":")
4949
}
5050

51+
func (s *stack) Path(env map[string]string) string {
52+
// Look up the paths-list for each stack element, and join them together to get the final PATH.
53+
pathLists := lo.Map(s.keys, func(part string, idx int) string { return env[part] })
54+
return JoinPathLists(pathLists...)
55+
}
56+
5157
// Key is the element stored in the stack for a devbox-project. It represents
5258
// a pointer to the nixEnvPath value stored in its own env-var, also using this same
5359
// Key.
5460
func Key(projectHash string) string {
5561
return "DEVBOX_NIX_ENV_PATH_" + projectHash
5662
}
5763

58-
// PushAndUpdateEnv adds the new nixEnvPath for the devbox-project identified by projectHash.
59-
// The nixEnvPath is pushed to the top of the stack (given highest priority), unless preservePathStack
60-
// is enabled.
64+
// Push adds the new nixEnvPath for the devbox-project identified by projectHash.
65+
// The nixEnvPath is pushed to the top of the stack (given highest priority),
66+
// unless preservePathStack is enabled.
6167
//
62-
// It also updated the env by modifying the following env-vars:
68+
// It also updates the env by modifying the following env-vars:
6369
// 1. nixEnvPath key
6470
// 2. PathStack
6571
// 3. PATH
66-
//
67-
// Returns the modified env map
68-
func (s *stack) PushAndUpdateEnv(
72+
func (s *stack) Push(
6973
env map[string]string,
7074
projectHash string,
7175
nixEnvPath string,
7276
preservePathStack bool,
73-
) map[string]string {
77+
) {
7478
key := Key(projectHash)
7579

7680
// Add this nixEnvPath to env
@@ -85,15 +89,10 @@ func (s *stack) PushAndUpdateEnv(
8589
s.keys = lo.Uniq(slices.Insert(s.keys, 0, key))
8690
}
8791
env[PathStackEnv] = s.String()
88-
89-
// Look up the paths-list for each stack element, and join them together to get the final PATH.
90-
pathLists := lo.Map(s.keys, func(part string, idx int) string { return env[part] })
91-
env["PATH"] = JoinPathLists(pathLists...)
92-
return env
9392
}
9493

9594
// Has tests if the stack has the specified key. Refer to the Key function for constructing
9695
// the appropriate key for any devbox-project.
97-
func (s *stack) Has(key string) bool {
98-
return lo.Contains(s.keys, key)
96+
func (s *stack) Has(projectHash string) bool {
97+
return lo.Contains(s.keys, Key(projectHash))
9998
}

internal/impl/envvars.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,16 @@
44
package impl
55

66
import (
7-
"fmt"
87
"os"
98
"sort"
109
"strings"
1110

11+
"go.jetpack.io/devbox/internal/envir"
1212
"go.jetpack.io/devbox/internal/impl/envpath"
1313
)
1414

1515
const devboxSetPrefix = "__DEVBOX_SET_"
1616

17-
func mapToPairs(m map[string]string) []string {
18-
pairs := []string{}
19-
for k, v := range m {
20-
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
21-
}
22-
return pairs
23-
}
24-
25-
func pairsToMap(pairs []string) map[string]string {
26-
vars := map[string]string{}
27-
for _, p := range pairs {
28-
k, v, ok := strings.Cut(p, "=")
29-
if !ok {
30-
continue
31-
}
32-
vars[k] = v
33-
}
34-
return vars
35-
}
36-
3717
// exportify takes a map of [string]string and returns a single string
3818
// of the form export KEY="VAL"; and escapes all the vals from special characters.
3919
func exportify(vars map[string]string) string {
@@ -138,11 +118,6 @@ func markEnvsAsSetByDevbox(envs ...map[string]string) {
138118
// as a proxy for this. This allows us to differentiate between global and
139119
// individual project shells.
140120
func (d *Devbox) IsEnvEnabled() bool {
141-
env := map[string]string{}
142-
for _, keyVal := range os.Environ() {
143-
parts := strings.Split(keyVal, "=")
144-
env[parts[0]] = parts[1]
145-
}
146-
pathStack := envpath.Stack(env)
147-
return pathStack.Has(envpath.Key(d.projectDirHash()))
121+
pathStack := envpath.Stack(envir.PairsToMap(os.Environ()))
122+
return pathStack.Has(d.projectDirHash())
148123
}

internal/impl/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (s *DevboxShell) Run() error {
246246
}
247247

248248
cmd = exec.Command(s.binPath)
249-
cmd.Env = mapToPairs(env)
249+
cmd.Env = envir.MapToPairs(env)
250250
cmd.Args = append(cmd.Args, extraArgs...)
251251
cmd.Stdin = os.Stdin
252252
cmd.Stdout = os.Stdout

internal/impl/shell_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414

1515
"github.com/google/go-cmp/cmp"
16+
"go.jetpack.io/devbox/internal/envir"
1617
"go.jetpack.io/devbox/internal/shellgen"
1718
)
1819

@@ -45,7 +46,7 @@ func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
4546
test := &tests[i]
4647
test.name = filepath.Base(path)
4748
if b, err := os.ReadFile(filepath.Join(path, "env")); err == nil {
48-
test.env = pairsToMap(strings.Split(string(b), "\n"))
49+
test.env = envir.PairsToMap(strings.Split(string(b), "\n"))
4950
}
5051

5152
test.hooksFilePath = shellgen.ScriptPath(projectDir, shellgen.HooksFilename)

0 commit comments

Comments
 (0)