init
The init command initializes a Bazel project with the necessary configuration for Gazelle. It detects languages in your project and sets up the required dependencies.
Usage
bazelle init [flags] [path]Flags
| Flag | Description |
|---|---|
-l, --languages | Languages to configure (auto-detected if not specified) |
--name | Module name (defaults to directory name) |
--check | Check if project is properly configured (exit 1 if not) |
--dry-run | Show what would change without applying |
What It Does
-
Detects languages used in your project by scanning file extensions
-
Creates MODULE.bazel with required dependencies:
bazel_skylibrules_goandgazelle(always included)- Language-specific rules (rules_kotlin, rules_python, etc.)
-
Creates BUILD.bazel with a gazelle target
Examples
Basic Initialization
Initialize the current directory:
bazelle initExample output:
Languages: go, kotlin, pythonCreated /path/to/project/MODULE.bazelCreated /path/to/project/BUILD.bazel
Next steps: 1. Update the gazelle:prefix directive in BUILD.bazel 2. Run 'bazelle update' to generate BUILD filesSpecify Languages
Override auto-detection:
bazelle init --languages go,kotlinCustom Module Name
Set a specific module name:
bazelle init --name myprojectPreview Changes
See what would be created:
bazelle init --dry-runCI Check
Verify project is properly configured:
bazelle init --checkGenerated Files
MODULE.bazel
module( name = "myproject", version = "0.1.0",)
# Core dependenciesbazel_dep(name = "bazel_skylib", version = "1.9.0")
# Language supportbazel_dep(name = "gazelle", version = "0.47.0")bazel_dep(name = "rules_go", version = "0.59.0")bazel_dep(name = "rules_kotlin", version = "2.2.2")bazel_dep(name = "rules_python", version = "1.8.0")bazel_dep(name = "rules_python_gazelle_plugin", version = "1.6.3")BUILD.bazel
load("@gazelle//:def.bzl", "gazelle")
# gazelle:prefix github.com/your-org/your-repo
gazelle( name = "gazelle",)Language Detection
init detects languages by scanning for common file extensions:
| Extensions | Language |
|---|---|
.go | Go |
.kt, .kts | Kotlin |
.py | Python |
.c, .cc, .cpp, .h, .hpp | C/C++ |
.proto | Protocol Buffers |
.groovy | Groovy |
Existing Files
If MODULE.bazel or BUILD.bazel already exist, init will not overwrite them:
MODULE.bazel already exists (skipping)BUILD.bazel already exists (skipping)Use --check to verify existing configuration has required dependencies.
Exit Codes
| Code | Mode | Meaning |
|---|---|---|
| 0 | Normal | Initialization completed |
| 0 | --check | Project is properly configured |
| 1 | --check | Configuration issues found |
| 1 | Any | Error occurred |
After Initialization
-
Edit BUILD.bazel to set the correct
gazelle:prefix:# gazelle:prefix github.com/your-org/your-repo -
Run update to generate BUILD files:
Terminal window bazelle update -
Build and test:
Terminal window bazel build //...bazel test //...