@@ -140,8 +140,8 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
140140 // --print_env must be the first argument.
141141 if len (args ) > 0 && args [0 ] == "--print_env" {
142142 // 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 {
145145 fmt .Println (val )
146146 }
147147 return 0 , nil
@@ -198,7 +198,7 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
198198 }
199199 }
200200
201- exitCode , err := runBazel (bazelPath , args , nil , config )
201+ exitCode , err := execBazel (bazelPath , args , config )
202202 if err != nil {
203203 return - 1 , fmt .Errorf ("could not run Bazel: %v" , err )
204204 }
@@ -526,46 +526,64 @@ func maybeDelegateToWrapper(bazel string, config config.Config) string {
526526 return maybeDelegateToWrapperFromDir (bazel , wd , config )
527527}
528528
529- func prependDirToPathList (cmd * exec. Cmd , dir string ) {
529+ func prependDirToPathList (env [] string , dir string ) {
530530 found := false
531- for idx , val := range cmd . Env {
531+ for idx , val := range env {
532532 splits := strings .Split (val , "=" )
533533 if len (splits ) != 2 {
534534 continue
535535 }
536536 if strings .EqualFold (splits [0 ], "PATH" ) {
537537 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 ])
539539 break
540540 }
541541 }
542542
543543 if ! found {
544- cmd . Env = append (cmd . Env , fmt .Sprintf ("PATH=%s" , dir ))
544+ env = append (env , fmt .Sprintf ("PATH=%s" , dir ))
545545 }
546546}
547547
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 ) {
549549 execPath := maybeDelegateToWrapper (bazel , config )
550550
551- cmd := exec .Command (execPath , args ... )
552- cmd .Env = append (os .Environ (), skipWrapperEnv + "=true" )
551+ env := append (os .Environ (), skipWrapperEnv + "=true" )
553552 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 ))
555554 }
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
557579 cmd .Stdin = os .Stdin
558580 if out == nil {
559581 cmd .Stdout = os .Stdout
560582 } else {
561583 cmd .Stdout = out
562584 }
563585 cmd .Stderr = os .Stderr
564- return cmd
565- }
566586
567- func runBazel (bazel string , args []string , out io.Writer , config config.Config ) (int , error ) {
568- cmd := makeBazelCmd (bazel , args , out , config )
569587 err := cmd .Start ()
570588 if err != nil {
571589 return 1 , fmt .Errorf ("could not start Bazel: %v" , err )
0 commit comments