rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

`crates_vendor` in remote mode uses incorrect labels when used from the root workspace

Open csmulhern opened this issue 3 years ago • 0 comments

Note: this is a different problem than #1341.

When using crates_vendor in remote mode from the root workspace, incorrect labels are used in the defs.bzl and crates.bzl generated files. Here are two examples:

defs.bzl:

maybe(
    http_archive,
    name = "crates__log-0.4.17",
    sha256 = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e",
    type = "tar.gz",
    urls = ["https://crates.io/api/v1/crates/log/0.4.17/download"],
    strip_prefix = "log-0.4.17",
    build_file = Label("@__main__//third_party/crates:BUILD.log-0.4.17.bazel"),
)

crates.bzl:

load("@__main__//third_party/crates:defs.bzl", _crate_repositories = "crate_repositories")

def crate_repositories():
    maybe(
        crates_vendor_remote_repository,
        name = "crates",
        build_file = Label("@__main__//third_party/crates:BUILD.bazel"),
        defs_module = Label("@__main__//third_party/crates:defs.bzl"),
    )

    _crate_repositories()

Something like the following fixes things for me locally:

diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl
index 021ffc3b..f766d92e 100644
--- a/crate_universe/private/crates_vendor.bzl
+++ b/crate_universe/private/crates_vendor.bzl
@@ -130,24 +130,43 @@ def _write_config_file(ctx):
     output_pkg = _get_output_package(ctx)
 
     if ctx.attr.mode == "local":
-        build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel"
+        build_file_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel".format(
+            ctx.workspace_name,
+            output_pkg
+        )
+
         crate_label_template = "//{}/{{name}}-{{version}}:{{target}}".format(
             output_pkg,
         )
-    else:
-        build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel"
-        crate_label_template = rendering_config["crate_label_template"]
 
-    rendering_config.update({
-        "build_file_template": build_file_base_template.format(
+        crates_module_template = "@{}//{}:{{file}}".format(
             ctx.workspace_name,
             output_pkg,
-        ),
-        "crate_label_template": crate_label_template,
-        "crates_module_template": "@{}//{}:{{file}}".format(
+        )
+    elif ctx.workspace_name != '__main__':
+        build_file_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel".format(
+            ctx.workspace_name,
+            output_pkg
+        )
+
+        crate_label_template = rendering_config["crate_label_template"]
+
+        crates_module_template = "@{}//{}:{{file}}".format(
             ctx.workspace_name,
             output_pkg,
-        ),
+        )
+    else:
+        build_file_template = "//{}:BUILD.{{name}}-{{version}}.bazel".format(
+            output_pkg
+        )
+
+        crate_label_template = rendering_config["crate_label_template"]
+        crates_module_template = "//{}:{{file}}".format(output_pkg)
+
+    rendering_config.update({
+        "build_file_template": build_file_template,
+        "crate_label_template": crate_label_template,
+        "crates_module_template": crates_module_template,
         "vendor_mode": ctx.attr.mode,
     })

csmulhern avatar Aug 14 '22 04:08 csmulhern