Skip to content

fix

The fix command runs Gazelle in fix mode, which may make breaking changes such as deleting obsolete rules or renaming targets.

Usage

Terminal window
bazelle fix [flags] [path...]

Flags

FlagDescription
--checkCheck if BUILD files need fixing (exit 1 if changes needed)
--dry-runShow what would change without applying
--verboseShow detailed output

Examples

Basic Fix

Fix all BUILD files in the workspace:

Terminal window
bazelle fix

Fix a specific directory:

Terminal window
bazelle fix ./src/mypackage

Preview Changes

See what would change without applying:

Terminal window
bazelle fix --dry-run

Example output:

--- src/old/BUILD.bazel
+++ src/old/BUILD.bazel
@@ -1,10 +1,3 @@
load("@rules_go//go:def.bzl", "go_library")
-go_library(
- name = "deleted_package",
- srcs = ["deleted.go"],
- importpath = "github.com/org/repo/src/old",
- visibility = ["//visibility:public"],
-)
-
go_library(
name = "old",

CI Integration

Check if BUILD files need fixing:

Terminal window
bazelle fix --check

Verbose Mode

Show detailed information about changes:

Terminal window
bazelle fix --verbose

When to Use fix vs update

ScenarioCommand
Added new source filesupdate
Modified existing filesupdate
Renamed source filesfix
Deleted source filesfix
Changed package structurefix
Migrating rule typesfix

What fix Does

The fix command may:

  1. Delete obsolete rules: Removes targets for deleted source files
  2. Rename targets: Updates target names to match new file names
  3. Update load statements: Removes unused loads, adds missing ones
  4. Reorganize rules: Consolidates split rules when appropriate

Passthrough Flags

Like update, unrecognized flags are passed to Gazelle:

Terminal window
bazelle fix -go_prefix=github.com/org/repo

Exit Codes

CodeModeMeaning
0NormalFix completed successfully
0--checkBUILD files are up to date
1--checkBUILD files need fixing
1AnyError occurred

Safe Workflow

When making significant changes to your codebase:

Terminal window
# 1. Preview what fix would do
bazelle fix --dry-run
# 2. Review the changes
# 3. Apply if they look correct
bazelle fix
# 4. Build to verify
bazel build //...
# 5. Run tests
bazel test //...