aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmelia <140295624+AmeliasCode@users.noreply.github.com>2024-02-09 07:26:34 -0800
committerGitHub <noreply@github.com>2024-02-09 15:26:34 +0000
commit63a73ac7f003bf72b6a109eb2634ddb9ae3ad3fb (patch)
tree082023e9a6d8c6c02127ed7b3e1f880d7cce9712
parent7ce81c1e28c0eedffda2b61fa14ce223016054f8 (diff)
downloadbazelbuild-rules_rust-63a73ac7f003bf72b6a109eb2634ddb9ae3ad3fb.tar.gz
Add support for `all_crate_deps` when using bzlmod (#2461)
This adds support for the `all_crate_deps` functionality in repositories created via bzlmod. An [earlier update](https://github.com/bazelbuild/rules_rust/pull/1910/files) that added support for 3rd party crates with bzlmod intentionally left this functionality out. Some key things that I think should be reviewed: - Is the method of getting `crate_label_template` in `extensions.bzl` the best approach? It requires a lot of hard-coding, and it seems like it might be possible to simplify but I'm not familiar enough with the code base to know how. - Is the `generate_config_file` method from `crates_vendor.bzl` the right method to use for generating the cargo-bazel config? It seems like there are two places with similar logic (here and in `crates_repositories.bzl` (see https://github.com/bazelbuild/rules_rust/pull/1832)
-rw-r--r--crate_universe/extension.bzl2
-rw-r--r--crate_universe/private/crates_vendor.bzl5
-rw-r--r--examples/bzlmod/all_crate_deps/.bazelrc6
-rw-r--r--examples/bzlmod/all_crate_deps/.gitignore2
-rw-r--r--examples/bzlmod/all_crate_deps/BUILD.bazel45
-rw-r--r--examples/bzlmod/all_crate_deps/Cargo.lock16
-rw-r--r--examples/bzlmod/all_crate_deps/Cargo.toml12
-rw-r--r--examples/bzlmod/all_crate_deps/MODULE.bazel40
-rw-r--r--examples/bzlmod/all_crate_deps/WORKSPACE.bazel1
-rw-r--r--examples/bzlmod/all_crate_deps/WORKSPACE.bzlmod1
-rwxr-xr-xexamples/bzlmod/all_crate_deps/all_crate_deps_test.sh35
-rw-r--r--examples/bzlmod/all_crate_deps/src/main.rs18
12 files changed, 182 insertions, 1 deletions
diff --git a/crate_universe/extension.bzl b/crate_universe/extension.bzl
index 46bb70c4..9117f6c4 100644
--- a/crate_universe/extension.bzl
+++ b/crate_universe/extension.bzl
@@ -46,6 +46,7 @@ def _generate_hub_and_spokes(module_ctx, cargo_bazel, cfg, annotations):
rendering_config = json.decode(render_config(
regen_command = "Run 'cargo update [--workspace]'",
+ crate_label_template = "@@rules_rust~override~crate~{repository}__{name}-{version}//:{target}",
))
config_file = tag_path.get_child("config.json")
module_ctx.file(
@@ -121,6 +122,7 @@ def _generate_hub_and_spokes(module_ctx, cargo_bazel, cfg, annotations):
name = cfg.name,
contents = {
"BUILD.bazel": module_ctx.read(crates_dir.get_child("BUILD.bazel")),
+ "defs.bzl": module_ctx.read(crates_dir.get_child("defs.bzl")),
},
)
diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl
index 484f09ab..8f4a03d4 100644
--- a/crate_universe/private/crates_vendor.bzl
+++ b/crate_universe/private/crates_vendor.bzl
@@ -225,8 +225,11 @@ def generate_config_file(
"vendor_mode": mode,
}
+ # "crate_label_template" is explicitly supported above in non-local modes
+ excluded_from_key_check = ["crate_label_template"]
+
for key in updates:
- if render_config[key] != default_render_config[key]:
+ if (render_config[key] != default_render_config[key]) and key not in excluded_from_key_check:
fail("The `crates_vendor.render_config` attribute does not support the `{}` parameter. Please update {} to remove this value.".format(
key,
ctx.label,
diff --git a/examples/bzlmod/all_crate_deps/.bazelrc b/examples/bzlmod/all_crate_deps/.bazelrc
new file mode 100644
index 00000000..825e3155
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/.bazelrc
@@ -0,0 +1,6 @@
+# Required on windows
+common --enable_platform_specific_config
+startup --windows_enable_symlinks
+build:windows --enable_runfiles
+
+build --experimental_enable_bzlmod
diff --git a/examples/bzlmod/all_crate_deps/.gitignore b/examples/bzlmod/all_crate_deps/.gitignore
new file mode 100644
index 00000000..2f0f755d
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/.gitignore
@@ -0,0 +1,2 @@
+/bazel-*
+/MODULE.bazel.lock
diff --git a/examples/bzlmod/all_crate_deps/BUILD.bazel b/examples/bzlmod/all_crate_deps/BUILD.bazel
new file mode 100644
index 00000000..d4045fd2
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/BUILD.bazel
@@ -0,0 +1,45 @@
+load("@bazel_skylib//rules:build_test.bzl", "build_test")
+load("@crates//:defs.bzl", "all_crate_deps")
+load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc")
+
+package(default_visibility = ["//visibility:public"])
+
+rust_binary(
+ name = "all_crate_deps",
+ srcs = ["src/main.rs"],
+ deps = all_crate_deps(normal = True),
+)
+
+rust_doc(
+ name = "all_crate_deps_doc",
+ crate = ":all_crate_deps",
+)
+
+sh_test(
+ name = "all_crate_deps_test",
+ size = "small",
+ srcs = ["all_crate_deps_test.sh"],
+ args = [
+ "$(rlocationpath :all_crate_deps)",
+ ],
+ data = [
+ ":all_crate_deps",
+ ],
+ deps = [
+ "@bazel_tools//tools/bash/runfiles",
+ ],
+)
+
+build_test(
+ name = "gen_rust_project",
+ targets = [
+ "@rules_rust//tools/rust_analyzer:gen_rust_project",
+ ],
+)
+
+build_test(
+ name = "rust_fmt",
+ targets = [
+ "@rules_rust//:rustfmt",
+ ],
+)
diff --git a/examples/bzlmod/all_crate_deps/Cargo.lock b/examples/bzlmod/all_crate_deps/Cargo.lock
new file mode 100644
index 00000000..3471b64a
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/Cargo.lock
@@ -0,0 +1,16 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "all_crate_deps"
+version = "0.0.0"
+dependencies = [
+ "anyhow",
+]
+
+[[package]]
+name = "anyhow"
+version = "1.0.79"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca"
diff --git a/examples/bzlmod/all_crate_deps/Cargo.toml b/examples/bzlmod/all_crate_deps/Cargo.toml
new file mode 100644
index 00000000..4ae9c363
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/Cargo.toml
@@ -0,0 +1,12 @@
+[workspace]
+[package]
+name = "all_crate_deps"
+version = "0.0.0"
+edition = "2021"
+publish = false
+
+[lib]
+path = "/dev/null"
+
+[dependencies]
+anyhow = "1.0.79"
diff --git a/examples/bzlmod/all_crate_deps/MODULE.bazel b/examples/bzlmod/all_crate_deps/MODULE.bazel
new file mode 100644
index 00000000..63148904
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/MODULE.bazel
@@ -0,0 +1,40 @@
+"""bazelbuild/rules_rust - bzlmod example"""
+
+module(
+ name = "all_crate_deps_bzlmod_example",
+ version = "0.0.0",
+)
+
+bazel_dep(name = "platforms", version = "0.0.8")
+bazel_dep(
+ name = "bazel_skylib",
+ version = "1.5.0",
+)
+bazel_dep(
+ name = "rules_rust",
+ version = "0.0.0",
+)
+local_path_override(
+ module_name = "rules_rust",
+ path = "../../..",
+)
+
+rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
+rust.toolchain(edition = "2021")
+use_repo(
+ rust,
+ "rust_toolchains",
+)
+
+register_toolchains("@rust_toolchains//:all")
+
+crate = use_extension(
+ "@rules_rust//crate_universe:extension.bzl",
+ "crate",
+)
+crate.from_cargo(
+ name = "crates",
+ cargo_lockfile = "//:Cargo.lock",
+ manifests = ["//:Cargo.toml"],
+)
+use_repo(crate, "crates")
diff --git a/examples/bzlmod/all_crate_deps/WORKSPACE.bazel b/examples/bzlmod/all_crate_deps/WORKSPACE.bazel
new file mode 100644
index 00000000..b543b79e
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/WORKSPACE.bazel
@@ -0,0 +1 @@
+# Intentionally blank; using bzlmod
diff --git a/examples/bzlmod/all_crate_deps/WORKSPACE.bzlmod b/examples/bzlmod/all_crate_deps/WORKSPACE.bzlmod
new file mode 100644
index 00000000..8e081c0b
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/WORKSPACE.bzlmod
@@ -0,0 +1 @@
+# Intentionally blank; enable strict mode for bzlmod
diff --git a/examples/bzlmod/all_crate_deps/all_crate_deps_test.sh b/examples/bzlmod/all_crate_deps/all_crate_deps_test.sh
new file mode 100755
index 00000000..f0efdf02
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/all_crate_deps_test.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+
+# --- begin runfiles.bash initialization v3 ---
+# Copy-pasted from the Bazel Bash runfiles library v3.
+set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$0.runfiles/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v3 ---
+
+
+set -euo pipefail
+
+# MARK - Functions
+
+fail() {
+ echo >&2 "$@"
+ exit 1
+}
+
+# MARK - Args
+
+if [[ "$#" -ne 1 ]]; then
+ fail "Usage: $0 /path/to/hello_world"
+fi
+HELLO_WORLD="$(rlocation "$1")"
+
+# MARK - Test
+
+OUTPUT="$("${HELLO_WORLD}")"
+[[ "${OUTPUT}" == "Hello, world!" ]] ||
+ fail 'Expected "Hello, world!", but was' "${OUTPUT}"
diff --git a/examples/bzlmod/all_crate_deps/src/main.rs b/examples/bzlmod/all_crate_deps/src/main.rs
new file mode 100644
index 00000000..1aa867e3
--- /dev/null
+++ b/examples/bzlmod/all_crate_deps/src/main.rs
@@ -0,0 +1,18 @@
+// Copyright 2015 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+fn main() -> anyhow::Result<()> {
+ println!("Hello, world!");
+ Ok(())
+}