Skip to content

Commit

Permalink
fix(gpd): Write large target patterns to file (bazel-contrib#3372)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhongpeng Lin <[email protected]>
  • Loading branch information
2 people authored and jacqueline.lee committed Jul 19, 2023
1 parent d57d3c7 commit f1b9501
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package main

import (
"bufio"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -166,7 +168,31 @@ func (b *BazelJSONBuilder) Build(ctx context.Context, mode LoadMode) ([]string,
"--aspects=" + strings.Join(aspects, ","),
"--output_groups=" + b.outputGroupsForMode(mode),
"--keep_going", // Build all possible packages
}, bazelBuildFlags, labels)
}, bazelBuildFlags)

if len(labels) < 100 {
buildArgs = append(buildArgs, labels...)
} else {
// To avoid hitting MAX_ARGS length, write labels to a file and use `--target_pattern_file`
targetsFile, err := ioutil.TempFile("", "gopackagesdriver_targets_")
if err != nil {
return nil, fmt.Errorf("unable to create target pattern file: %w", err)
}
writer := bufio.NewWriter(targetsFile)
defer writer.Flush()
for _, l := range labels {
writer.WriteString(l+"\n")
}
if err := writer.Flush(); err != nil {
return nil, fmt.Errorf("unable to flush data to target pattern file: %w", err)
}
defer func() {
targetsFile.Close()
os.Remove(targetsFile.Name())
}()

buildArgs = append(buildArgs, "--target_pattern_file="+targetsFile.Name())
}
files, err := b.bazel.Build(ctx, buildArgs...)
if err != nil {
return nil, fmt.Errorf("unable to bazel build %v: %w", buildArgs, err)
Expand Down

0 comments on commit f1b9501

Please sign in to comment.