@@ -12,7 +12,6 @@ import (
12
12
"os"
13
13
"os/exec"
14
14
"path/filepath"
15
- "sort"
16
15
"strings"
17
16
"text/template"
18
17
@@ -58,7 +57,7 @@ type DevboxShell struct {
58
57
binPath string
59
58
projectDir string // path to where devbox.json config resides
60
59
pkgConfigDir string
61
- env [ ]string
60
+ env map [ string ]string
62
61
userShellrcPath string
63
62
pluginInitHook string
64
63
@@ -218,15 +217,7 @@ func WithHistoryFile(historyFile string) ShellOption {
218
217
// via wrapper scripts.
219
218
func WithEnvVariables (envVariables map [string ]string ) ShellOption {
220
219
return func (s * DevboxShell ) {
221
- keys := make ([]string , 0 , len (envVariables ))
222
- for k := range envVariables {
223
- keys = append (keys , k )
224
- }
225
- sort .Strings (keys )
226
-
227
- for _ , k := range keys {
228
- s .env = append (s .env , fmt .Sprintf ("%s=%s" , k , envVariables [k ]))
229
- }
220
+ s .env = envVariables
230
221
}
231
222
}
232
223
@@ -277,9 +268,13 @@ func (s *DevboxShell) Run() error {
277
268
// Link other files that affect the shell settings and environments.
278
269
s .linkShellStartupFiles (filepath .Dir (shellrc ))
279
270
extraEnv , extraArgs := s .shellRCOverrides (shellrc )
271
+ env := s .env
272
+ for k , v := range extraEnv {
273
+ env [k ] = v
274
+ }
280
275
281
276
cmd = exec .Command (s .binPath )
282
- cmd .Env = append ( s . env , extraEnv ... )
277
+ cmd .Env = mapToPairs ( env )
283
278
cmd .Args = append (cmd .Args , extraArgs ... )
284
279
cmd .Stdin = os .Stdin
285
280
cmd .Stdout = os .Stdout
@@ -309,16 +304,16 @@ func (s *DevboxShell) Run() error {
309
304
return errors .WithStack (err )
310
305
}
311
306
312
- func (s * DevboxShell ) shellRCOverrides (shellrc string ) (extraEnv [ ]string , extraArgs []string ) {
307
+ func (s * DevboxShell ) shellRCOverrides (shellrc string ) (extraEnv map [ string ]string , extraArgs []string ) {
313
308
// Shells have different ways of overriding the shellrc, so we need to
314
309
// look at the name to know which env vars or args to set when launching the shell.
315
310
switch s .name {
316
311
case shBash :
317
312
extraArgs = []string {"--rcfile" , shellescape .Quote (shellrc )}
318
313
case shZsh :
319
- extraEnv = [ ]string {fmt . Sprintf ( ` ZDOTDIR=%s` , shellescape .Quote (filepath .Dir (shellrc ) ))}
314
+ extraEnv = map [ string ]string {" ZDOTDIR" : shellescape .Quote (filepath .Dir (shellrc ))}
320
315
case shKsh , shPosix :
321
- extraEnv = [ ]string {fmt . Sprintf ( ` ENV=%s` , shellescape .Quote (shellrc ) )}
316
+ extraEnv = map [ string ]string {" ENV" : shellescape .Quote (shellrc )}
322
317
case shFish :
323
318
extraArgs = []string {"-C" , ". " + shellrc }
324
319
}
@@ -369,29 +364,6 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
369
364
tmpl = fishrcTmpl
370
365
}
371
366
372
- exportEnv := ""
373
- strb := strings.Builder {}
374
- for _ , kv := range s .env {
375
- k , v , ok := strings .Cut (kv , "=" )
376
- if ! ok {
377
- continue
378
- }
379
- strb .WriteString ("export " )
380
- strb .WriteString (k )
381
- strb .WriteString (`="` )
382
- for _ , r := range v {
383
- switch r {
384
- // Special characters inside double quotes:
385
- // https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_02_03
386
- case '$' , '`' , '"' , '\\' , '\n' :
387
- strb .WriteRune ('\\' )
388
- }
389
- strb .WriteRune (r )
390
- }
391
- strb .WriteString ("\" \n " )
392
- }
393
- exportEnv = strings .TrimSpace (strb .String ())
394
-
395
367
err = tmpl .Execute (shellrcf , struct {
396
368
ProjectDir string
397
369
OriginalInit string
@@ -413,7 +385,7 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
413
385
ScriptCommand : strings .TrimSpace (s .ScriptCommand ),
414
386
ShellStartTime : s .shellStartTime ,
415
387
HistoryFile : strings .TrimSpace (s .historyFile ),
416
- ExportEnv : exportEnv ,
388
+ ExportEnv : exportify ( s . env ) ,
417
389
})
418
390
if err != nil {
419
391
return "" , fmt .Errorf ("execute shellrc template: %v" , err )
0 commit comments