bazel

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package bazel provides integration between Buffalo and the Bazel build system.

Buffalo can detect Bazel workspaces, parse proto_library targets from BUILD files, generate code respecting Bazel's dependency graph, and produce BUILD.bazel files for the generated output so that downstream Bazel targets can depend on it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindBuildFiles

func FindBuildFiles(root string) (map[string]string, error)

FindBuildFiles discovers all BUILD and BUILD.bazel files under root.

func HasBazelFile

func HasBazelFile(dir string) bool

HasBazelFile returns true if the specific directory contains a BUILD file.

func IsBazelAvailable

func IsBazelAvailable() bool

IsBazelAvailable checks if the bazel binary is available in PATH.

func IsBazelWorkspace

func IsBazelWorkspace(dir string) bool

IsBazelWorkspace returns true if the given directory (or a parent) is a Bazel workspace.

func ResolveProtoFiles

func ResolveProtoFiles(ws *Workspace, target BazelTarget) []string

ResolveProtoFiles resolves the actual file paths for a target's srcs, relative to the workspace root and the target's package directory.

Types

type BazelError

type BazelError struct {
	Command string
	Stderr  string
	Err     error
}

BazelError wraps errors from bazel commands.

func (*BazelError) Error

func (e *BazelError) Error() string

func (*BazelError) Unwrap

func (e *BazelError) Unwrap() error

type BazelQuerier

type BazelQuerier struct {
	// contains filtered or unexported fields
}

BazelQuerier runs `bazel query` / `bazel cquery` to inspect the build graph.

func NewQuerier

func NewQuerier(workspaceRoot string) *BazelQuerier

NewQuerier creates a querier rooted at the given workspace.

func (*BazelQuerier) BuildQueryResult

func (q *BazelQuerier) BuildQueryResult(ctx context.Context, targets []string) (*QueryResult, error)

BuildQueryResult runs bazel query for a set of targets and returns a structured QueryResult with deps and source files.

func (*BazelQuerier) FindProtoTargets

func (q *BazelQuerier) FindProtoTargets(ctx context.Context, pattern string) ([]string, error)

FindProtoTargets uses `bazel query` to find all proto_library targets.

func (*BazelQuerier) GetDeps

func (q *BazelQuerier) GetDeps(ctx context.Context, target string) ([]string, error)

GetDeps returns the direct dependencies of a target.

func (*BazelQuerier) GetProtoSources

func (q *BazelQuerier) GetProtoSources(ctx context.Context, target string) ([]string, error)

GetProtoSources returns the proto source files for a target using --output=xml.

func (*BazelQuerier) GetTransitiveDeps

func (q *BazelQuerier) GetTransitiveDeps(ctx context.Context, target string) ([]string, error)

GetTransitiveDeps returns all transitive deps of a target.

func (*BazelQuerier) SetBazelPath

func (q *BazelQuerier) SetBazelPath(path string)

SetBazelPath overrides the bazel binary path.

type BazelTarget

type BazelTarget struct {
	// Name is the Bazel target name (e.g., "user_proto").
	Name string

	// Rule is the Bazel rule name (e.g., "proto_library").
	Rule string

	// Package is the Bazel package path (e.g., "//proto/user").
	Package string

	// Srcs lists source files declared in srcs = [...].
	Srcs []string

	// Deps lists dependency labels declared in deps = [...].
	Deps []string

	// Visibility lists visibility labels.
	Visibility []string

	// StripImportPrefix is the strip_import_prefix attribute.
	StripImportPrefix string

	// ImportPrefix is the import_prefix attribute.
	ImportPrefix string

	// ProtoSourceRoot is the proto_source_root attribute.
	ProtoSourceRoot string

	// Tags are build tags.
	Tags []string

	// Extra holds any additional attributes as key-value pairs.
	Extra map[string]string
}

BazelTarget represents a target parsed from a BUILD/BUILD.bazel file.

func FilterLangProtoTargets

func FilterLangProtoTargets(targets []BazelTarget, lang string) []BazelTarget

FilterLangProtoTargets returns language-specific proto library targets.

func FilterProtoTargets

func FilterProtoTargets(targets []BazelTarget) []BazelTarget

FilterProtoTargets returns only proto_library targets from a list.

func ParseBuildFile

func ParseBuildFile(path string, pkg string) ([]BazelTarget, error)

ParseBuildFile parses a BUILD/BUILD.bazel file and extracts proto_library targets.

type GeneratedBuild

type GeneratedBuild struct {
	// Path is the relative path where the BUILD.bazel should be written.
	Path string

	// Content is the rendered BUILD.bazel content.
	Content string

	// Bindings are the language bindings declared in this BUILD file.
	Bindings []LanguageBinding
}

GeneratedBuild represents a BUILD.bazel file to be written for generated code.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator creates BUILD.bazel files for generated protobuf code.

func NewGenerator

func NewGenerator(root, moduleName string) *Generator

NewGenerator creates a new BUILD.bazel generator.

func (*Generator) GenerateBuildFiles

func (g *Generator) GenerateBuildFiles(targets []BazelTarget, languages []string, outputDir string) ([]GeneratedBuild, error)

GenerateBuildFiles produces BUILD.bazel files for the generated code based on the proto_library targets that were compiled and the target languages.

type Integrator

type Integrator struct {
	// contains filtered or unexported fields
}

Integrator is the main entry point for Buffalo ↔ Bazel integration. It detects a Bazel workspace, discovers proto_library targets, resolves proto files, and after compilation generates BUILD.bazel files.

func NewIntegrator

func NewIntegrator(dir string) (*Integrator, error)

NewIntegrator creates an Integrator for the given directory. Returns (nil, nil) if no Bazel workspace is detected.

func NewIntegratorFromWorkspace

func NewIntegratorFromWorkspace(ws *Workspace) *Integrator

NewIntegratorFromWorkspace creates an Integrator from an already-detected workspace.

func (*Integrator) CreateSyncPlan

func (it *Integrator) CreateSyncPlan(ctx context.Context, targets []BazelTarget, languages []string, outputDir string) (*SyncPlan, error)

CreateSyncPlan builds a plan describing what Buffalo will compile and what BUILD files it will generate.

func (*Integrator) DiscoverProtoTargets

func (it *Integrator) DiscoverProtoTargets(ctx context.Context, patterns []string) ([]BazelTarget, error)

DiscoverProtoTargets finds all proto_library targets in the workspace. Uses `bazel query` when available, otherwise falls back to file parsing.

func (*Integrator) GetWorkspace

func (it *Integrator) GetWorkspace() *Workspace

GetWorkspace returns the detected workspace.

func (*Integrator) ResolveProtoFilePaths

func (it *Integrator) ResolveProtoFilePaths(targets []BazelTarget) []string

ResolveProtoFilePaths resolves all proto_library targets to actual file paths on disk, relative to the workspace root.

func (*Integrator) SetBazelPath

func (it *Integrator) SetBazelPath(path string)

SetBazelPath sets a custom bazel binary path.

func (*Integrator) SyncAfterBuild

func (it *Integrator) SyncAfterBuild(ctx context.Context, targets []BazelTarget, languages []string, outputDir string) error

SyncAfterBuild is called after Buffalo compilation completes. It generates and writes BUILD.bazel files for the generated code.

func (*Integrator) WriteBuildFiles

func (it *Integrator) WriteBuildFiles(builds []GeneratedBuild) error

WriteBuildFiles writes the generated BUILD.bazel files to disk.

type LanguageBinding

type LanguageBinding struct {
	// Language is the target language (e.g., "go", "python", "cpp", "rust", "typescript").
	Language string

	// Rule is the Bazel rule to generate (e.g., "go_proto_library").
	Rule string

	// TargetName is the generated target name.
	TargetName string

	// ProtoTarget is the label of the source proto_library.
	ProtoTarget string

	// Deps are language-specific dependencies.
	Deps []string

	// Importpath is language-specific import path (Go module, Python package, etc.).
	Importpath string

	// Options are extra rule attributes.
	Options map[string]string
}

LanguageBinding maps a proto_library target to language-specific generated library targets (go_proto_library, py_proto_library, etc.).

type QueryResult

type QueryResult struct {
	// Targets lists discovered target labels.
	Targets []string

	// Deps maps a target label to its transitive dependency labels.
	Deps map[string][]string

	// ProtoFiles maps a target label to its resolved proto source files.
	ProtoFiles map[string][]string
}

QueryResult holds results from `bazel query` or `bazel cquery`.

type SyncPlan

type SyncPlan struct {
	// TargetsToCompile lists proto_library targets to compile.
	TargetsToCompile []BazelTarget

	// BuildFilesToGenerate lists BUILD.bazel files to create/update.
	BuildFilesToGenerate []GeneratedBuild

	// Languages are the target languages.
	Languages []string

	// OutputDir is the output root.
	OutputDir string
}

SyncPlan describes what Buffalo will do to synchronize with Bazel.

type Workspace

type Workspace struct {
	// Root is the absolute path to the workspace root.
	Root string

	// Type is "bzlmod" (MODULE.bazel) or "workspace" (WORKSPACE/WORKSPACE.bazel).
	Type string

	// ModuleName is the module name from MODULE.bazel (bzlmod only).
	ModuleName string

	// ProtoTargets are all proto_library targets discovered in the workspace.
	ProtoTargets []BazelTarget

	// BuildFiles maps Bazel package paths to their BUILD file paths on disk.
	BuildFiles map[string]string
}

Workspace describes a detected Bazel workspace.

func DetectWorkspace

func DetectWorkspace(dir string) (*Workspace, error)

DetectWorkspace searches upward from dir looking for a Bazel workspace root. Returns nil if no workspace is found.

Jump to

Keyboard shortcuts

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