Skip to content

Commit 31724df

Browse files
committed
[shellenv] fix PATH nesting for devbox-project and devbox-global
1 parent 0d64e71 commit 31724df

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

internal/impl/devbox.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,16 +786,14 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
786786

787787
currentEnvPath := env["PATH"]
788788
debug.Log("current environment PATH is: %s", currentEnvPath)
789-
// Use the original path, if available. If not available, set it for future calls.
790-
// See https://github.com/jetpack-io/devbox/issues/687
791-
// We add the project dir hash to ensure that we don't have conflicts
792-
// between different projects (including global)
793-
// (moving a project would change the hash and that's fine)
794-
originalPath, ok := env[d.ogPathKey()]
789+
790+
pathStackEnv, ok := env["PATH_STACK"]
795791
if !ok {
792+
// if path stack is empty, then set the first element
793+
pathStackEnv = d.ogPathKey()
796794
env[d.ogPathKey()] = currentEnvPath
797-
originalPath = currentEnvPath
798795
}
796+
debug.Log("path stack is: %s", pathStackEnv)
799797

800798
vaf, err := d.nix.PrintDevEnv(ctx, &nix.PrintDevEnvArgs{
801799
FlakesFilePath: d.nixFlakesFilePath(),
@@ -901,7 +899,25 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
901899
})
902900
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
903901

904-
env["PATH"] = JoinPathLists(nixEnvPath, originalPath)
902+
// The PathStack enables:
903+
// 1. Tracking which sub-paths in PATH come from which devbox-project
904+
// 2. The priority order of these sub-paths: devbox-projects encountered later get higher priority.
905+
pathStack := strings.Split(pathStackEnv, ":")
906+
nixEnvPathKey := "DEVBOX_NIX_ENV_PATH_" + d.projectDirHash()
907+
if !lo.Contains(pathStack, nixEnvPathKey) {
908+
// if pathStack does not contain this project's nixEnvPath already,
909+
// then we add it
910+
pathStack = append(pathStack, nixEnvPathKey)
911+
}
912+
// Store the nixEnvPath for this devbox-project. This lets us replace this sub-path in the eventual PATH,
913+
// without affecting the sub-paths from other devbox-projects.
914+
env[nixEnvPathKey] = nixEnvPath
915+
env["PATH_STACK"] = strings.Join(pathStack, ":")
916+
917+
// Look up the path-list for each path-stack element, and join them together to get the final PATH.
918+
pathLists := lo.Map(pathStack, func(part string, idx int) string { return env[part] })
919+
env["PATH"] = JoinPathLists(lo.Reverse(pathLists)...) // reverse to give priority to the most recent project
920+
905921
debug.Log("computed environment PATH is: %s", env["PATH"])
906922

907923
d.setCommonHelperEnvVars(env)

0 commit comments

Comments
 (0)