Skip to content

Commit 43eb62d

Browse files
authored
Improved rust_wasm_bindgen error messaging (#914)
1 parent e2f1ab9 commit 43eb62d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

docs/flatten.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
13241324
| <a id="rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
13251325
| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | [] |
13261326
| <a id="rust_wasm_bindgen-target"></a>target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | "bundler" |
1327-
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The .wasm file to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
1327+
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The <code>.wasm</code> file or crate to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
13281328

13291329

13301330
<a id="#rust_wasm_bindgen_toolchain"></a>

docs/rust_wasm_bindgen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
5858
| <a id="rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
5959
| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | [] |
6060
| <a id="rust_wasm_bindgen-target"></a>target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | "bundler" |
61-
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The .wasm file to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
61+
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The <code>.wasm</code> file or crate to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
6262

6363

6464
<a id="#rust_wasm_bindgen_toolchain"></a>

wasm_bindgen/wasm_bindgen.bzl

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
# limitations under the License.
1414

1515
# buildifier: disable=module-docstring
16+
load("//rust:defs.bzl", "rust_common")
17+
1618
# buildifier: disable=bzl-visibility
1719
load("//rust/private:transitions.bzl", "wasm_bindgen_transition")
1820
load(
19-
":providers.bzl",
21+
"//wasm_bindgen:providers.bzl",
2022
"DeclarationInfo",
2123
"JSEcmaScriptModuleInfo",
2224
"JSModuleInfo",
@@ -78,6 +80,27 @@ def _rust_wasm_bindgen_impl(ctx):
7880
toolchain = ctx.toolchains[Label("//wasm_bindgen:wasm_bindgen_toolchain")]
7981
bindgen_bin = toolchain.bindgen
8082

83+
# Since the `wasm_file` attribute is behind a transition, it will be converted
84+
# to a list.
85+
if len(ctx.attr.wasm_file) == 1 and rust_common.crate_info in ctx.attr.wasm_file[0]:
86+
target = ctx.attr.wasm_file[0]
87+
crate_info = target[rust_common.crate_info]
88+
89+
# Provide a helpful warning informing users how to use the rule
90+
if rust_common.crate_info in target:
91+
supported_types = ["cdylib", "bin"]
92+
if crate_info.type not in supported_types:
93+
fail("The target '{}' is not a supported type: {}".format(
94+
ctx.attr.crate.label,
95+
supported_types,
96+
))
97+
98+
progress_message_label = target.label
99+
input_file = crate_info.output
100+
else:
101+
progress_message_label = ctx.file.wasm_file.path
102+
input_file = ctx.file.wasm_file
103+
81104
bindgen_wasm_module = ctx.actions.declare_file(ctx.attr.name + "_bg.wasm")
82105

83106
js_out = [ctx.actions.declare_file(ctx.attr.name + ".js")]
@@ -94,14 +117,14 @@ def _rust_wasm_bindgen_impl(ctx):
94117
args.add("--out-dir", bindgen_wasm_module.dirname)
95118
args.add("--out-name", ctx.attr.name)
96119
args.add_all(ctx.attr.bindgen_flags)
97-
args.add(ctx.file.wasm_file)
120+
args.add(input_file)
98121

99122
ctx.actions.run(
100123
executable = bindgen_bin,
101-
inputs = [ctx.file.wasm_file],
124+
inputs = [input_file],
102125
outputs = outputs,
103126
mnemonic = "RustWasmBindgen",
104-
progress_message = "Generating WebAssembly bindings for {}...".format(ctx.file.wasm_file.path),
127+
progress_message = "Generating WebAssembly bindings for {}...".format(progress_message_label),
105128
arguments = [args],
106129
)
107130

@@ -146,9 +169,10 @@ rust_wasm_bindgen = rule(
146169
values = ["web", "bundler", "nodejs", "no-modules", "deno"],
147170
),
148171
"wasm_file": attr.label(
149-
doc = "The .wasm file to generate bindings for.",
172+
doc = "The `.wasm` file or crate to generate bindings for.",
150173
allow_single_file = True,
151174
cfg = wasm_bindgen_transition,
175+
mandatory = True,
152176
),
153177
"_allowlist_function_transition": attr.label(
154178
default = Label("//tools/allowlists/function_transition_allowlist"),

0 commit comments

Comments
 (0)