language

package
v2.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package language provides interfaces for language extensions in Gazelle.

To implement a new extension, define a type that implements Language and other interfaces listed here. You will likely also want to implement github.com/bazel-contrib/bazel-gazelle/v2/config.Configurer, github.com/bazel-contrib/bazel-gazelle/v2/resolve.Indexer, and github.com/bazel-contrib/bazel-gazelle/v2/resolve.Resolver. Then define a function `NewV2()` that returns a value of your type. `gazelle_binary` generates code that calls this function when Gazelle starts.

These interfaces are designd for extensibility. Most interfaces are optional, and most have only one method. Most methods accept arguments and return results as struct values, so more fields may be added in the future without breaking compatibility.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixArgs

type FixArgs struct {
	// Config is the configuration for the current directory.
	Config *config.Config

	// Rel is the slash-separated path to the directory, relative to the
	// repository root ("" for the root directory itself). This may be used
	// as the package name in labels.
	Rel string

	// File is the build file for the directory. File is nil if there is
	// no existing build file.
	File *rule.File

	// Cache is an in-memory cache of file metadata. It may be used instead of
	// direct I/O to read file metadata from other directories.
	Cache *walk.Cache
}

type Fixer

type Fixer interface {
	Fix(context.Context, FixArgs) error
}

Fixer supports repairing deprecated usage of rules in a BUILD file. Fix is called in a directory Gazelle will update after the BUILD file is parsed, before new rules are generated and before rules are indexed. Fix should only perform destructive changes (like deleting or renaming rules) if `args.Config.ShouldFix` is true.

type GenerateArgs

type GenerateArgs struct {
	// Config is the configuration for the current directory.
	Config *config.Config

	// Dir is the canonical absolute path to the directory.
	Dir string

	// Rel is the slash-separated path to the directory, relative to the
	// repository root ("" for the root directory itself). This may be used
	// as the package name in labels.
	Rel string

	// File is the build file for the directory. File is nil if there is
	// no existing build file.
	File *rule.File

	// Subdirs is a list of subdirectories in the directory, including
	// symbolic links to directories that Gazelle will follow.
	// RegularFiles is a list of regular files including other symbolic
	// links.
	// GenFiles is a list of generated files in the directory
	// (usually these are mentioned as "out" or "outs" attributes in rules).
	// These slices must not be modified.
	Subdirs, RegularFiles, GenFiles []string

	// OtherEmpty is a list of empty rules generated by other languages.
	// OtherGen is a list of generated rules generated by other languages.
	OtherEmpty, OtherGen []*rule.Rule

	// Cache is an in-memory cache of file metadata. It may be used instead of
	// direct I/O to read file metadata from other directories.
	Cache *walk.Cache
}

type GenerateResult

type GenerateResult struct {
	// Gen is a list of rules generated from files found in the directory
	// GenerateRules was asked to process. These will be merged with existing
	// rules or added to the build file.
	Gen []*rule.Rule

	// Empty is a list of rules that cannot be built with the files found in the
	// directory GenerateRules was asked to process. These will be merged with
	// existing rules. If the merged rules are empty, they will be deleted.
	Empty []*rule.Rule

	// Imports contains information about the imported libraries for each
	// rule in Gen. If Imports is set, it must have the same number of elements
	// as Gen. When Resolve.Resolver is called later, this value is passed
	// as ResolveArgs.Imports. The type is opaque so different extensions can
	// represent imports however they want.
	Imports []any

	// RelsToIndex is a list of additional directories to index for dependency
	// resolution, expressed as slash-separated paths relative to the repository
	// root, or "" for the root directory itself. If indexing is enabled,
	// libraries in these directories are indexed before dependencies are
	// resolved. Subdirectories are not recursively indexed. This list may
	// contain non-existent directories.
	RelsToIndex []string
}

type Generator

type Generator interface {

	// Kinds returns a list of rules this extension may generate, with information
	// on how to load symbols and merge attributes. All kinds of rules generated
	// by this language are found here.
	Kinds() []rule.KindInfo

	// Generate reads source files in a directory and produces a list of rules
	// that should appear in that directory's build file.
	Generate(context.Context, GenerateArgs) (GenerateResult, error)
}

Generator has methods for generating new rules.

Most extensions that implement Generator should also implement github.com/bazel-contrib/bazel-gazelle/v2/resolve.Indexer and github.com/bazel-contrib/bazel-gazelle/v2/resolve.Resolver to support dependency resolution. But this is not necessary if generated rules' dependencies can be completely determined without indexing other rules in the repo.

type Language

type Language interface {
	// Name returns the name of the extension, like "go" or "cc". Name must always
	// return the same value.
	Name() string
}

Language is a marker interface for extensions. It is the only mandatory interface that extensions must implement.

type OnFinisher

type OnFinisher interface {
	// Called after Gazelle has resolved dependencies on all rules and written
	// updated build files.
	OnFinish(context.Context) error
}

type OnResolver

type OnResolver interface {
	// Called after Gazelle generates rules in all directories it will visit
	// but before Gazelle performs dependency resolution.
	OnResolve(context.Context) error
}

type OnStarter

type OnStarter interface {
	// Called when Gazelle starts, before walking the directory tree and before
	// Configure is called on any extension.
	OnStart(context.Context) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL