@@ -140,8 +140,8 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
140
140
// --print_env must be the first argument.
141
141
if len (args ) > 0 && args [0 ] == "--print_env" {
142
142
// print environment variables for sub-processes
143
- cmd := makeBazelCmd (bazelPath , args , nil , config )
144
- for _ , val := range cmd . Env {
143
+ _ , _ , env := makeBazelCmd (bazelPath , args , config )
144
+ for _ , val := range env {
145
145
fmt .Println (val )
146
146
}
147
147
return 0 , nil
@@ -198,7 +198,7 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
198
198
}
199
199
}
200
200
201
- exitCode , err := runBazel (bazelPath , args , nil , config )
201
+ exitCode , err := execBazel (bazelPath , args , config )
202
202
if err != nil {
203
203
return - 1 , fmt .Errorf ("could not run Bazel: %v" , err )
204
204
}
@@ -526,46 +526,64 @@ func maybeDelegateToWrapper(bazel string, config config.Config) string {
526
526
return maybeDelegateToWrapperFromDir (bazel , wd , config )
527
527
}
528
528
529
- func prependDirToPathList (cmd * exec. Cmd , dir string ) {
529
+ func prependDirToPathList (env [] string , dir string ) {
530
530
found := false
531
- for idx , val := range cmd . Env {
531
+ for idx , val := range env {
532
532
splits := strings .Split (val , "=" )
533
533
if len (splits ) != 2 {
534
534
continue
535
535
}
536
536
if strings .EqualFold (splits [0 ], "PATH" ) {
537
537
found = true
538
- cmd . Env [idx ] = fmt .Sprintf ("PATH=%s%s%s" , dir , string (os .PathListSeparator ), splits [1 ])
538
+ env [idx ] = fmt .Sprintf ("PATH=%s%s%s" , dir , string (os .PathListSeparator ), splits [1 ])
539
539
break
540
540
}
541
541
}
542
542
543
543
if ! found {
544
- cmd . Env = append (cmd . Env , fmt .Sprintf ("PATH=%s" , dir ))
544
+ env = append (env , fmt .Sprintf ("PATH=%s" , dir ))
545
545
}
546
546
}
547
547
548
- func makeBazelCmd (bazel string , args []string , out io. Writer , config config.Config ) * exec. Cmd {
548
+ func makeBazelCmd (bazel string , args []string , config config.Config ) ( string , [] string , [] string ) {
549
549
execPath := maybeDelegateToWrapper (bazel , config )
550
550
551
- cmd := exec .Command (execPath , args ... )
552
- cmd .Env = append (os .Environ (), skipWrapperEnv + "=true" )
551
+ env := append (os .Environ (), skipWrapperEnv + "=true" )
553
552
if execPath != bazel {
554
- cmd . Env = append (cmd . Env , fmt .Sprintf ("%s=%s" , bazelReal , bazel ))
553
+ env = append (env , fmt .Sprintf ("%s=%s" , bazelReal , bazel ))
555
554
}
556
- prependDirToPathList (cmd , filepath .Dir (execPath ))
555
+ prependDirToPathList (env , filepath .Dir (execPath ))
556
+
557
+ commandLine := []string {execPath }
558
+ commandLine = append (commandLine , args ... )
559
+ return execPath , commandLine , env
560
+ }
561
+
562
+ func execBazel (bazel string , args []string , config config.Config ) (int , error ) {
563
+ if runtime .GOOS == "windows" {
564
+ // syscall.Exec is not supported on windows
565
+ return runBazel (bazel , args , nil , config )
566
+ }
567
+
568
+ execPath , args , env := makeBazelCmd (bazel , args , config )
569
+
570
+ err := syscall .Exec (execPath , args , env )
571
+ return 1 , fmt .Errorf ("could not start Bazel: %v" , err )
572
+ }
573
+
574
+ func runBazel (bazel string , args []string , out io.Writer , config config.Config ) (int , error ) {
575
+ execPath , commandLine , env := makeBazelCmd (bazel , args , config )
576
+
577
+ cmd := exec .Command (execPath , commandLine [1 :]... )
578
+ cmd .Env = env
557
579
cmd .Stdin = os .Stdin
558
580
if out == nil {
559
581
cmd .Stdout = os .Stdout
560
582
} else {
561
583
cmd .Stdout = out
562
584
}
563
585
cmd .Stderr = os .Stderr
564
- return cmd
565
- }
566
586
567
- func runBazel (bazel string , args []string , out io.Writer , config config.Config ) (int , error ) {
568
- cmd := makeBazelCmd (bazel , args , out , config )
569
587
err := cmd .Start ()
570
588
if err != nil {
571
589
return 1 , fmt .Errorf ("could not start Bazel: %v" , err )
0 commit comments