Skip to content

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

Terminal window
bazelle init [flags] [path]

Flags

FlagDescription
-l, --languagesLanguages to configure (auto-detected if not specified)
--nameModule name (defaults to directory name)
--checkCheck if project is properly configured (exit 1 if not)
--dry-runShow what would change without applying

What It Does

  1. Detects languages used in your project by scanning file extensions

  2. Creates MODULE.bazel with required dependencies:

    • bazel_skylib
    • rules_go and gazelle (always included)
    • Language-specific rules (rules_kotlin, rules_python, etc.)
  3. Creates BUILD.bazel with a gazelle target

Examples

Basic Initialization

Initialize the current directory:

Terminal window
bazelle init

Example output:

Languages: go, kotlin, python
Created /path/to/project/MODULE.bazel
Created /path/to/project/BUILD.bazel
Next steps:
1. Update the gazelle:prefix directive in BUILD.bazel
2. Run 'bazelle update' to generate BUILD files

Specify Languages

Override auto-detection:

Terminal window
bazelle init --languages go,kotlin

Custom Module Name

Set a specific module name:

Terminal window
bazelle init --name myproject

Preview Changes

See what would be created:

Terminal window
bazelle init --dry-run

CI Check

Verify project is properly configured:

Terminal window
bazelle init --check

Generated Files

MODULE.bazel

module(
name = "myproject",
version = "0.1.0",
)
# Core dependencies
bazel_dep(name = "bazel_skylib", version = "1.9.0")
# Language support
bazel_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:

ExtensionsLanguage
.goGo
.kt, .ktsKotlin
.pyPython
.c, .cc, .cpp, .h, .hppC/C++
.protoProtocol Buffers
.groovyGroovy

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

CodeModeMeaning
0NormalInitialization completed
0--checkProject is properly configured
1--checkConfiguration issues found
1AnyError occurred

After Initialization

  1. Edit BUILD.bazel to set the correct gazelle:prefix:

    # gazelle:prefix github.com/your-org/your-repo
  2. Run update to generate BUILD files:

    Terminal window
    bazelle update
  3. Build and test:

    Terminal window
    bazel build //...
    bazel test //...