Skip to content

Commit

Permalink
Expose crates_vendor deps to bzlmod (#2372)
Browse files Browse the repository at this point in the history
Without this, I have been unable to update
https://github.com/dtolnay/cxx to bzlmod. When I run `bazel run
//third-party:vendor` ([this
rule](https://github.com/dtolnay/cxx/blob/3c1bcc012f285dc59d39143e658df67659142cd1/third-party/BUILD#L3-L11))
it failed with **"unknown repo 'cargo_bazel.buildifier-linux-amd64'
requested from @@rules_rust~0.35.0"**:

```console
ERROR: no such package '@@[unknown repo 'cargo_bazel.buildifier-linux-amd64' requested from @@rules_rust~0.35.0]//file': The repository '@@[unknown repo 'cargo_bazel.buildifier-linux-amd64' requested from @@rules_rust~0.35.0]' could not be resolved: No repository visible as '@cargo_bazel.buildifier-linux-amd64' from repository '@@rules_rust~0.35.0'
ERROR: ~/.cache/bazel/_bazel_david/ebce1d0721fb68dda9c70c0dd1405803/external/rules_rust~0.35.0/crate_universe/private/vendor/BUILD.bazel:3:27: no such package '@@[unknown repo 'cargo_bazel.buildifier-linux-amd64' requested from @@rules_rust~0.35.0]//file': The repository '@@[unknown repo 'cargo_bazel.buildifier-linux-amd64' requested from @@rules_rust~0.35.0]' could not be resolved: No repository visible as '@cargo_bazel.buildifier-linux-amd64' from repository '@@rules_rust~0.35.0' and referenced by '@@rules_rust~0.35.0//crate_universe/private/vendor:buildifier'
ERROR: Analysis of target '//third-party:vendor' failed; build aborted: Analysis failed
```

After this change, it works. My MODULE.bazel can be seen in
dtolnay/cxx#1294.
  • Loading branch information
dtolnay authored Dec 28, 2023
1 parent 0d77db7 commit 5001ff6
Show file tree
Hide file tree
Showing 20 changed files with 745 additions and 14,838 deletions.
4 changes: 4 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,16 @@ tasks:
name: bzlmod BCR presubmit
platform: ubuntu2004
working_directory: examples/bzlmod/hello_world
run_targets:
- "//third-party:vendor"
test_targets:
- "//..."
macos_bzlmod_bcr:
name: bzlmod BCR presubmit
platform: macos
working_directory: examples/bzlmod/hello_world
run_targets:
- "//third-party:vendor"
test_targets:
- "//..."

Expand Down
2 changes: 2 additions & 0 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ bcr_test_module:
working_directory: examples/bzlmod/hello_world
name: "Run test module"
platform: ${{ platform }}
run_targets:
- "//third-party:vendor"
test_targets:
- "//..."
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
bindgen/3rdparty/crates/** linguist-generated
crate_universe/3rdparty/crates/** linguist-generated
crate_universe/test_data/metadata/*/metadata.json linguist-generated
examples/bzlmod/hello_world/third-party/crates/** linguist-generated
examples/crate_universe/vendor_external/crates/** linguist-generated
examples/crate_universe/vendor_local_manifests/crates/** linguist-generated
examples/crate_universe/vendor_local_pkgs/crates/** linguist-generated
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ internal_deps = use_extension("//rust/private:extensions.bzl", "internal_deps")
use_repo(
internal_deps,
"bazelci_rules",
"cargo_bazel.buildifier-darwin-amd64",
"cargo_bazel.buildifier-darwin-arm64",
"cargo_bazel.buildifier-linux-amd64",
"cargo_bazel.buildifier-linux-arm64",
"cargo_bazel.buildifier-windows-amd64.exe",
"com_google_googleapis",
"cui",
"cui__anyhow-1.0.75",
Expand Down
13 changes: 12 additions & 1 deletion crate_universe/private/vendor_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ _BUILDIFIER_SHA256S = {
}

def crates_vendor_deps():
"""Define dependencies of the `crates_vendor` rule
Returns:
list[struct(repo=str, is_dev_dep=bool)]: List of the dependency repositories.
"""
direct_deps = []

for bin, sha256 in _BUILDIFIER_SHA256S.items():
repo = "cargo_bazel.{}".format(bin)
maybe(
http_file,
name = "cargo_bazel.{}".format(bin),
name = repo,
urls = [_BUILDIFIER_URL_TEMPLATE.format(
bin = bin,
version = _BUILDIFIER_VERSION,
Expand All @@ -26,6 +34,9 @@ def crates_vendor_deps():
downloaded_file_path = "buildifier.exe",
executable = True,
)
direct_deps.append(struct(repo = repo, is_dev_dep = False))

return direct_deps

# buildifier: disable=unnamed-macro
def crates_vendor_deps_targets():
Expand Down
3 changes: 1 addition & 2 deletions crate_universe/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def crate_universe_dependencies(rust_version = rust_common.default_version, boot
cargo_bazel_bootstrap(rust_version = rust_version, **kwargs)

direct_deps = _vendor_crate_repositories()

crates_vendor_deps()
direct_deps.extend(crates_vendor_deps())
cross_installer_deps()
return direct_deps
2 changes: 2 additions & 0 deletions examples/bzlmod/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bazel-*
/MODULE.bazel.lock
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
4 changes: 4 additions & 0 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 +20,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")
Loading

0 comments on commit 5001ff6

Please sign in to comment.