resolve

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: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindRuleWithOverride

func FindRuleWithOverride(c *config.Config, imp ImportSpec, lang string) (label.Label, bool)

FindRuleWithOverride searches the current configuration for user-specified dependency resolution overrides. Overrides specified later (in configuration files in deeper directories, or closer to the end of the file) are returned first. If no override is found, label.NoLabel is returned.

Types

type Configurer

type Configurer struct{}

func (*Configurer) Configure

func (*Configurer) Configure(ctx context.Context, args config.ConfigureArgs) error

func (*Configurer) KnownDirectives

func (*Configurer) KnownDirectives() []string

type FindArgs

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

	// Index is global rule index, built with [Indexer].
	Index *RuleIndex

	// Import is the import string to resolve. Find should return a list of
	// libraries that may be imported with this string.
	Import ImportSpec

	// Lang is the language of the source code where Import was found.
	Lang string

	// 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 FindResult

type FindResult struct {
	// Label is the absolute label (including repository and package name) for
	// a matched rule.
	Label label.Label

	// Embeds is the transitive closure of labels for rules that the matched
	// rule embeds. It may contains duplicates and does not include the label
	// for the rule itself.
	Embeds []label.Label
}

func (FindResult) IsSelfImport

func (r FindResult) IsSelfImport(from label.Label) bool

IsSelfImport returns true if the result's label matches the given label or the result's rule transitively embeds the rule with the given label. Self imports cause cyclic dependencies, so the caller may want to omit the dependency or report an error.

type Finder

type Finder interface {
	// Name is the name of the extension, as returned by language.Language.Name.
	Name() string

	// Find returns a list of libraries that can be imported with the given
	// import string. Each extension's Find method is called by RuleIndex if
	// a rule was not located within the index.
	Find(context.Context, FindArgs) ([]FindResult, error)
}

Finder is an interface that language extensions can implement to provide import resolution beyond what RuleIndex can do.

Most extensions do not need to implement this interface. It may be useful in cases where Indexer can't be completely implemented. For exmaple, the proto extension's Imports method can return imports in each language that it's aware of, but the user may have a custom proto compiler, rule set, and Gazelle extension.

type ImportSpec

type ImportSpec struct {
	Lang, Imp string
}

ImportSpec describes a library to be imported. Imp is an import string for the library. Lang is the language in which the import string appears (this should match Resolver.Name).

type ImportsArgs

type ImportsArgs struct {
	// Config is the configuration directory that contains the rule.
	Config *config.Config

	// Rule is the non-nil rule to index.
	Rule *rule.Rule

	// File is the build file that contains Rule.
	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 ImportsResult

type ImportsResult struct {
	// Imports is a list of language names and strings by which the rule may be
	// imported.
	Imports []ImportSpec

	// Embeds is a list of library labels that this rule embeds. Both the embedder
	// and embeddee may be importable by the same string, but in case of ambiguity,
	// the dependency resolver prefers the embedder.
	Embeds []label.Label

	// NotImportable is set for a rule that can't be imported, such as a test.
	// It's not necessary to set this when both Imports and Embeds are empty.
	NotImportable bool
}

type Indexer

type Indexer interface {
	// Name is the name of the extension, as returned by language.Language.Name.
	Name() string

	// Imports returns the names by which a rule can be imported. These names are
	// added to RuleIndex to aid dependency resolution.
	Imports(context.Context, ImportsArgs) (ImportsResult, error)
}

Indexer is an interface that language extensions can implement to list the names by which a rule can be imported.

type ResolveArgs

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

	// Index is a table mapping import strings to bazel labels for known
	// libraries, constructed using Indexer.
	Index *RuleIndex

	// Rule is the rule to resolve dependencies for. Resolve should modify its
	// "deps" attribute (or language-appropriate equivalent).
	Rule *rule.Rule

	// From is Rule's Bazel label.
	From label.Label

	// TODO(v2): definitely remove usage of this and refactor it out, after #2458.
	RemoteCache *repo.RemoteCache

	// 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

	// Imports contains information about imported libraries, returned in
	// GenerateResult.Imports. If GenerateResult.Imports was nil, then
	// Imports here is nil, too.
	Imports any
}

type Resolver

type Resolver interface {
	// Resolve performs dependency resolution on a rule, usually setting its
	// "deps" attribute.
	Resolve(context.Context, ResolveArgs) error
}

Resolver is an interface that languages extensions can implement to transform import strings from source files into Bazel labels, typically in a "deps" attribute. This is done separately from GenerateRules so that Gazelle can build an index of relevant library rules across the repo, including rules generated by GenerateRules.

type RuleIndex

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

RuleIndex is a table of rules in a workspace, indexed by label and by import path. Used by Resolver to map import paths to labels.

func NewRuleIndex

func NewRuleIndex(indexerForRule func(r *rule.Rule, pkgRel string) Indexer, finders []Finder, cache *walk.Cache) *RuleIndex

NewRuleIndex creates a new index.

indexerForRule returns an Indexer for a given rule in pkgRel, a slash-separated directory path relative to the repository root. For example, this might return the extension that generated the rule, or the extension responsible for indexing rule of this kind (considering alias_kind).

finders is a list of all extensions implementing the Finder interface.

cache is passed to Indexer.Imports and Finder.Find; it may be nil.

func (*RuleIndex) AddRule

func (ix *RuleIndex) AddRule(ctx context.Context, c *config.Config, r *rule.Rule, f *rule.File) error

AddRule adds a rule r to the index. The rule will only be indexed if there is a known resolver for the rule's kind and Resolver.Imports returns a non-nil slice.

AddRule may only be called before Finish.

func (*RuleIndex) Find

func (ix *RuleIndex) Find(ctx context.Context, c *config.Config, imp ImportSpec, lang string) ([]FindResult, error)

Find attempts to resolve an import.

c is the configuration for the directory containing the import.

imp describes the import. It contains an import string and language of the thing being imported.

lang is the language of the source code where imp was found.

If one or more libraries matching imp were previously added to the index with AddRule, then Find returns those libraries. Otherwise, Find calls the Find method of each enabled Finder implementation and returns the combined list of results, along with any returned errors.

func (*RuleIndex) Finish

func (ix *RuleIndex) Finish()

Finish constructs the import index and performs any other necessary indexing actions after all rules have been added. This step is necessary because a rule may be indexed differently based on what rules are added later.

Finish must be called after all AddRule calls and before any Find calls.

Jump to

Keyboard shortcuts

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