From ccacf8f609ec1724edaeaa815ccbd8d6313a3eb6 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Sun, 24 Nov 2024 13:34:04 +0000 Subject: [PATCH] Add example with a non-workspace Cargo.toml --- examples/bzlmod/hello_world/BUILD.bazel | 28 +++++++++++++------ examples/bzlmod/hello_world/MODULE.bazel | 10 ++++++- .../third-party-without-workspace/BUILD.bazel | 0 .../third-party-without-workspace/Cargo.lock | 16 +++++++++++ .../third-party-without-workspace/Cargo.toml | 11 ++++++++ 5 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 examples/bzlmod/hello_world/third-party-without-workspace/BUILD.bazel create mode 100644 examples/bzlmod/hello_world/third-party-without-workspace/Cargo.lock create mode 100644 examples/bzlmod/hello_world/third-party-without-workspace/Cargo.toml diff --git a/examples/bzlmod/hello_world/BUILD.bazel b/examples/bzlmod/hello_world/BUILD.bazel index 5170b2a579..fb4b7501cc 100644 --- a/examples/bzlmod/hello_world/BUILD.bazel +++ b/examples/bzlmod/hello_world/BUILD.bazel @@ -19,34 +19,44 @@ rust_binary( ], ) +rust_binary( + name = "hello_world_without_workspace_transient", + srcs = ["src/main.rs"], + deps = [ + "@crates_without_workspace//:anyhow", + ], +) + [ rust_doc( - name = "hello_world_from_workspace_{}_doc".format(target), - crate = ":hello_world_from_workspace_{}".format(target), + name = "hello_world_{}_doc".format(target), + crate = ":hello_world_{}".format(target), ) for target in [ - "transient", - "vendored", + "from_workspace_transient", + "from_workspace_vendored", + "without_workspace_transient", ] ] [ sh_test( - name = "hello_world_from_workspace_{}_test".format(target), + name = "hello_world_{}_test".format(target), srcs = ["hello_world_test.sh"], args = [ - "$(rlocationpath :hello_world_from_workspace_{})".format(target), + "$(rlocationpath :hello_world_{})".format(target), ], data = [ - ":hello_world_from_workspace_{}".format(target), + ":hello_world_{}".format(target), ], deps = [ "@bazel_tools//tools/bash/runfiles", ], ) for target in [ - "transient", - "vendored", + "from_workspace_transient", + "from_workspace_vendored", + "without_workspace_transient", ] ] diff --git a/examples/bzlmod/hello_world/MODULE.bazel b/examples/bzlmod/hello_world/MODULE.bazel index 1ef959c1e1..794dd27efe 100644 --- a/examples/bzlmod/hello_world/MODULE.bazel +++ b/examples/bzlmod/hello_world/MODULE.bazel @@ -39,7 +39,7 @@ crate.annotation( data = [":cargo_toml"], # Optional, you probably don't need this. Defaults to all from_cargo # invocations in this module. - repositories = ["crates_in_workspace"], + repositories = ["crates_in_workspace", "crates_without_workspace"], # Optional, you probably don't need this, defaults to "*". version = "*", ) @@ -47,3 +47,11 @@ crate.annotation( # Option 2: Vendored crates crate_repositories = use_extension("//third-party-in-workspace:extension.bzl", "crate_repositories") use_repo(crate_repositories, "vendor__anyhow-1.0.77") + +# Another example of Option 1, but where the Cargo.toml file isn't a [workspace] +crate.from_cargo( + name = "crates_without_workspace", + cargo_lockfile = "//third-party-without-workspace:Cargo.lock", + manifests = ["//third-party-without-workspace:Cargo.toml"], +) +use_repo(crate, "crates_without_workspace") diff --git a/examples/bzlmod/hello_world/third-party-without-workspace/BUILD.bazel b/examples/bzlmod/hello_world/third-party-without-workspace/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.lock b/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.lock new file mode 100644 index 0000000000..4123cbf49a --- /dev/null +++ b/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9" + +[[package]] +name = "third-party" +version = "0.0.0" +dependencies = [ + "anyhow", +] diff --git a/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.toml b/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.toml new file mode 100644 index 0000000000..9a1fdf5735 --- /dev/null +++ b/examples/bzlmod/hello_world/third-party-without-workspace/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "third-party" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +path = "fake.rs" + +[dependencies] +anyhow = "1.0.77"