Skip to content

Commit

Permalink
Pass startup options to bazel shutdown and bazel clean --expunge (baz…
Browse files Browse the repository at this point in the history
…elbuild#362)

* Pass startup options to bazel shutdown and bazel clean --expunge

If startup options are missing, bazel shutdown/clean commands may end up
shutdown the wrong server and clean up wrong caches if
flags like --output_user_root is set.

This is needed for fixing Windows permission denied error found at
https://buildkite.com/bazel/bazelisk-plus-incompatible-flags/builds/1246#01831fc5-a439-4894-9418-0c4d7591fcfd

* Address reviewer comment

* Address reviewer comment
  • Loading branch information
meteorcloudy authored Sep 15, 2022
1 parent 4dd53d9 commit 7fe393e
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,50 @@ func insertArgs(baseArgs []string, newArgs []string) []string {
return result
}

func shutdownIfNeeded(bazelPath string) {
func parseStartupOptions(baseArgs []string) []string {
var result []string
var BAZEL_COMMANDS = map[string]bool{
"analyze-profile": true,
"aquery": true,
"build": true,
"canonicalize-flags": true,
"clean": true,
"coverage": true,
"cquery": true,
"dump": true,
"fetch": true,
"help": true,
"info": true,
"license": true,
"mobile-install": true,
"mod": true,
"print_action": true,
"query": true,
"run": true,
"shutdown": true,
"sync": true,
"test": true,
"version": true,
}
// Arguments before a Bazel command are startup options.
for _, arg := range baseArgs {
if _, ok := BAZEL_COMMANDS[arg]; ok {
return result
}
result = append(result, arg)
}
return result
}

func shutdownIfNeeded(bazelPath string, startupOptions []string) {
bazeliskClean := GetEnvOrConfig("BAZELISK_SHUTDOWN")
if len(bazeliskClean) == 0 {
return
}

fmt.Printf("bazel shutdown\n")
exitCode, err := runBazel(bazelPath, []string{"shutdown"}, nil)
args := append(startupOptions, "shutdown")
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil)
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run bazel shutdown: %v", err)
Expand All @@ -555,14 +591,15 @@ func shutdownIfNeeded(bazelPath string) {
}
}

func cleanIfNeeded(bazelPath string) {
func cleanIfNeeded(bazelPath string, startupOptions []string) {
bazeliskClean := GetEnvOrConfig("BAZELISK_CLEAN")
if len(bazeliskClean) == 0 {
return
}

fmt.Printf("bazel clean --expunge\n")
exitCode, err := runBazel(bazelPath, []string{"clean", "--expunge"}, nil)
args := append(startupOptions, "clean", "--expunge")
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil)
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run clean: %v", err)
Expand All @@ -575,11 +612,13 @@ func cleanIfNeeded(bazelPath string) {

// migrate will run Bazel with each flag separately and report which ones are failing.
func migrate(bazelPath string, baseArgs []string, flags []string) {
var startupOptions = parseStartupOptions(baseArgs)

// 1. Try with all the flags.
args := insertArgs(baseArgs, flags)
fmt.Printf("\n\n--- Running Bazel with all incompatible flags\n\n")
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
shutdownIfNeeded(bazelPath, startupOptions)
cleanIfNeeded(bazelPath, startupOptions)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args, nil)
if err != nil {
Expand All @@ -593,8 +632,8 @@ func migrate(bazelPath string, baseArgs []string, flags []string) {
// 2. Try with no flags, as a sanity check.
args = baseArgs
fmt.Printf("\n\n--- Running Bazel with no incompatible flags\n\n")
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
shutdownIfNeeded(bazelPath, startupOptions)
cleanIfNeeded(bazelPath, startupOptions)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args, nil)
if err != nil {
Expand All @@ -611,8 +650,8 @@ func migrate(bazelPath string, baseArgs []string, flags []string) {
for _, arg := range flags {
args = insertArgs(baseArgs, []string{arg})
fmt.Printf("\n\n--- Running Bazel with %s\n\n", arg)
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
shutdownIfNeeded(bazelPath, startupOptions)
cleanIfNeeded(bazelPath, startupOptions)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args, nil)
if err != nil {
Expand Down

0 comments on commit 7fe393e

Please sign in to comment.