@@ -786,16 +786,14 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
786
786
787
787
currentEnvPath := env ["PATH" ]
788
788
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" ]
795
791
if ! ok {
792
+ // if path stack is empty, then set the first element
793
+ pathStackEnv = d .ogPathKey ()
796
794
env [d .ogPathKey ()] = currentEnvPath
797
- originalPath = currentEnvPath
798
795
}
796
+ debug .Log ("path stack is: %s" , pathStackEnv )
799
797
800
798
vaf , err := d .nix .PrintDevEnv (ctx , & nix.PrintDevEnvArgs {
801
799
FlakesFilePath : d .nixFlakesFilePath (),
@@ -901,7 +899,25 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
901
899
})
902
900
debug .Log ("PATH after filtering with buildInputs (%v) is: %s" , buildInputs , nixEnvPath )
903
901
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
+
905
921
debug .Log ("computed environment PATH is: %s" , env ["PATH" ])
906
922
907
923
d .setCommonHelperEnvVars (env )
0 commit comments