Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for automated module fixups for changed dependencies #52

Open
ar3s3ru opened this issue Dec 7, 2023 · 4 comments
Open

Support for automated module fixups for changed dependencies #52

ar3s3ru opened this issue Dec 7, 2023 · 4 comments

Comments

@ar3s3ru
Copy link

ar3s3ru commented Dec 7, 2023

The Gazelle plugin at the moment only works on buf_dependencies when running update-repos, which is mostly geared to WORKSPACE-based projects.

There is support to pull in Buf dependencies in a Bzlmod context with the module extensions, but that's mostly manual work (correct me if I'm wrong).

However, Bazel has support for automatic use_repo fixups (source: bazelbuild/bazel#17908), and can be seen in action here for Go: bazelbuild/bazel-gazelle#1511 (review)

We could add something similar here too, so that running bazel run :gazelle can output dependencies fixes, and applied with a script similar to this:

#!/usr/bin/env bash

set -Eeo pipefail

set +e
echo "==> running gazelle through 'bazel run :gazelle'"
buildozer_cmd="$(bazel run :gazelle 2>&1 | grep "buildozer 'use_repo")"
set -Eeo pipefail

if [ -z "$buildozer_cmd" ]; then
  exit 0
fi

echo "==> fixing module dependencies with buildozer"
echo "----> running $buildozer_cmd"
bash -c "$buildozer_cmd"
@srikrsna-buf
Copy link
Member

I am not sure about buildozer, need to look into that. But I did want to add support for reading the lock files directly to get the versions similar to how it works in go

@ar3s3ru
Copy link
Author

ar3s3ru commented Dec 7, 2023

The way it currently works with Gazelle's go is the following:

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
    go_deps,
    "com_connectrpc_connect",
    "com_connectrpc_grpchealth",
    "com_connectrpc_grpcreflect",
    # ...
)

When running bazel run :gazelle, if there is a mismatch between the contents of go.mod and what's in the MODULE.bazel's use_repo, Bazel will output the buildozer commands necessary to run to fix use_repo(go_deps, ...).

Example:

buildozer 'use_repo_remove @gazelle//:extensions.bzl go_deps bazel_gazelle_go_repository_config' //MODULE.bazel:all

In the current dependencies implementation for Bzlmod (very elegant btw 👏🏻), the dependencies are defined with the extension method buf.dependencies(), but are then loaded automatically under @buf_deps.

One feature we could add is something like:

buf_deps = use_extension("@rules_buf//path/to:extensions.bzl", "buf_deps")
buf_deps.from_file(buf_lock = "//path/to:buf_lock")

The big change with this approach however would be to create separate workspaces for each dependency, instead of grouping the modules under a single @buf_deps workspace.

Then we could use something like:

use_repo(
  buf_deps,
  "buf_build_googleapis_googleapis",
  # ...
)

and access them using @buf_build_googleapis_googleapis//....

This might also unlock the ability of doing some dependency-level override, e.g. applying a patch to a specific dependency (I have this specific case atm, where buf.build/googleapis/googleapis has a BUILD.bazel file under google/type with one single proto_library -- not sure if the issue is from Gazelle running in buf_dependencies or from the upstream Buf module)

@aaomidi
Copy link

aaomidi commented Aug 16, 2024

Is there any progress on this? Buf seems to be the last thing I need a workplace file for.

@aaomidi
Copy link

aaomidi commented Aug 16, 2024

It might be that we need https://buf.build/docs/build-systems/bazel updated to explain how to use it in module mode?

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

No branches or pull requests

3 participants