Skip to content

Use bazel cquery to find out the binary path instead of guessing#67

Open
apesternikov wants to merge 15 commits into
mainfrom
ap/query_bins
Open

Use bazel cquery to find out the binary path instead of guessing#67
apesternikov wants to merge 15 commits into
mainfrom
ap/query_bins

Conversation

@apesternikov
Copy link
Copy Markdown
Contributor

@apesternikov apesternikov commented Jun 3, 2026

We were using heuristics to guess a binary for a target.
It is possible to use cquery to get the exact path instead of guessing.
As a side effect of this change it does not depend on protobuf anymore so we did a cleanup
Also, timeout on git operation. github seems to get stuck on git push sometimes.

Comment thread gitops/exec/exec.go
Comment on lines +35 to +51
func ExWithTimeout(timeout time.Duration, dir, name string, arg ...string) (output string, err error) {
log.Println("executing:", name, strings.Join(arg, " "))
var cmd *exec.Cmd
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd = exec.CommandContext(ctx, name, arg...)
} else {
cmd = exec.Command(name, arg...)
}
if dir != "" {
cmd.Dir = dir
}
b, err := cmd.CombinedOutput()
log.Printf("%s", string(b))
return string(b), err
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ExWithTimeout, errors resulting from context timeout or cancellation are not distinguished from other errors. This can make it difficult to diagnose whether a failure was due to a timeout or another issue. Consider checking if the error is context.DeadlineExceeded or context.Canceled and returning a more descriptive error message.

Recommended solution:

if ctx.Err() != nil {
    return string(b), fmt.Errorf("command timed out or was canceled: %w", ctx.Err())
}

Comment thread gitops/exec/exec.go
Comment thread gitops/git/git_test.go
Comment thread gitops/prer/create_gitops_prs.go
Comment on lines 88 to 161
flag.StringVar(&gitopsdir, "gitopsdir", "", "do not use temporary directory for gitops, use this directory instead")
}

func bazelQuery(query string) *analysis.CqueryResult {
type GitopsTarget struct {
Target string `json:"target"`
Binary string `json:"executable"`
DeploymentBranch string `json:"deployment_branch"`
}

func cleanTarget(target string) string {
if strings.HasPrefix(target, "@@//") {
return target[2:]
}
if strings.HasPrefix(target, "@//") {
return target[1:]
}
return target
}

func bazelQueryTargets(query string) []GitopsTarget {
log.Println("Executing bazel cquery ", query)
cmd := oe.Command(*bazelCmd, "cquery", query, "--output=proto")
starlarkExpr := `json.encode(struct(deployment_branch = getattr(providers(target)['//gitops:provider.bzl%GitopsArtifactsInfo'], 'deployment_branch', '') if '//gitops:provider.bzl%GitopsArtifactsInfo' in providers(target) else '', target = str(target.label), executable = target.files_to_run.executable.path if target.files_to_run.executable else ''))`
cmd := oe.Command(*bazelCmd, "cquery", "--implicit_deps=false", query, "--output=starlark", "--starlark:expr="+starlarkExpr)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
go func() {
io.Copy(os.Stderr, stderr)
}()
buildproto, err := cmd.Output()
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
var targets []GitopsTarget
for _, line := range strings.Split(string(out), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
var gt GitopsTarget
if err := json.Unmarshal([]byte(line), &gt); err != nil {
log.Fatalf("failed to unmarshal JSON line %q: %v", line, err)
}
targets = append(targets, gt)
}
return targets
}

func bazelQueryPaths(query string) []string {
log.Println("Executing bazel cquery ", query)
cmd := oe.Command(*bazelCmd, "cquery", "--implicit_deps=false", query, "--output=starlark", `--starlark:expr=target.files.to_list()[0].path if target.files.to_list() else ''`)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
qr := &analysis.CqueryResult{}
if err := proto.Unmarshal(buildproto, qr); err != nil {
go func() {
io.Copy(os.Stderr, stderr)
}()
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return qr
var paths []string
for _, line := range strings.Split(string(out), "\n") {
line = strings.TrimSpace(line)
if line != "" {
paths = append(paths, line)
}
}
return paths
}

func main() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Abrupt Process Termination on Errors

Throughout the code (e.g., in bazelQueryTargets, bazelQueryPaths, and the main release train logic), errors are handled using log.Fatal or exec.Mustex, which abruptly terminate the process. This approach prevents graceful error handling, makes it difficult to report partial results, and complicates debugging in batch or CI environments.

Recommendation:

  • Refactor functions to return errors instead of calling log.Fatal or using Mustex.
  • In the main logic, aggregate and report errors, allowing the process to continue where possible or to provide a comprehensive error report at the end.

Example:

func bazelQueryTargets(query string) ([]GitopsTarget, error) {
    // ...
    if err != nil {
        return nil, err
    }
    // ...
}

This enables more robust and maintainable error handling.

Comment thread gitops/prer/prer_test.sh
Comment on lines +65 to +67
escaped_workspace=$(echo "$WORKSPACE_ROOT" | sed 's/\./\\./g')
escaped_bin=$(echo "$push_bin" | sed 's/\./\\./g')
pattern="Skipping execution of $escaped_workspace/bazel-out/[^/]+/bin/$escaped_bin in "
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragile Regex Pattern for Push Binary Verification

The regex pattern constructed for verifying push binaries (lines 65-67) only escapes periods (.) in the workspace and binary paths. If either path contains other regex special characters (such as +, *, ?, (, ), [, ], {, }, |, ^, $), the pattern may not match as intended, leading to false negatives or positives.

Recommendation:
Use a more robust escaping mechanism for all regex special characters. For example, use sed -e 's/[]\^$.|?*+(){}[]/\\&/g' to escape all regex metacharacters:

escaped_workspace=$(echo "$WORKSPACE_ROOT" | sed -e 's/[]\\^$.|?*+(){}[]/\\&/g')
escaped_bin=$(echo "$push_bin" | sed -e 's/[]\\^$.|?*+(){}[]/\\&/g')

This will ensure the pattern matches the intended literal paths.

Comment thread gitops/prer/prer_test.sh
Comment on lines 187 to +191
releaseTrain, bin, found := strings.Cut(rb, ":")
if !found {
log.Fatalf("resolved_binaries: invalid resolved_binary format: %s", rb)
}
releaseTrains[releaseTrain] = append(releaseTrains[releaseTrain], bin)
releaseTrains[releaseTrain] = append(releaseTrains[releaseTrain], GitopsTarget{Target: bin, Binary: bin})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential Issue: Incorrect Parsing of Resolved Binaries

The parsing logic for resolvedBinaries uses strings.Cut(rb, ":"), which only splits on the first colon. If the binary path contains additional colons (e.g., in Windows paths or container image references), this may lead to incorrect parsing and assignment.

Recommendation:

  • Use a more robust parsing method, such as splitting only on the first colon and treating the remainder as the binary path, or validating the input format more strictly.

Example:

releaseTrain, bin, found := strings.Cut(rb, ":")
if !found || releaseTrain == "" || bin == "" {
    log.Fatalf("resolved_binaries: invalid resolved_binary format: %s", rb)
}

Or use regex for stricter validation.

Comment on lines 248 to 254
msg := workdir.GetLastCommitMessage()
targetset := make(map[string]bool)
for _, t := range targets {
targetset[t] = true
targetset[t.Target] = true
}
oldtargets := commitmsg.ExtractTargets(msg)
for _, t := range oldtargets {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic Issue: Branch Recreation May Not Handle All Deleted Targets

The branch recreation logic only recreates the branch if the first missing target is found, then breaks. If multiple targets are deleted, only the first is considered, and the branch is recreated once. This may not fully synchronize the branch state with the current targets, potentially leaving stale commits or metadata.

Recommendation:

  • After recreating the branch, ensure that all deleted targets are handled and the branch state is fully synchronized. Consider re-checking the target set after recreation or performing a full reset.

Example:

for _, t := range oldtargets {
    if !targetset[t] {
        workdir.RecreateBranch(branch, *prInto)
        // Optionally, re-check or reset branch state here
        break
    }
}

Ensure that the branch reflects the current set of targets after recreation.

Comment thread gitops/git/git_test.go
…pty WORKSPACE file at the root of the workspace. This maintains compatibility with older tooling (including older versions of Bazelisk, IDE plugins, and third-party tools) that expect a WORKSPACE file to detect the project boundaries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant