rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

how to upgrade to 0.41.0 via bzlmod only

Open pdeva opened this issue 2 years ago • 4 comments

the upgrade instruction for 0.41.0 mention this as step 1:

http_archive(
    name = "googleapis",
    sha256 = "9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88",
    strip_prefix = "googleapis-64926d52febbf298cb82a8f472ade4a3969ba922",
    urls = [
        "https://github.com/googleapis/googleapis/archive/64926d52febbf298cb82a8f472ade4a3969ba922.zip",
    ],
)

load("@googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
    name = "com_google_googleapis_imports",
)

this seems to be for users of WORKSPACE file.

What is the equivalent to the above for those looking to use bzlmod only?

pdeva avatar Sep 07 '23 18:09 pdeva

Try this:

MODULE.bazel

GOOGLE_APIS_VERSION = "64926d52febbf298cb82a8f472ade4a3969ba922"

bazel_dep(name = "com_google_googleapis", version = GOOGLE_APIS_VERSION)
archive_override(
    module_name = "com_google_googleapis",
    integrity = "sha256-nRqTDnZ8k8glOYuPhpLso/41O5qq3t+88fyiKCyF34g=",
    patch_strip = 1,
    patches = [
        "add_bzlmod_support.patch"
    ],
    strip_prefix = "googleapis-" + GOOGLE_APIS_VERSION,
    urls = [
        "https://github.com/googleapis/googleapis/archive/%s.zip" % GOOGLE_APIS_VERSION
    ],
)

switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
switched_rules.use_languages()
use_repo(switched_rules, "com_google_googleapis_imports")

add_bzlmod_support.patch

diff --git a/MODULE.bazel b/MODULE.bazel
new file mode 100644
index 000000000..c26575773
--- /dev/null
+++ b/MODULE.bazel
@@ -0,0 +1,22 @@
+module(
+    name = "com_google_googleapis",
+    version = "64926d52febbf298cb82a8f472ade4a3969ba922",
+)
+
+bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
+bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
+
+switched_rules = use_extension("//:extensions.bzl", "switched_rules")
+switched_rules.use_languages(
+    cc = True,
+    csharp = True,
+    gapic = True,
+    go = True,
+    grpc = True,
+    java = True,
+    nodejs = True,
+    php = True,
+    python = True,
+    ruby = True,
+)
+use_repo(switched_rules, "com_google_googleapis_imports")
diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod
new file mode 100644
index 000000000..fafa28da3
--- /dev/null
+++ b/WORKSPACE.bzlmod
@@ -0,0 +1,2 @@
+# When Bzlmod is enabled, this file replaces the content of the original WORKSPACE
+# and makes sure no WORKSPACE prefix or suffix are added when Bzlmod is enabled.
diff --git a/extensions.bzl b/extensions.bzl
new file mode 100644
index 000000000..d8f04cf6c
--- /dev/null
+++ b/extensions.bzl
@@ -0,0 +1,50 @@
+load(":repository_rules.bzl", "switched_rules_by_language")
+
+_use_languages_tag = tag_class(
+    attrs = {
+        "cc": attr.bool(default = False),
+        "csharp": attr.bool(default = False),
+        "gapic": attr.bool(default = False),
+        "go": attr.bool(default = False),
+        "go_test": attr.bool(default = False),
+        "grpc": attr.bool(default = False),
+        "java": attr.bool(default = False),
+        "nodejs": attr.bool(default = False),
+        "php": attr.bool(default = False),
+        "python": attr.bool(default = False),
+        "ruby": attr.bool(default = False),
+    },
+)
+
+def _switched_rules_impl(ctx):
+    attrs = {}
+    for module in ctx.modules:
+        if not module.is_root:
+            continue
+
+        for t in module.tags.use_languages:
+            attrs = {
+                "cc": t.cc,
+                "csharp": t.csharp,
+                "gapic": t.gapic,
+                "go": t.go,
+                "go_test": t.go_test,
+                "grpc": t.grpc,
+                "java": t.java,
+                "nodejs": t.nodejs,
+                "php": t.php,
+                "python": t.python,
+                "ruby": t.ruby,
+            }
+
+    switched_rules_by_language(
+        name = "com_google_googleapis_imports",
+        **attrs
+    )
+
+switched_rules = module_extension(
+    implementation = _switched_rules_impl,
+    tag_classes = {
+        "use_languages": _use_languages_tag,
+    },
+)

lbcjbb avatar Sep 08 '23 15:09 lbcjbb

@fmeum or @pdeva Did you get this to work? I can't see the go build rules when I query using this:

bazel query @com_google_googleapis//google/api/...
@com_google_googleapis//google/api:annotations_proto
@com_google_googleapis//google/api:auth_proto
@com_google_googleapis//google/api:backend_proto
@com_google_googleapis//google/api:billing_proto
@com_google_googleapis//google/api:client_proto
@com_google_googleapis//google/api:config_change_proto
@com_google_googleapis//google/api:consumer_proto
@com_google_googleapis//google/api:context_proto
@com_google_googleapis//google/api:control_proto
@com_google_googleapis//google/api:distribution_proto
@com_google_googleapis//google/api:documentation_proto
@com_google_googleapis//google/api:endpoint_proto
@com_google_googleapis//google/api:error_reason_proto
@com_google_googleapis//google/api:field_behavior_proto
@com_google_googleapis//google/api:http_proto
@com_google_googleapis//google/api:httpbody_proto
@com_google_googleapis//google/api:label_proto
@com_google_googleapis//google/api:launch_stage_proto
@com_google_googleapis//google/api:log_proto
@com_google_googleapis//google/api:logging_proto
@com_google_googleapis//google/api:metric_proto
@com_google_googleapis//google/api:monitored_resource_proto
@com_google_googleapis//google/api:monitoring_proto
@com_google_googleapis//google/api:quota_proto
@com_google_googleapis//google/api:resource_proto
@com_google_googleapis//google/api:routing_proto
@com_google_googleapis//google/api:service_proto
@com_google_googleapis//google/api:source_info_proto
@com_google_googleapis//google/api:system_parameter_proto
@com_google_googleapis//google/api:usage_proto
@com_google_googleapis//google/api:visibility_proto
@com_google_googleapis//google/api/apikeys/v2:apikeys_proto
@com_google_googleapis//google/api/expr/conformance/v1alpha1:conformance_proto
@com_google_googleapis//google/api/expr/v1alpha1:checked_proto
@com_google_googleapis//google/api/expr/v1alpha1:eval_proto
@com_google_googleapis//google/api/expr/v1alpha1:explain_proto
@com_google_googleapis//google/api/expr/v1alpha1:expr_proto
@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto
@com_google_googleapis//google/api/expr/v1alpha1:value_proto
@com_google_googleapis//google/api/expr/v1beta1:cel_proto
@com_google_googleapis//google/api/expr/v1beta1:decl_proto
@com_google_googleapis//google/api/expr/v1beta1:eval_proto
@com_google_googleapis//google/api/expr/v1beta1:expr_proto
@com_google_googleapis//google/api/expr/v1beta1:source_proto
@com_google_googleapis//google/api/expr/v1beta1:value_proto
@com_google_googleapis//google/api/servicecontrol/v1:servicecontrol_proto
@com_google_googleapis//google/api/servicecontrol/v2:servicecontrol_proto
@com_google_googleapis//google/api/servicemanagement/v1:servicemanagement_proto
@com_google_googleapis//google/api/serviceusage/v1:serviceusage_proto
@com_google_googleapis//google/api/serviceusage/v1beta1:serviceusage_proto

jamesthompson avatar Oct 25 '23 18:10 jamesthompson

Note to anybody trying this at home: if you see an error like this:

no such package '@[unknown repo 'googleapis' requested from @gazelle~0.34.0~go_deps~com_github_grpc_ecosystem_grpc_gateway]//google/api'

then you need to patch Gazelle to pull in googleapis so that its extension modules can see it.

In your MODULE.bazel, change Gazelle from a bazel_dep to a git_override (note that single_version_override won't work). Then add a patch like this:

diff --git MODULE.bazel MODULE.bazel
index e272b19..41f4c71 100644
--- MODULE.bazel
+++ MODULE.bazel
@@ -5,6 +5,7 @@ module(
 )
 
 bazel_dep(name = "bazel_skylib", version = "1.3.0")
+bazel_dep(name = "googleapis")
 bazel_dep(name = "protobuf", version = "3.19.6", repo_name = "com_google_protobuf")
 bazel_dep(name = "rules_go", version = "0.42.0", repo_name = "io_bazel_rules_go")
 bazel_dep(name = "rules_proto", version = "4.0.0")

Now all the Go deps that Gazelle pulls in will be able to find googleapis!

adzenith avatar Nov 14 '23 19:11 adzenith

Trying this with latest versions but i get this:

ERROR: error loading package '@@com_google_googleapis~//google/rpc': Unable to find package for @@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]//:imports.bzl: The repository '@@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]' could not be resolved: No repository visible as '@com_google_googleapis_imports' from repository '@@com_google_googleapis~'.
ERROR: error loading package '@@com_google_googleapis~//google/rpc': Unable to find package for @@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]//:imports.bzl: The repository '@@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]' could not be resolved: No repository visible as '@com_google_googleapis_imports' from repository '@@com_google_googleapis~'.
ERROR: error loading package '@@com_google_googleapis~//google/rpc': Unable to find package for @@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]//:imports.bzl: The repository '@@[unknown repo 'com_google_googleapis_imports' requested from @@com_google_googleapis~]' could not be resolved: No repository visible as '@com_google_googleapis_imports' from repository '@@com_google_googleapis~'.

Bazel: 7.3.0 googeapis: current head

mtoader avatar Aug 16 '24 20:08 mtoader