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

Add support for all_crate_deps when using bzlmod #2461

Merged
merged 7 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crate_universe/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")),
},
)

Expand Down
5 changes: 4 additions & 1 deletion crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions examples/bzlmod/all_crate_deps/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Required on windows
common --enable_platform_specific_config
startup --windows_enable_symlinks
build:windows --enable_runfiles

build --experimental_enable_bzlmod
2 changes: 2 additions & 0 deletions examples/bzlmod/all_crate_deps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bazel-*
/MODULE.bazel.lock
45 changes: 45 additions & 0 deletions examples/bzlmod/all_crate_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
16 changes: 16 additions & 0 deletions examples/bzlmod/all_crate_deps/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/bzlmod/all_crate_deps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
40 changes: 40 additions & 0 deletions examples/bzlmod/all_crate_deps/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions examples/bzlmod/all_crate_deps/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; using bzlmod
1 change: 1 addition & 0 deletions examples/bzlmod/all_crate_deps/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; enable strict mode for bzlmod
35 changes: 35 additions & 0 deletions examples/bzlmod/all_crate_deps/all_crate_deps_test.sh
Original file line number Diff line number Diff line change
@@ -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}"
18 changes: 18 additions & 0 deletions examples/bzlmod/all_crate_deps/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading