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

Fix alias_rules handling in bzlmod and port alias_rule examples to bz… #3158

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions crate_universe/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,9 @@ def _collect_render_config(module, repository):

config = None
for raw_config in module.tags.render_config:
if not raw_config.repositories:
continue

if not repository in raw_config.repositories:
# if the repositories is empty we apply the config to all repositories
# otherwise we filter for the requested repositories
if raw_config.repositories and repository not in raw_config.repositories:
continue

if config:
Expand Down Expand Up @@ -582,6 +581,7 @@ def _generate_hub_and_spokes(*, module_ctx, cargo_bazel, cfg, annotations, rende
_generate_repo(
name = cfg.name,
contents = {
"alias_rules.bzl": module_ctx.read(crates_dir.get_child("alias_rules.bzl")),
"BUILD.bazel": module_ctx.read(crates_dir.get_child("BUILD.bazel")),
"defs.bzl": module_ctx.read(crates_dir.get_child("defs.bzl")),
},
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/private/common_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def parse_alias_rule(value):
Returns:
value: A Rust compatible `AliasRule`.
"""
if value == None:
if not value:
return None

if value == "alias" or value == "dbg" or value == "fastbuild" or value == "opt":
Expand Down
38 changes: 38 additions & 0 deletions examples/crate_universe_bzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# `.bazelrc` is a Bazel configuration file.
# https://bazel.build/docs/best-practices#bazelrc-file

# Required on windows
common --enable_platform_specific_config
startup --windows_enable_symlinks
build:windows --enable_runfiles

# we need isolated extensions to be able to specify multiple repositories within the same module
common --experimental_isolated_extension_usages

# some dependencies wants to use /tmp dir which is not always writable
build --repo_env TMPDIR=/var/tmp

# Enable rustfmt for all targets in the workspace
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
build:rustfmt --output_groups=+rustfmt_checks

# Enable clippy for all targets in the workspace
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:clippy --output_groups=+clippy_checks

# Confirm crate universe works without relying on the legacy
# external symlink in runfiles.
build --nolegacy_external_runfiles

# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips.
build --incompatible_disallow_empty_glob

# Required for cargo_build_script support before Bazel 7
build --incompatible_merge_fixed_and_default_shell_env

# https://github.com/bazelbuild/bazel/issues/23043.
build --incompatible_autoload_externally=

# This import should always be last to allow users to override
# settings for local development.
try-import %workspace%/user.bazelrc
1 change: 1 addition & 0 deletions examples/crate_universe_bzlmod/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.1
Empty file.
26 changes: 26 additions & 0 deletions examples/crate_universe_bzlmod/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Developing crate_universe examples

## Bootstrapping

Crate Universe examples require a bootstrapping process the first time
`rules_rust` is checked out. To setup the examples, run the following
bazel commands:

```shell
bazel run //vendor_external:crates_vendor
bazel run //vendor_local_manifests:crates_vendor
bazel run //vendor_local_pkgs:crates_vendor
bazel run //vendor_remote_manifests:crates_vendor
bazel run //vendor_remote_pkgs:crates_vendor
```

## Repinning/Updating Dependencies

After bootstrapping, Bazel lockfiles can be regenerated by running the following:

```shell
CARGO_BAZEL_REPIN=1 bazel test //...
```

For more information on repinning, see: https://bazelbuild.github.io/rules_rust/crate_universe.html#repinning--updating-dependencies

24 changes: 24 additions & 0 deletions examples/crate_universe_bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Contains crate_universe usage example dependencies
"""

bazel_dep(name = "rules_rust", version = "0.54.1")
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")

###############################################################################
# A L I A S R U L E
###############################################################################

include("//alias_rule:sub.MODULE.bazel")
3 changes: 3 additions & 0 deletions examples/crate_universe_bzlmod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Examples

`crate_universe` examples based on bzlmod. These examples supports multiple repositories within the same module in an isolated way.
43 changes: 43 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("@rules_rust//rust:defs.bzl", "rust_test")

rust_test(
name = "global_alias_annotation_none",
srcs = ["src/fastbuild.rs"],
deps = ["@alias_rule_global_alias_annotation_none//:test_data_passing_crate"],
)

rust_test(
name = "global_alias_annotation_opt",
srcs = ["src/opt.rs"],
deps = ["@alias_rule_global_alias_annotation_opt//:test_data_passing_crate"],
)

rust_test(
name = "global_opt_annotation_none",
srcs = ["src/opt.rs"],
deps = ["@alias_rule_global_opt_annotation_none//:test_data_passing_crate"],
)

rust_test(
name = "global_opt_annotation_alias",
srcs = ["src/fastbuild.rs"],
deps = ["@alias_rule_global_opt_annotation_alias//:test_data_passing_crate"],
)

rust_test(
name = "global_opt_annotation_dbg",
srcs = ["src/dbg.rs"],
deps = ["@alias_rule_global_opt_annotation_dbg//:test_data_passing_crate"],
)

rust_test(
name = "global_dbg_annotation_fastbuild",
srcs = ["src/fastbuild.rs"],
deps = ["@alias_rule_global_dbg_annotation_fastbuild//:test_data_passing_crate"],
)

rust_test(
name = "global_custom_annotation_none",
srcs = ["src/fastbuild.rs"],
deps = ["@alias_rule_global_custom_annotation_none//:test_data_passing_crate"],
)
8 changes: 8 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/alias_rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Wrapper around `native.alias()` to test supplying a custom `alias_rule`."""

def alias_rule(name, actual, tags):
native.alias(
name = name,
actual = actual,
tags = tags,
)
9 changes: 9 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/src/dbg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn out_dir() {
assert!(test_data_passing_crate::get_out_dir().contains("-dbg"));
}

#[test]
fn opt_level() {
assert_eq!(test_data_passing_crate::get_opt_level(), "0");
}
9 changes: 9 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/src/fastbuild.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn out_dir() {
assert!(test_data_passing_crate::get_out_dir().contains("-fastbuild"));
}

#[test]
fn opt_level() {
assert_eq!(test_data_passing_crate::get_opt_level(), "0");
}
9 changes: 9 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/src/opt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn out_dir() {
assert!(test_data_passing_crate::get_out_dir().contains("-opt"));
}

#[test]
fn opt_level() {
assert_eq!(test_data_passing_crate::get_opt_level(), "3");
}
57 changes: 57 additions & 0 deletions examples/crate_universe_bzlmod/alias_rule/sub.MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Partial MODULE.bazel for alias_rule"""

# Repository for test alias_rule_global_alias_annotation_none
gaan = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
gaan.spec(package = "test_data_passing_crate", version = "0.1.0")
gaan.annotation(crate = "test_data_passing_crate", alias_rule = "")
gaan.render_config(default_alias_rule = "alias")
gaan.from_specs(name = "alias_rule_global_alias_annotation_none")
use_repo(gaan, "alias_rule_global_alias_annotation_none")

# Repository for test alias_rule_global_alias_annotation_opt
gaao = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
gaao.spec(package = "test_data_passing_crate", version = "0.1.0")
gaao.annotation(crate = "test_data_passing_crate", alias_rule = "opt")
gaao.render_config(default_alias_rule = "alias")
gaao.from_specs(name = "alias_rule_global_alias_annotation_opt")
use_repo(gaao, "alias_rule_global_alias_annotation_opt")

# Repository for test alias_rule_global_custom_annotation_none
gcan = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
gcan.spec(package = "test_data_passing_crate", version = "0.1.0")
gcan.annotation(crate = "test_data_passing_crate", alias_rule = "")
gcan.render_config(default_alias_rule = "@@//alias_rule:alias_rules.bzl:alias_rule")
gcan.from_specs(name = "alias_rule_global_custom_annotation_none")
use_repo(gcan, "alias_rule_global_custom_annotation_none")

# Repository for test alias_rule_global_dbg_annotation_fastbuild
gdaf = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
gdaf.spec(package = "test_data_passing_crate", version = "0.1.0")
gdaf.annotation(crate = "test_data_passing_crate", alias_rule = "fastbuild")
gdaf.render_config(default_alias_rule = "dbg")
gdaf.from_specs(name = "alias_rule_global_dbg_annotation_fastbuild")
use_repo(gdaf, "alias_rule_global_dbg_annotation_fastbuild")

# Repository for test alias_rule_global_opt_annotation_alias
goaa = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
goaa.spec(package = "test_data_passing_crate", version = "0.1.0")
goaa.annotation(crate = "test_data_passing_crate", alias_rule = "alias")
goaa.render_config(default_alias_rule = "opt")
goaa.from_specs(name = "alias_rule_global_opt_annotation_alias")
use_repo(goaa, "alias_rule_global_opt_annotation_alias")

# Repository for test alias_rule_global_opt_annotation_dbg
goad = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
goad.spec(package = "test_data_passing_crate", version = "0.1.0")
goad.annotation(crate = "test_data_passing_crate", alias_rule = "dbg")
goad.render_config(default_alias_rule = "opt")
goad.from_specs(name = "alias_rule_global_opt_annotation_dbg")
use_repo(goad, "alias_rule_global_opt_annotation_dbg")

# Repository for test alias_rule_global_alias_annotation_none
goan = use_extension("@rules_rust//crate_universe:extension.bzl", "crate", isolate = True)
goan.spec(package = "test_data_passing_crate", version = "0.1.0")
goan.annotation(crate = "test_data_passing_crate", alias_rule = "")
goan.render_config(default_alias_rule = "opt")
goan.from_specs(name = "alias_rule_global_opt_annotation_none")
use_repo(goan, "alias_rule_global_opt_annotation_none")
Loading