Skip to content

Commit 652f060

Browse files
committed
requested changes in envpath
1 parent 46e440a commit 652f060

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

internal/impl/devbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
789789

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

792-
pathStack := envpath.NewStack(env)
792+
pathStack := envpath.Stack(env)
793793
debug.Log("Original path stack is: %s", pathStack)
794794

795795
vaf, err := d.nix.PrintDevEnv(ctx, &nix.PrintDevEnvArgs{
@@ -896,7 +896,7 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
896896
})
897897
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
898898

899-
env = pathStack.AddToEnv(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
899+
env = pathStack.PushAndUpdateEnv(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
900900
debug.Log("New path stack is: %s", pathStack)
901901

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

internal/impl/envpath/pathstack.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,64 @@ import (
88
)
99

1010
const (
11+
// PathStackEnv stores the string representation of the stack, as a ":" separated list.
12+
// Each element in the list is also the key to the env-var that stores the
13+
// nixEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
1114
PathStackEnv = "DEVBOX_PATH_STACK"
1215

1316
// InitPathEnv stores the path prior to any devbox shellenv modifying the environment
1417
InitPathEnv = "DEVBOX_INIT_PATH"
1518
)
1619

17-
// Stack has the following design:
18-
// 1. The PathStack enables tracking which sub-paths in PATH come from which devbox-project
19-
// 2. What it stores: The PathStack is an ordered-list of nixEnvPathKeys
20-
// 3. Each nixEnvPathKey is set as an env-var with the value of the nixEnvPath for that devbox-project.
21-
// 4. The final PATH is reconstructed by concatenating the env-var values of each nixEnvPathKey env-var.
22-
// 5. The Stack is stored in its own env-var shared by all devbox-projects in this shell.
23-
type Stack struct {
20+
// stack has the following design:
21+
// 1. The stack enables tracking which sub-paths in PATH come from which devbox-project
22+
// 2. It is an ordered-list of keys to env-vars that store nixEnvPath values of devbox-projects.
23+
// 3. The final PATH is reconstructed by concatenating the env-var values of each nixEnvPathKey.
24+
// 5. The stack is stored in its own env-var PathStackEnv, shared by all devbox-projects in this shell.
25+
type stack struct {
2426

2527
// keys holds the stack elements.
2628
// Earlier (lower index number) keys get higher priority.
27-
// This keeps the string representation of the Stack aligned with the PATH value.
29+
// This keeps the string representation of the stack aligned with the PATH value.
2830
keys []string
2931
}
3032

31-
func NewStack(env map[string]string) *Stack {
33+
func Stack(env map[string]string) *stack {
3234
stackEnv, ok := env[PathStackEnv]
3335
if !ok {
3436
// if path stack is empty, then push the current PATH, which is the
3537
// external environment prior to any devbox-shellenv being applied to it.
3638
stackEnv = InitPathEnv
3739
env[InitPathEnv] = env["PATH"]
3840
}
39-
return &Stack{
41+
return &stack{
4042
keys: strings.Split(stackEnv, ":"),
4143
}
4244
}
4345

44-
// String is the value of the Stack stored in its env-var.
45-
func (s *Stack) String() string {
46+
// String is the value of the stack stored in its env-var.
47+
func (s *stack) String() string {
4648
return strings.Join(s.keys, ":")
4749
}
4850

49-
// Key is the element stored in the Stack for a devbox-project. It represents
51+
// Key is the element stored in the stack for a devbox-project. It represents
5052
// a pointer to the nixEnvPath value stored in its own env-var, also using this same
5153
// Key.
5254
func Key(projectHash string) string {
53-
return "DEVBOX_NIX_" + projectHash
55+
return "DEVBOX_NIX_ENV_PATH_" + projectHash
5456
}
5557

56-
// AddToEnv adds the new nixEnvPath for the devbox-project identified by projectHash to the env.
57-
// It does so by modifying the following env-vars:
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.
61+
//
62+
// It also updated the env by modifying the following env-vars:
5863
// 1. nixEnvPath key
5964
// 2. PathStack
6065
// 3. PATH
6166
//
6267
// Returns the modified env map
63-
func (s *Stack) AddToEnv(
68+
func (s *stack) PushAndUpdateEnv(
6469
env map[string]string,
6570
projectHash string,
6671
nixEnvPath string,
@@ -81,19 +86,14 @@ func (s *Stack) AddToEnv(
8186
}
8287
env[PathStackEnv] = s.String()
8388

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

9095
// Has tests if the stack has the specified key. Refer to the Key function for constructing
9196
// the appropriate key for any devbox-project.
92-
func (s *Stack) Has(key string) bool {
93-
for _, k := range s.keys {
94-
if k == key {
95-
return true
96-
}
97-
}
98-
return false
97+
func (s *stack) Has(key string) bool {
98+
return lo.Contains(s.keys, key)
9999
}

internal/impl/envvars.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,6 @@ func (d *Devbox) IsEnvEnabled() bool {
143143
parts := strings.Split(keyVal, "=")
144144
env[parts[0]] = parts[1]
145145
}
146-
pathStack := envpath.NewStack(env)
146+
pathStack := envpath.Stack(env)
147147
return pathStack.Has(envpath.Key(d.projectDirHash()))
148148
}

0 commit comments

Comments
 (0)