From 1374678997ca4d3ac6ab927cb0a1fe9753882632 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Wed, 4 Dec 2024 17:03:49 -0800 Subject: [PATCH] Allow rust_wasm_bindgen rules to re-expose rust-analyzer providers (#3035) This is useful for targets which are gated behind `target_compatible_with = ["@platforms//os:wasm32"]` but developers still want them to analyzed by rust-analyzer. --- docs/src/rust_wasm_bindgen_rules_js.md | 2 +- .../wasm_bindgen/private/wasm_bindgen.bzl | 37 ++++++++++++------- extensions/wasm_bindgen/rules_js/defs.bzl | 15 +++++++- extensions/wasm_bindgen/rules_nodejs/defs.bzl | 15 +++++++- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/docs/src/rust_wasm_bindgen_rules_js.md b/docs/src/rust_wasm_bindgen_rules_js.md index c358bccd0e..9ed570c74d 100644 --- a/docs/src/rust_wasm_bindgen_rules_js.md +++ b/docs/src/rust_wasm_bindgen_rules_js.md @@ -21,7 +21,7 @@ Generates javascript and typescript bindings for a webassembly module using [was | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` | +| bindgen_flags | Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` | | target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | `"bundler"` | | target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` | | wasm_file | The `.wasm` file or crate to generate bindings for. | Label | required | | diff --git a/extensions/wasm_bindgen/private/wasm_bindgen.bzl b/extensions/wasm_bindgen/private/wasm_bindgen.bzl index c0336055d2..a3ee605914 100644 --- a/extensions/wasm_bindgen/private/wasm_bindgen.bzl +++ b/extensions/wasm_bindgen/private/wasm_bindgen.bzl @@ -1,21 +1,24 @@ """Bazel rules for [wasm-bindgen](https://crates.io/crates/wasm-bindgen)""" load("@rules_rust//rust:defs.bzl", "rust_common") + +# buildifier: disable=bzl-visibility +load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo") load("//:providers.bzl", "RustWasmBindgenInfo") load(":transitions.bzl", "wasm_bindgen_transition") -def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_flags = []): +def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags = []): """Spawn a `RustWasmBindgen` action. Args: - ctx (ctx): _description_ - toolchain (ToolchainInfo): _description_ - wasm_file (Target): _description_ + ctx (ctx): The rule's context object. + toolchain (ToolchainInfo): The current `rust_wasm_bindgen_toolchain`. + wasm_file (Target): The target representing the `.wasm` file. target_output (str): _description_ - bindgen_flags (list, optional): _description_. Defaults to []. + flags (list, optional): Flags to pass to `wasm-bindgen`. Returns: - RustWasmBindgenInfo: _description_ + RustWasmBindgenInfo: A provider containing action outputs. """ bindgen_bin = toolchain.bindgen @@ -54,12 +57,12 @@ def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_f js_out = [ctx.actions.declare_file(ctx.label.name + ".js")] ts_out = [] - if not "--no-typescript" in bindgen_flags: + if not "--no-typescript" in flags: ts_out.append(ctx.actions.declare_file(ctx.label.name + ".d.ts")) if target_output == "bundler": js_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.js")) - if not "--no-typescript" in bindgen_flags: + if not "--no-typescript" in flags: ts_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.wasm.d.ts")) outputs = [bindgen_wasm_module] + js_out + ts_out @@ -68,7 +71,7 @@ def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_f args.add("--target", target_output) args.add("--out-dir", bindgen_wasm_module.dirname) args.add("--out-name", ctx.label.name) - args.add_all(bindgen_flags) + args.add_all(flags) args.add(input_file) ctx.actions.run( @@ -76,7 +79,7 @@ def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_f inputs = [input_file], outputs = outputs, mnemonic = "RustWasmBindgen", - progress_message = "Generating WebAssembly bindings for {}...".format(progress_message_label), + progress_message = "Generating WebAssembly bindings for {}".format(progress_message_label), arguments = [args], toolchain = str(Label("//:toolchain_type")), ) @@ -95,19 +98,27 @@ def _rust_wasm_bindgen_impl(ctx): toolchain = toolchain, wasm_file = ctx.attr.wasm_file, target_output = ctx.attr.target, - bindgen_flags = ctx.attr.bindgen_flags, + flags = ctx.attr.bindgen_flags, ) - return [ + providers = [ DefaultInfo( files = depset([info.wasm], transitive = [info.js, info.ts]), ), info, ] + if RustAnalyzerGroupInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo]) + + if RustAnalyzerInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerInfo]) + + return providers + WASM_BINDGEN_ATTR = { "bindgen_flags": attr.string_list( - doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.", + doc = "Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.", ), "target": attr.string( doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.", diff --git a/extensions/wasm_bindgen/rules_js/defs.bzl b/extensions/wasm_bindgen/rules_js/defs.bzl index aa42e7142b..70dc0a9813 100644 --- a/extensions/wasm_bindgen/rules_js/defs.bzl +++ b/extensions/wasm_bindgen/rules_js/defs.bzl @@ -1,6 +1,9 @@ """Rust WASM-bindgen rules for interfacing with aspect-build/rules_js""" load("@aspect_rules_js//js:providers.bzl", "js_info") + +# buildifier: disable=bzl-visibility +load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo") load("//private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action") def _js_rust_wasm_bindgen_impl(ctx): @@ -11,10 +14,10 @@ def _js_rust_wasm_bindgen_impl(ctx): toolchain = toolchain, wasm_file = ctx.attr.wasm_file, target_output = ctx.attr.target, - bindgen_flags = ctx.attr.bindgen_flags, + flags = ctx.attr.bindgen_flags, ) - return [ + providers = [ DefaultInfo( files = depset([info.wasm], transitive = [info.js, info.ts]), ), @@ -29,6 +32,14 @@ def _js_rust_wasm_bindgen_impl(ctx): ), ] + if RustAnalyzerGroupInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo]) + + if RustAnalyzerInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerInfo]) + + return providers + js_rust_wasm_bindgen = rule( doc = """\ Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs]. diff --git a/extensions/wasm_bindgen/rules_nodejs/defs.bzl b/extensions/wasm_bindgen/rules_nodejs/defs.bzl index 081086d516..784b78de88 100644 --- a/extensions/wasm_bindgen/rules_nodejs/defs.bzl +++ b/extensions/wasm_bindgen/rules_nodejs/defs.bzl @@ -1,6 +1,9 @@ """Rust WASM-bindgen rules for interfacing with bazelbuild/rules_nodejs""" load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo") + +# buildifier: disable=bzl-visibility +load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo") load("//private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action") def _nodejs_rust_wasm_bindgen_impl(ctx): @@ -11,14 +14,14 @@ def _nodejs_rust_wasm_bindgen_impl(ctx): toolchain = toolchain, wasm_file = ctx.attr.wasm_file, target_output = ctx.attr.target, - bindgen_flags = ctx.attr.bindgen_flags, + flags = ctx.attr.bindgen_flags, ) # Return a structure that is compatible with the deps[] of a ts_library. declarations = info.ts es5_sources = info.js - return [ + providers = [ DefaultInfo( files = depset([info.wasm], transitive = [info.js, info.ts]), ), @@ -34,6 +37,14 @@ def _nodejs_rust_wasm_bindgen_impl(ctx): info, ] + if RustAnalyzerGroupInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo]) + + if RustAnalyzerInfo in ctx.attr.wasm_file: + providers.append(ctx.attr.wasm_file[RustAnalyzerInfo]) + + return providers + nodejs_rust_wasm_bindgen = rule( doc = """\ Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [bazelbuild/rules_nodejs][bbnjs].