Skip to content

Commit

Permalink
Exercise crates_vendor in bzlmod hello_world example
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 28, 2023
1 parent c1d1847 commit 6190e52
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/bzlmod/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
3 changes: 3 additions & 0 deletions examples/bzlmod/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package(default_visibility = ["//visibility:public"])
rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
deps = [
"//third-party/crates:anyhow",
],
)

rust_doc(
Expand Down
5 changes: 5 additions & 0 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module(
version = "0.0.0",
)

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",
Expand All @@ -19,3 +21,6 @@ use_repo(
)

register_toolchains("@rust_toolchains//:all")

crate_repositories = use_extension("//third-party:extension.bzl", "crate_repositories")
use_repo(crate_repositories, "vendor__anyhow-1.0.77")
172 changes: 129 additions & 43 deletions examples/bzlmod/hello_world/MODULE.bazel.lock

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

3 changes: 2 additions & 1 deletion examples/bzlmod/hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
fn main() -> anyhow::Result<()> {
println!("Hello, world!");
Ok(())
}
10 changes: 10 additions & 0 deletions examples/bzlmod/hello_world/third-party/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_rust//crate_universe:defs.bzl", "crates_vendor")

crates_vendor(
name = "vendor",
cargo_lockfile = "//third-party:Cargo.lock",
generate_build_scripts = True,
manifests = ["//third-party:Cargo.toml"],
mode = "remote",
tags = ["manual"],
)
16 changes: 16 additions & 0 deletions examples/bzlmod/hello_world/third-party/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/hello_world/third-party/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
[package]
name = "third-party"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
path = "/dev/null"

[dependencies]
anyhow = "1.0.77"
14 changes: 14 additions & 0 deletions examples/bzlmod/hello_world/third-party/extension.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Module extensions for using vendored crates with bzlmod"""

load("//third-party/crates:defs.bzl", _crate_repositories = "crate_repositories")

def _crate_repositories_impl(module_ctx):
direct_deps = _crate_repositories()
return module_ctx.extension_metadata(
root_module_direct_deps = [repo.repo for repo in direct_deps],
root_module_direct_dev_deps = [],
)

crate_repositories = module_extension(
implementation = _crate_repositories_impl,
)

0 comments on commit 6190e52

Please sign in to comment.