Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co

// RunBazeliskWithArgsFuncAndConfigAndOut runs the main Bazelisk logic for the given ArgsFunc and Bazel
// repositories and config, writing its stdout to the passed writer.
func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositories, config config.Config, out io.Writer) (int, error) {
func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositories, config config.Config, stdout io.Writer) (int, error) {
return RunBazeliskWithArgsFuncAndConfigAndOutAndErr(argsFunc, repos, config, stdout, nil)
}

// RunBazeliskWithArgsFuncAndConfigAndOutAndErr runs the main Bazelisk logic for the given ArgsFunc and Bazel
// repositories and config, writing its stdout and stderr to the passed writer.
func RunBazeliskWithArgsFuncAndConfigAndOutAndErr(argsFunc ArgsFunc, repos *Repositories, config config.Config, stdout, stderr io.Writer) (int, error) {
httputil.UserAgent = getUserAgent(config)

bazelInstallation, err := GetBazelInstallation(repos, config)
Expand All @@ -111,7 +117,7 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori
// --print_env must be the first argument.
if len(args) > 0 && args[0] == "--print_env" {
// print environment variables for sub-processes
cmd, err := makeBazelCmd(bazelInstallation.Path, args, nil, config)
cmd, err := makeBazelCmd(bazelInstallation.Path, args, nil, nil, config)
if err != nil {
return -1, err
}
Expand Down Expand Up @@ -180,7 +186,7 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori
return 0, nil
}

exitCode, err := runBazel(bazelInstallation.Path, args, out, config)
exitCode, err := runBazel(bazelInstallation.Path, args, stdout, stderr, config)
if err != nil {
return -1, fmt.Errorf("could not run Bazel: %v", err)
}
Expand Down Expand Up @@ -692,7 +698,7 @@ func prependDirToPathList(cmd *exec.Cmd, dir string) {
}
}

func makeBazelCmd(bazel string, args []string, out io.Writer, config config.Config) (*exec.Cmd, error) {
func makeBazelCmd(bazel string, args []string, stdout, stderr io.Writer, config config.Config) (*exec.Cmd, error) {
execPath := maybeDelegateToWrapper(bazel, config)

var cmd *exec.Cmd
Expand All @@ -718,17 +724,21 @@ func makeBazelCmd(bazel string, args []string, out io.Writer, config config.Conf
}
prependDirToPathList(cmd, filepath.Dir(execPath))
cmd.Stdin = os.Stdin
if out == nil {
if stdout == nil {
cmd.Stdout = os.Stdout
} else {
cmd.Stdout = out
cmd.Stdout = stdout
}
if stderr == nil {
cmd.Stderr = os.Stderr
} else {
cmd.Stderr = stderr
}
cmd.Stderr = os.Stderr
return cmd, nil
}

func runBazel(bazel string, args []string, out io.Writer, config config.Config) (int, error) {
cmd, makeCmdErr := makeBazelCmd(bazel, args, out, config)
func runBazel(bazel string, args []string, stdout, stderr io.Writer, config config.Config) (int, error) {
cmd, makeCmdErr := makeBazelCmd(bazel, args, stdout, stderr, config)
if makeCmdErr != nil {
return 1, makeCmdErr
}
Expand Down Expand Up @@ -776,7 +786,7 @@ func getIncompatibleFlags(bazelPath, cmd string, config config.Config) ([]string
}

out := strings.Builder{}
if _, err := runBazel(bazelPath, []string{"help", cmd, "--short"}, &out, config); err != nil {
if _, err := runBazel(bazelPath, []string{"help", cmd, "--short"}, &out, nil, config); err != nil {
return nil, fmt.Errorf("unable to determine incompatible flags with binary %s: %v", bazelPath, err)
}

Expand Down Expand Up @@ -852,7 +862,7 @@ func shutdownIfNeeded(bazelPath string, startupOptions []string, config config.C

args := append(startupOptions, "shutdown")
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil, config)
exitCode, err := runBazel(bazelPath, args, nil, nil, config)
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run bazel shutdown: %v", err)
Expand All @@ -871,7 +881,7 @@ func cleanIfNeeded(bazelPath string, startupOptions []string, config config.Conf

args := append(startupOptions, "clean", "--expunge")
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil, config)
exitCode, err := runBazel(bazelPath, args, nil, nil, config)
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run clean: %v", err)
Expand Down Expand Up @@ -1070,7 +1080,7 @@ func testWithBazelAtCommit(bazelCommit string, args []string, bazeliskHome strin
shutdownIfNeeded(bazelPath, startupOptions, config)
cleanIfNeeded(bazelPath, startupOptions, config)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
bazelExitCode, err := runBazel(bazelPath, args, nil, config)
bazelExitCode, err := runBazel(bazelPath, args, nil, nil, config)
if err != nil {
return -1, fmt.Errorf("could not run Bazel: %v", err)
}
Expand All @@ -1087,7 +1097,7 @@ func migrate(bazelPath string, baseArgs []string, flags []string, config config.
shutdownIfNeeded(bazelPath, startupOptions, config)
cleanIfNeeded(bazelPath, startupOptions, config)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil, config)
exitCode, err := runBazel(bazelPath, args, nil, nil, config)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
Expand All @@ -1102,7 +1112,7 @@ func migrate(bazelPath string, baseArgs []string, flags []string, config config.
shutdownIfNeeded(bazelPath, startupOptions, config)
cleanIfNeeded(bazelPath, startupOptions, config)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args, nil, config)
exitCode, err = runBazel(bazelPath, args, nil, nil, config)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
Expand All @@ -1120,7 +1130,7 @@ func migrate(bazelPath string, baseArgs []string, flags []string, config config.
shutdownIfNeeded(bazelPath, startupOptions, config)
cleanIfNeeded(bazelPath, startupOptions, config)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args, nil, config)
exitCode, err = runBazel(bazelPath, args, nil, nil, config)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
Expand Down