Skip to content

Commit

Permalink
[ci_runner] Refactor ci_runner to add custom flags to .bazelrc rather…
Browse files Browse the repository at this point in the history
… than manually parsing into the command (#7001)
  • Loading branch information
maggie-lou authored Jul 10, 2024
1 parent 563ef5f commit a651dd7
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions enterprise/server/cmd/ci_runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func run() error {
}

// Write default bazelrc
if err := writeBazelrc(buildbuddyBazelrcPath, buildEventReporter.invocationID); err != nil {
if err := writeBazelrc(buildbuddyBazelrcPath, buildEventReporter.invocationID, rootDir); err != nil {
return status.WrapError(err, "write "+buildbuddyBazelrcPath)
}
// Delete bazelrc before exiting. Use abs path since we might cd after this
Expand Down Expand Up @@ -721,7 +721,7 @@ func run() error {
if cfg != nil {
wsPath = bazelWorkspacePath(cfg)
}
args, err := bazelArgs(rootDir, wsPath, "shutdown")
args, err := bazelArgsWithCustomBazelrc(rootDir, wsPath, "shutdown")
if err != nil {
return err
}
Expand Down Expand Up @@ -1111,7 +1111,7 @@ func (ar *actionRunner) Run(ctx context.Context, ws *workspace) error {
}

iid := wfc.GetInvocation()[i].GetInvocationId()
args, err := bazelArgs(ar.rootDir, action.BazelWorkspaceDir, bazelCmd)
args, err := bazelArgsWithCustomBazelrc(ar.rootDir, action.BazelWorkspaceDir, bazelCmd)
if err != nil {
return status.InvalidArgumentErrorf("failed to parse bazel command: %s", err)
}
Expand Down Expand Up @@ -1577,21 +1577,17 @@ func printCommandLine(out io.Writer, command string, args ...string) error {
return nil
}

// TODO: Handle shell variable expansion. Probably want to run this with sh -c
func bazelArgs(rootAbsPath, bazelWorkspaceRelPath, cmd string) ([]string, error) {
// Returns the tokenized bazel command with a startup option to use the custom
// baelrc written by the ci_runner.
func bazelArgsWithCustomBazelrc(rootAbsPath, bazelWorkspaceRelPath, cmd string) ([]string, error) {
tokens, err := shlex.Split(cmd)
if err != nil {
return nil, err
}
if tokens[0] == bazelBinaryName || tokens[0] == bazeliskBinaryName {
tokens = tokens[1:]
}
startupFlags, err := shlex.Split(*bazelStartupFlags)
if err != nil {
return nil, err
}
startupFlags = append(startupFlags, "--output_base="+filepath.Join(rootAbsPath, outputBaseDirName))
startupFlags = append(startupFlags, "--bazelrc="+filepath.Join(rootAbsPath, buildbuddyBazelrcPath))
startupFlags := []string{"--bazelrc=" + filepath.Join(rootAbsPath, buildbuddyBazelrcPath)}
// Bazel will treat the user's workspace .bazelrc file with lower precedence
// than our --bazelrc, which is undesired. So instead, explicitly add the
// workspace rc as a --bazelrc flag after ours, and also set --noworkspace_rc
Expand All @@ -1607,13 +1603,6 @@ func bazelArgs(rootAbsPath, bazelWorkspaceRelPath, cmd string) ([]string, error)
if exists := (err == nil); exists {
startupFlags = append(startupFlags, "--noworkspace_rc", "--bazelrc=.bazelrc")
}
if *extraBazelArgs != "" {
extras, err := shlex.Split(*extraBazelArgs)
if err != nil {
return nil, err
}
tokens = appendBazelSubcommandArgs(tokens, extras...)
}
return append(startupFlags, tokens...), nil
}

Expand Down Expand Up @@ -2131,7 +2120,7 @@ func invocationURL(invocationID string) string {
return urlPrefix + invocationID
}

func writeBazelrc(path, invocationID string) error {
func writeBazelrc(path, invocationID, rootDir string) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
Expand Down Expand Up @@ -2203,6 +2192,24 @@ func writeBazelrc(path, invocationID string) error {
lines = append(lines, "build:buildbuddy_remote_executor --remote_executor="+*rbeBackend)
}

outputBase := filepath.Join(rootDir, outputBaseDirName)
lines = append(lines, "startup --output_base="+outputBase)
startupFlags, err := shlex.Split(*bazelStartupFlags)
if err != nil {
return status.WrapError(err, "failed to split --bazel_startup_flags")
}
for _, s := range startupFlags {
lines = append(lines, "startup "+s)
}

extras, err := shlex.Split(*extraBazelArgs)
if err != nil {
return status.WrapError(err, "failed to split --extra_bazel_args")
}
for _, e := range extras {
lines = append(lines, "common "+e)
}

contents := strings.Join(lines, "\n") + "\n"

if _, err := io.WriteString(f, contents); err != nil {
Expand Down

0 comments on commit a651dd7

Please sign in to comment.