Skip to main content
Integrating Gazelle with Aspect Workflows streamlines the management of Bazel build files, ensuring they remain consistent and up-to-date as your project evolves. This integration is particularly beneficial for projects using specific Bazel rules, such as rules_go, as it automates the maintenance of build files like BUILD.bazel.

Gazelle integration with Bazel and Aspect Workflows

Gazelle plays a crucial role in Bazel projects, especially when you use specific rules like rules_go, because it automates managing and keeping build files like BUILD.bazel consistent. When you integrate Gazelle with Aspect Workflows, you keep your build files up-to-date and consistent, even as project dependencies change. This practice is vital for maintaining a healthy Continuous Integration/Continuous Delivery system.

The role of Gazelle

Gazelle automatically generates and maintains Bazel build files based on your source code structure and declared dependencies. This approach eliminates manual inconsistencies and reduces the effort needed to keep these files in sync with your code.

How the integration works

Aspect Workflows integrates Gazelle in two main ways:
  • Consistency Check: Aspect Workflows runs the //:gazelle.check target. This target checks for pending changes that Gazelle would make to the build files.
  • If the check fails with a non-zero exit code, it means the build files are out-of-date, and the task prints suggested changes for the developer to apply.
  • Correction: If the check fails, the developer should run the //:gazelle target. This target executes the Gazelle binary to apply the suggested changes and update the build files.
The Aspect Workflows configuration file config.yaml defines the target for the check and the target for the fix.

Enforcing additional behaviors (Optional)

In projects that use rules like rules_go, Gazelle synchronizes Bazel build rules with Go module dependencies declared in go.mod. You can extend the integration with a custom shell script like gazelle.check.sh that groups several important checks into a single gazelle.check target. This optional script not only checks for Gazelle changes but also ensures the consistency of the Go ecosystem, enforcing three essential additional checks:
  • go mod tidy: Ensures that you have run the go.mod file update that lists Go module dependencies.
  • deps.bzl Synchronization: Ensures that changes in go.mod reflect in the generated deps.bzl file.
  • No Pending Gazelle Changes: Finally, it ensures that Gazelle has no pending changes.
By running these steps as part of the CI/CD pipelines, your team immediately detects and corrects any change in the code or dependencies that affects the build or Go dependency files.