diff --git a/examples/bzlmod/hello_world/BUILD.bazel b/examples/bzlmod/hello_world/BUILD.bazel index 0d1dc04979..e912b2d928 100644 --- a/examples/bzlmod/hello_world/BUILD.bazel +++ b/examples/bzlmod/hello_world/BUILD.bazel @@ -3,7 +3,15 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc") package(default_visibility = ["//visibility:public"]) rust_binary( - name = "hello_world", + name = "hello_world_transient", + srcs = ["src/main.rs"], + deps = [ + "@crates//:anyhow", + ], +) + +rust_binary( + name = "hello_world_vendored", srcs = ["src/main.rs"], deps = [ "//third-party/crates:anyhow", @@ -19,10 +27,12 @@ sh_test( name = "hello_world_test", srcs = ["hello_world_test.sh"], args = [ - "$(rlocationpath :hello_world)", + "$(rlocationpath :hello_world_transient)", + "$(rlocationpath :hello_world_vendored)", ], data = [ - ":hello_world", + ":hello_world_transient", + ":hello_world_vendored", ], deps = [ "@bazel_tools//tools/bash/runfiles", diff --git a/examples/bzlmod/hello_world/MODULE.bazel b/examples/bzlmod/hello_world/MODULE.bazel index 1b4cbf9bac..ad55387c75 100644 --- a/examples/bzlmod/hello_world/MODULE.bazel +++ b/examples/bzlmod/hello_world/MODULE.bazel @@ -5,15 +5,25 @@ module( version = "0.0.0", ) -bazel_dep(name = "bazel_skylib", version = "1.5.0") -bazel_dep(name = "rules_rust", 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", path = "../../..", ) rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") + rust.toolchain(edition = "2021") + use_repo( rust, "rust_toolchains", @@ -21,5 +31,23 @@ use_repo( register_toolchains("@rust_toolchains//:all") +# To do third party dependencies, you have multiple options: + +# Option 1: Fully transient (Cargo.toml / Cargo.lock as source of truth). +crate = use_extension( + "@rules_rust//crate_universe:extension.bzl", + "crate", +) + +crate.from_cargo( + name = "crates", + cargo_lockfile = "//third-party:Cargo.lock", + manifests = ["//third-party:Cargo.toml"], +) + +use_repo(crate, "crates") + +# Option 2: Vendored crates crate_repositories = use_extension("//third-party:extension.bzl", "crate_repositories") + use_repo(crate_repositories, "vendor__anyhow-1.0.77") diff --git a/examples/bzlmod/hello_world/annotations.json b/examples/bzlmod/hello_world/annotations.json new file mode 100644 index 0000000000..3d5102e9fa --- /dev/null +++ b/examples/bzlmod/hello_world/annotations.json @@ -0,0 +1,13 @@ +{ + "env_logger": [ + { + "version": "0.9.2", + "additive_build_file_content": "\n# Extra stuff here" + } + ], + "log": [ + { + "additive_build_file_content": "\n# Extra stuff here" + } + ] +} \ No newline at end of file diff --git a/examples/bzlmod/hello_world/hello_world_test.sh b/examples/bzlmod/hello_world/hello_world_test.sh index f0efdf02ca..5b97626c36 100755 --- a/examples/bzlmod/hello_world/hello_world_test.sh +++ b/examples/bzlmod/hello_world/hello_world_test.sh @@ -23,13 +23,18 @@ fail() { # MARK - Args -if [[ "$#" -ne 1 ]]; then - fail "Usage: $0 /path/to/hello_world" +if [[ "$#" -ne 2 ]]; then + fail "Usage: $0 /path/to/hello_world_transient /path/to/hello_world_vendored" fi -HELLO_WORLD="$(rlocation "$1")" +HELLO_WORLD_TRANSIENT="$(rlocation "$1")" +HELLO_WORLD_VENDORED="$(rlocation "$2")" # MARK - Test -OUTPUT="$("${HELLO_WORLD}")" +OUTPUT="$("${HELLO_WORLD_TRANSIENT}")" +[[ "${OUTPUT}" == "Hello, world!" ]] || + fail 'Expected "Hello, world!", but was' "${OUTPUT}" + +OUTPUT="$("${HELLO_WORLD_VENDORED}")" [[ "${OUTPUT}" == "Hello, world!" ]] || fail 'Expected "Hello, world!", but was' "${OUTPUT}"