# bazel go hygiene

> Keep Bazel Go repositories green by automating Gazelle, Buildifier, and bzlmod dependency synchronization.

bazel go hygiene is a free AI agent skill on Agensi by Omar Gallo, in APIs & Backend. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [Omar Gallo](https://www.agensi.io/creators/omargallob)
- Category: [APIs & Backend](https://www.agensi.io/skills/apis-backend)
- Uses: 3
- Version: 1.0
- Last updated: 2026-10-15
- Security: scan passed (2026-10-15); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/bazel-go-hygiene
- Tags: api, code-review, devops, bazel, golang, build-systems

## About this skill

Maintaining a Bazel-managed Go repository often feels like fighting two build systems at once. Code compiles with the Go toolchain but fails in Bazel due to stale BUILD files, missing dependencies in `MODULE.bazel`, or nogo static analysis failures. Manual edits to `use_repo` lists or `deps` arrays lead to brittle repositories that break in CI.

This skill automates the hygiene required to keep `rules_go`, `gazelle`, and `bzlmod` in sync. It ensures your `go.mod` changes propagate correctly to your Bazel workspace and that your Starlark files remain formatted.

### What it does
- **Synchronizes dependencies** by mapping `go.mod` updates to `MODULE.bazel` via `bazel mod tidy`.
- **Regenerates BUILD files** using Gazelle to track added, removed, or renamed Go files and imports.
- **Enforces formatting** across `BUILD.bazel`, `.bzl`, and `MODULE.bazel` files using Buildifier.
- **Validates builds** against the source of truth, ensuring `nogo` static analysis passes within the Bazel sandbox.
- **Resolves pathing issues** for transitive dependencies and OS/CPU specific `select()` statements.

### How it works
1. **Analyze changes** to identify if the user updated Go source, dependencies, or Bazel configuration.
2. **Execute hygiene pipeline** by running the sequence of `go mod tidy`, `bazel mod tidy`, and `gazelle`.
3. **Audit against nogo** to catch static analysis failures that `go build` ignores but Bazel rejects.
4. **Format Starlark** to ensure all configuration files meet the repository's style standards.

### Frameworks & tools
Bazel, Go, `rules_go`, Gazelle, Buildifier, `bzlmod`, and `nogo` static analysis.

### Why this beats prompting it yourself
Generic AI agents often suggest standard Go commands that bypass Bazel's sandbox. This skill understands the specific requirements of `bzlmod` and the `use_repo` extension, preventing the common "no such package" errors that occur when manually editing Bazel files.

### Use cases
- Adding new internal Go packages and generating their corresponding `go_library` targets.
- Upgrading third-party dependencies and updating the `go_deps` extension in `MODULE.bazel`.
- Fixing CI failures where Gazelle indicates a diff between source code and BUILD files.
- Implementing OS-specific build tags or platform-dependent dependencies correctly.

### Known limitations
Requires a functional Bazel installation and a pre-configured `gazelle` target in the root `BUILD.bazel` file. Hermetic Go SDK settings may require a matching host Go version for local linting.

## Requires

- bazel
- golang

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the bazel go hygiene skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### How does this skill help manage the friction between go.mod and Bazel?

This skill automates the synchronization between Go's standard module system and Bazel's build graph, handling repetitive tasks like updating `MODULE.bazel` extensions and fixing stale BUILD files that typically break CI pipelines.

### Does this skill support modern Bazel projects using bzlmod?

Yes, the skill is fully compatible with bzlmod and specifically handles the complexities of `bazel mod tidy` and the `go_deps` extension to ensure transitive dependencies are correctly mapped.

### What are the prerequisites for using this skill in my repository?

You will need a functional Bazel installation and a root-level Gazelle target; the skill will then automatically detect your configuration to run the appropriate hygiene pipeline.

### Why should I use this skill instead of a general-purpose AI agent for my build files?

Unlike generic AI, this skill understands Bazel-specific nuances like `nogo` static analysis and `select()` statements, preventing common sandbox errors that standard Go-focused agents often overlook.

### How does the skill handle dependency updates?

The skill analyzes your `go.mod` and source code changes, then executes a sequenced pipeline of `go mod tidy`, `gazelle`, and `buildifier` to ensure your workspace remains in a buildable state.

### Which AI agents or environments can run this skill?

The skill is designed for any AI agent capable of executing shell commands or interacting with local file systems, provided it has access to the Bazel binary.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
