Skip to content

Conversation

@WojciechMazur
Copy link
Collaborator

Extracted from #92

Adds indexer powered by BCR as provider of target definitions, allowing to create indexes in the same way as the internal BCR indexer, but takes into account only projects found in MODULE.bazel ensuring to use correct version

@WojciechMazur WojciechMazur changed the title Add indexer for bzlmod based on the BCR registry and user defined MODULE.bazel Indexer: Add indexer for bzlmod based on the BCR registry and user defined MODULE.bazel Nov 16, 2025
@WojciechMazur WojciechMazur marked this pull request as draft November 16, 2025 01:07
@WojciechMazur WojciechMazur force-pushed the indexer/03-bcr-indexer-go branch from 150772d to 6b200d0 Compare December 24, 2025 00:26
@WojciechMazur WojciechMazur force-pushed the indexer/04-bzlmod-indexer branch from 36aa550 to 18d182d Compare December 24, 2025 00:31
@WojciechMazur WojciechMazur marked this pull request as ready for review December 24, 2025 00:31
Base automatically changed from indexer/03-bcr-indexer-go to main December 30, 2025 11:55
@WojciechMazur WojciechMazur force-pushed the indexer/04-bzlmod-indexer branch from 18d182d to 029cd3d Compare December 30, 2025 12:13
…ULE.bazel

Signed-off-by: Wojciech Mazur <wmazur@virtuslab.com>
Signed-off-by: Wojciech Mazur <wmazur@virtuslab.com>
Version string
}

func ExtractBazelDependencies(file build.File) []BazelDependency {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build.File may refer to any starlark file like BUILD, MODULE or custom.bzl. Please use moduleFile as variable name to emphasize the intention.


func ExtractBazelDependencies(file build.File) []BazelDependency {
result := []BazelDependency{}
for _, stmt := range file.Stmt {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could extract the loop body into

func parseBazelDependency(stmt build.Expr) BazelDependency, bool

and then use collections.FilterMapSlice here. So we avoid too many indentation levels.

return resolvedModules
}

type BazelDependency struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to use capitalized identifiers - same for functions. Nothing can be exported from an executable. I recommend not doing this, so nobody thinks this is used in other packages.

case "bazel_dep":
dep := BazelDependency{}
parseArg:
for idx, arg := range tree.List {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing can be done here:

func parseBazelDependencyArgs(args []Expr) BazelDependency, bool

But it's up to you. I am not a fan of deep nesting. Separate functions provide documentation advantage - their names clarify the intention behind them.

}
log.Printf("Would run in %v", callerRoot)

moduleBazelPath := *moduleBzl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are too similar. Please change them to something like absModuleBazelPath and moduleBazelPath (for the pure flag value, which is moduleBzl right now).

}
}

workers := runtime.GOMAXPROCS(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW (I am not a Go concurrency master) - is it worth implementing a thread pool like this? Isn't Go concurrency "smart" enough to just run a separate goroutine for every BazelDependency? Or is there too much resource consumption for so many goroutines?

for bazelDep := range bazelDeps {
result := bcrClient.ResolveModuleInfo(bazelDep.Name, bazelDep.Version)
if *cli.Verbose {
switch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple if is more readable for a single true/false condition.

for result := range resolveResults {
switch {
case result.IsResolved() && len(result.Info.Targets) > 0:
if len(result.Info.Targets) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is never true. You've already assumed len(result.Info.Targets) > 0 above.

Please add a non-existing repo to MODULE.bazel in the integration test to check this.


var resolvedModules []indexer.Module
var emptyModules []string
var failed int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could store module names var failed []string, similarly to emptyModules, for better verbosity.

You should be able to obtain the module name from ResolveModuleInfoResult.Unresolved.Module.Name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants