9
9
"fmt"
10
10
"io"
11
11
"io/fs"
12
+ "maps"
12
13
"os"
13
14
"os/exec"
14
15
"path/filepath"
@@ -21,6 +22,7 @@ import (
21
22
"github.com/pkg/errors"
22
23
"github.com/samber/lo"
23
24
"go.jetpack.io/devbox/internal/devpkg"
25
+ "go.jetpack.io/devbox/internal/impl/envpath"
24
26
"go.jetpack.io/devbox/internal/impl/generate"
25
27
"go.jetpack.io/devbox/internal/searcher"
26
28
"go.jetpack.io/devbox/internal/shellgen"
@@ -59,6 +61,7 @@ type Devbox struct {
59
61
nix nix.Nixer
60
62
projectDir string
61
63
pluginManager * plugin.Manager
64
+ preservePathStack bool
62
65
pure bool
63
66
allowInsecureAdds bool
64
67
customProcessComposeFile string
@@ -89,6 +92,7 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
89
92
projectDir : projectDir ,
90
93
pluginManager : plugin .NewManager (),
91
94
stderr : opts .Stderr ,
95
+ preservePathStack : opts .PreservePathStack ,
92
96
pure : opts .Pure ,
93
97
customProcessComposeFile : opts .CustomProcessComposeFile ,
94
98
allowInsecureAdds : opts .AllowInsecureAdds ,
@@ -317,7 +321,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
317
321
if err != nil {
318
322
return nil , err
319
323
}
320
- return mapToPairs (envs ), nil
324
+ return envir . MapToPairs (envs ), nil
321
325
}
322
326
323
327
func (d * Devbox ) ShellEnvHash (ctx context.Context ) (string , error ) {
@@ -782,18 +786,10 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
782
786
}
783
787
}
784
788
785
- currentEnvPath := env ["PATH" ]
786
- debug .Log ("current environment PATH is: %s" , currentEnvPath )
787
- // Use the original path, if available. If not available, set it for future calls.
788
- // See https://github.com/jetpack-io/devbox/issues/687
789
- // We add the project dir hash to ensure that we don't have conflicts
790
- // between different projects (including global)
791
- // (moving a project would change the hash and that's fine)
792
- originalPath , ok := env [d .ogPathKey ()]
793
- if ! ok {
794
- env [d .ogPathKey ()] = currentEnvPath
795
- originalPath = currentEnvPath
796
- }
789
+ debug .Log ("current environment PATH is: %s" , env ["PATH" ])
790
+
791
+ originalEnv := make (map [string ]string , len (env ))
792
+ maps .Copy (originalEnv , env )
797
793
798
794
vaf , err := d .nix .PrintDevEnv (ctx , & nix.PrintDevEnvArgs {
799
795
FlakesFilePath : d .nixFlakesFilePath (),
@@ -853,13 +849,13 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
853
849
854
850
addEnvIfNotPreviouslySetByDevbox (env , pluginEnv )
855
851
856
- env ["PATH" ] = JoinPathLists (
852
+ env ["PATH" ] = envpath . JoinPathLists (
857
853
nix .ProfileBinPath (d .projectDir ),
858
854
env ["PATH" ],
859
855
)
860
856
861
857
if ! d .OmitBinWrappersFromPath {
862
- env ["PATH" ] = JoinPathLists (
858
+ env ["PATH" ] = envpath . JoinPathLists (
863
859
filepath .Join (d .projectDir , plugin .WrapperBinPath ),
864
860
env ["PATH" ],
865
861
)
@@ -899,14 +895,18 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
899
895
})
900
896
debug .Log ("PATH after filtering with buildInputs (%v) is: %s" , buildInputs , nixEnvPath )
901
897
902
- env ["PATH" ] = JoinPathLists (nixEnvPath , originalPath )
898
+ pathStack := envpath .Stack (env , originalEnv )
899
+ pathStack .Push (env , d .projectDirHash (), nixEnvPath , d .preservePathStack )
900
+ env ["PATH" ] = pathStack .Path (env )
901
+ debug .Log ("New path stack is: %s" , pathStack )
902
+
903
903
debug .Log ("computed environment PATH is: %s" , env ["PATH" ])
904
904
905
905
d .setCommonHelperEnvVars (env )
906
906
907
907
if ! d .pure {
908
908
// preserve the original XDG_DATA_DIRS by prepending to it
909
- env ["XDG_DATA_DIRS" ] = JoinPathLists (env ["XDG_DATA_DIRS" ], os .Getenv ("XDG_DATA_DIRS" ))
909
+ env ["XDG_DATA_DIRS" ] = envpath . JoinPathLists (env ["XDG_DATA_DIRS" ], os .Getenv ("XDG_DATA_DIRS" ))
910
910
}
911
911
912
912
for k , v := range d .env {
@@ -941,10 +941,6 @@ func (d *Devbox) nixEnv(ctx context.Context) (map[string]string, error) {
941
941
return d .computeNixEnv (ctx , usePrintDevEnvCache )
942
942
}
943
943
944
- func (d * Devbox ) ogPathKey () string {
945
- return "DEVBOX_OG_PATH_" + d .projectDirHash ()
946
- }
947
-
948
944
func (d * Devbox ) nixPrintDevEnvCachePath () string {
949
945
return filepath .Join (d .projectDir , ".devbox/.nix-print-dev-env-cache" )
950
946
}
@@ -1120,8 +1116,8 @@ var ignoreDevEnvVar = map[string]bool{
1120
1116
// common setups (e.g. gradio, rust)
1121
1117
func (d * Devbox ) setCommonHelperEnvVars (env map [string ]string ) {
1122
1118
profileLibDir := filepath .Join (d .projectDir , nix .ProfilePath , "lib" )
1123
- env ["LD_LIBRARY_PATH" ] = JoinPathLists (profileLibDir , env ["LD_LIBRARY_PATH" ])
1124
- env ["LIBRARY_PATH" ] = JoinPathLists (profileLibDir , env ["LIBRARY_PATH" ])
1119
+ env ["LD_LIBRARY_PATH" ] = envpath . JoinPathLists (profileLibDir , env ["LD_LIBRARY_PATH" ])
1120
+ env ["LIBRARY_PATH" ] = envpath . JoinPathLists (profileLibDir , env ["LIBRARY_PATH" ])
1125
1121
}
1126
1122
1127
1123
// NixBins returns the paths to all the nix binaries that are installed by
@@ -1197,7 +1193,7 @@ func (d *Devbox) parseEnvAndExcludeSpecialCases(currentEnv []string) (map[string
1197
1193
return nil , err
1198
1194
}
1199
1195
includedInPath = append (includedInPath , dotdevboxBinPath (d ))
1200
- env ["PATH" ] = JoinPathLists (includedInPath ... )
1196
+ env ["PATH" ] = envpath . JoinPathLists (includedInPath ... )
1201
1197
}
1202
1198
return env , nil
1203
1199
}
0 commit comments