diff --git a/MODULE.bazel b/MODULE.bazel index 4a6ebadeb2..9669a55123 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -164,17 +164,14 @@ register_toolchains( register_toolchains( "//proto/protobuf:default-proto-toolchain", - dev_dependency = True, ) register_toolchains( "//proto/prost:default_prost_toolchain", - dev_dependency = True, ) register_toolchains( "//bindgen:default_bindgen_toolchain", - dev_dependency = True, ) rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools") diff --git a/examples/bzlmod/hello_world/MODULE.bazel b/examples/bzlmod/hello_world/MODULE.bazel index 121281c558..24a4bba802 100644 --- a/examples/bzlmod/hello_world/MODULE.bazel +++ b/examples/bzlmod/hello_world/MODULE.bazel @@ -19,15 +19,6 @@ local_path_override( path = "../../..", ) -rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") -rust.toolchain(edition = "2021") -use_repo( - rust, - "rust_toolchains", -) - -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). diff --git a/examples/bzlmod/hello_world/MODULE.bazel.lock b/examples/bzlmod/hello_world/MODULE.bazel.lock index f7cef6ce78..33290d22ac 100644 --- a/examples/bzlmod/hello_world/MODULE.bazel.lock +++ b/examples/bzlmod/hello_world/MODULE.bazel.lock @@ -1,6 +1,6 @@ { "lockFileVersion": 6, - "moduleFileHash": "97fc4632c12fa1c1dc76d29301d11258c4d118d7080c0caf767b093b05361be0", + "moduleFileHash": "626730f6bfed207274c0f53502d56fd955e96d92ecd46b177e51164bd7c88cca", "flags": { "cmdRegistries": [ "https://bcr.bazel.build/" @@ -13,7 +13,7 @@ "compatibilityMode": "ERROR" }, "localOverrideHashes": { - "rules_rust": "b01d8fa2d0cadc2b4b427e5008dfef525aa9e17ddb781848b17f0af39e09c076", + "rules_rust": "87746193d9d8812a75923cc19772c2bb61b1ff870adfe6a70863a2df47188864", "bazel_tools": "1ae69322ac3823527337acf02016e8ee95813d8d356f47060255b8956fa642f0" }, "moduleDepGraph": { @@ -23,47 +23,15 @@ "key": "", "repoName": "hello_world_example", "executionPlatformsToRegister": [], - "toolchainsToRegister": [ - "@rust_toolchains//:all" - ], + "toolchainsToRegister": [], "extensionUsages": [ - { - "extensionBzlFile": "@rules_rust//rust:extensions.bzl", - "extensionName": "rust", - "usingModule": "", - "location": { - "file": "@@//:MODULE.bazel", - "line": 22, - "column": 21 - }, - "imports": { - "rust_toolchains": "rust_toolchains" - }, - "devImports": [], - "tags": [ - { - "tagName": "toolchain", - "attributeValues": { - "edition": "2021" - }, - "devDependency": false, - "location": { - "file": "@@//:MODULE.bazel", - "line": 23, - "column": 15 - } - } - ], - "hasDevUseExtension": false, - "hasNonDevUseExtension": true - }, { "extensionBzlFile": "@rules_rust//crate_universe:extension.bzl", "extensionName": "crate", "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 34, + "line": 25, "column": 22 }, "imports": { @@ -83,7 +51,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 38, + "line": 29, "column": 17 } }, @@ -103,7 +71,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 44, + "line": 35, "column": 17 } } @@ -117,7 +85,7 @@ "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 57, + "line": 48, "column": 35 }, "imports": { @@ -196,12 +164,15 @@ }, "rules_rust@_": { "name": "rules_rust", - "version": "0.42.0", + "version": "0.42.1", "key": "rules_rust@_", "repoName": "rules_rust", "executionPlatformsToRegister": [], "toolchainsToRegister": [ - "@rust_toolchains//:all" + "@rust_toolchains//:all", + "//proto/protobuf:default-proto-toolchain", + "//proto/prost:default_prost_toolchain", + "//bindgen:default_bindgen_toolchain" ], "extensionUsages": [ { @@ -350,7 +321,7 @@ "usingModule": "rules_rust@_", "location": { "file": "@@rules_rust~//:MODULE.bazel", - "line": 176, + "line": 173, "column": 32 }, "imports": { @@ -367,7 +338,7 @@ "usingModule": "rules_rust@_", "location": { "file": "@@rules_rust~//:MODULE.bazel", - "line": 179, + "line": 176, "column": 38 }, "imports": { @@ -2159,7 +2130,7 @@ }, "@@rules_rust~//rust:extensions.bzl%rust": { "general": { - "bzlTransitiveDigest": "fc5384f0cxBOMkpjo3iXF+BV2bz6qjpFTzUqS8we/JM=", + "bzlTransitiveDigest": "kaZnVIQi/tGg5Je230Rp7u/785j3/IFhhQPVChEJd6A=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/rust/extensions.bzl b/rust/extensions.bzl index eb8ea831b5..5430f5799b 100644 --- a/rust/extensions.bzl +++ b/rust/extensions.bzl @@ -11,47 +11,28 @@ load( "DEFAULT_STATIC_RUST_URL_TEMPLATES", ) -_EXAMPLE_TOOLCHAIN = """ -rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") -rust.toolchain( - edition = "2021", - versions = ["1.70.2"], -) -use_repo(rust, "rust_toolchains") -register_toolchains("@rust_toolchains//:all")""" - -_TRANSITIVE_DEP_ERR = """ -Your transitive dependency %s is using rules_rust, so you need to define a rust toolchain. -To do so, you will need to add the following to your root MODULE.bazel. For example: - -bazel_dep(name = "rules_rust", version = "") -""" + _EXAMPLE_TOOLCHAIN - -_TOOLCHAIN_ERR = """ -Please add at least one toolchain to your root MODULE.bazel. For example: -""" + _EXAMPLE_TOOLCHAIN - -def _find_root(module_ctx): +def _find_modules(module_ctx): root = None + our_module = None for mod in module_ctx.modules: if mod.is_root: root = mod + if mod.name == "rules_rust": + our_module = mod + if root == None: + root = our_module + if our_module == None: + fail("Unable to find rules_rust module") - return root + return root, our_module def _rust_impl(module_ctx): - # Toolchain configuration is only allowed in the root module. - # It would be very confusing (and a security concern) if I was using the - # default rust toolchains, then when I added a module built on rust, I was - # suddenly using a custom rustc. - root = _find_root(module_ctx) - - if not root: - fail(_TRANSITIVE_DEP_ERR % module_ctx.modules[0].name) + # Toolchain configuration is only allowed in the root module, or in + # rules_rust. + # See https://github.com/bazelbuild/bazel/discussions/22024 for discussion. + root, rules_rust = _find_modules(module_ctx) - toolchains = root.tags.toolchain - if not toolchains: - fail(_TOOLCHAIN_ERR) + toolchains = root.tags.toolchain or rules_rust.tags.toolchain for toolchain in toolchains: rust_register_toolchains( @@ -134,9 +115,9 @@ rust = module_extension( # This is a separate module extension so that only the host tools are # marked as reproducible and os and arch dependent def _rust_host_tools_impl(module_ctx): - root = _find_root(module_ctx) + root, _ = _find_modules(module_ctx) - if root != None and len(root.tags.host_tools) == 1: + if len(root.tags.host_tools) == 1: attrs = root.tags.host_tools[0] iso_date = None @@ -156,7 +137,7 @@ def _rust_host_tools_impl(module_ctx): "urls": attrs.urls, "version": version, } - elif root == None or not root.tags.host_tools: + elif not root.tags.host_tools: host_tools = { "version": rust_common.default_version, }