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

Allow rust toolchain repositories from custom repo rules #1019

Closed
ikalchev opened this issue Nov 17, 2021 · 3 comments · Fixed by #1036
Closed

Allow rust toolchain repositories from custom repo rules #1019

ikalchev opened this issue Nov 17, 2021 · 3 comments · Fixed by #1036

Comments

@ikalchev
Copy link
Contributor

If I use some non-standard packaging system and thus have a custom repo rules that pull rust binaries (rustc, cargo), I would like to be able to configure the rust_toolchain_repository_proxy (or otherwise) to use this other repo rule.

My current workaround is to:

  1. Define a rust_toolchain which uses the artefacts from the custom repo rule. This is needed for using rust in bazel outside of the crate_universe (i.e. not important to this issue).
  2. Patch crate_universe to accept a mapping from tool to path.
  3. Use crate_universe as:
crate_univers(
        ...
        rust_toolchain_repository_template = "custom repo rule label",
        rust_toolchain_repository_tool_path = {
            "cargo": "path to cargo within custom repo",
            "rustc": "path to cargo within custom repo",
        },
        ...
@illicitonion
Copy link
Collaborator

If you can't make it so that cargo is at //:bin/cargo in your repository, and rustc is at //:bin/rustc in your repository (which would be ideal), I'd be happy to review a patch which made the paths to these tools within the repository configurable :) I imagine it should be a simple change to put together (as it sounds like you already have done), adding an attribute or two here

"rust_toolchain_repository_template": attr.string(
and consuming them here
cargo_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/cargo" + extension)))
rustc_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/rustc" + extension)))

@ikalchev
Copy link
Contributor Author

Here is my diff. Let me know if you would like a PR.

diff --git a/crate_universe/defs.bzl b/crate_universe/defs.bzl
index ea9b5f2..2c85d08 100644
--- a/crate_universe/defs.bzl
+++ b/crate_universe/defs.bzl
@@ -157,7 +157,7 @@ __WARNING__: This rule experimental and subject to change without warning.
 Environment Variables:
 - `REPIN`: Re-pin the lockfile if set (useful for repinning deps from multiple rulesets).
 - `RULES_RUST_REPIN`: Re-pin the lockfile if set (useful for only repinning Rust deps).
-- `RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE`: Override URL to use to download resolver binary 
+- `RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE`: Override URL to use to download resolver binary
     - for local paths use a `file://` URL.
 - `RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE_SHA256`: An optional sha256 value for the binary at the override url location.
 """,
@@ -226,6 +226,13 @@ Dict of registry_name: index_url.
             ),
             default = "rust_{system}_{arch}",
         ),
+        "rust_toolchain_repository_tool_path": attr.string_dict(
+            doc = "The relative path of the tools in the repository",
+            default = {
+                "cargo": "bin/cargo",
+                "rustc": "bin/rustc"
+            }
+        ),
         "sha256s": attr.string_dict(
             doc = "The sha256 checksum of the desired rust artifacts",
         ),
diff --git a/crate_universe/private/util.bzl b/crate_universe/private/util.bzl
index ca64116..1a43ca6 100644
--- a/crate_universe/private/util.bzl
+++ b/crate_universe/private/util.bzl
@@ -134,8 +134,13 @@ def get_cargo_and_rustc(repository_ctx, host_triple):
     rust_toolchain_repository = rust_toolchain_repository.replace("{triple}", host_triple)
     rust_toolchain_repository = rust_toolchain_repository.replace("{arch}", arch)
 
-    cargo_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/cargo" + extension)))
-    rustc_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/rustc" + extension)))
+    tool_path = repository_ctx.attr.rust_toolchain_repository_tool_path
+    cargo_path = repository_ctx.path(Label("@{}//:{}{}".format(
+        rust_toolchain_repository, tool_path["cargo"], extension
+    )))
+    rustc_path = repository_ctx.path(Label("@{}//:{}{}".format(
+        rust_toolchain_repository, tool_path["rustc"], extension
+    )))
 
     return struct(
         cargo = cargo_path,

@illicitonion
Copy link
Collaborator

A PR would be great if you wouldn't mind!

Also, let's include the : in the values so that folks can have package structure if they want.

Thanks!

illicitonion pushed a commit that referenced this issue Nov 29, 2021
This change allows `crate_universe` to use a custom repo for the rust toolchain. I.e. suppose that `cargo` and `rustc` come from a custom repo rule and have different paths inside the repo rule (compared to the standard "upstream")

Fixes #1019
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 a pull request may close this issue.

2 participants