Skip to content

Commit 8df9edb

Browse files
committed
fix(windows): detect Windows host in transition and set linker to wasm-component-ld.exe
The previous select() approach didn't work because it evaluated in the target platform context (wasm32-wasip2) rather than the execution platform context (Windows). Transitions have access to both platforms through the settings parameter, so we can detect the Windows host platform and conditionally set the linker in the transition itself. This approach: - Reads the host_platform from transition settings - Detects 'windows' in the platform string - Adds '-C linker=wasm-component-ld.exe' to extra_rustc_flags on Windows - Works because transitions can modify build configuration based on exec platform The transition now explicitly handles the @rules_rust//:extra_rustc_flags output, preserving existing flags and adding Windows-specific ones when needed.
1 parent b6b1b15 commit 8df9edb

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

rust/rust_wasm_component.bzl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,6 @@ def rust_wasm_component(
311311

312312
profile_rustc_flags = rustc_flags + config["rustc_flags"]
313313

314-
# Add Windows-specific linker flag for wasm-component-ld.exe
315-
# On Windows, executables need .exe extension but Rust target spec doesn't add it
316-
# This is a workaround for https://github.com/rust-lang/rust/issues/...
317-
windows_linker_flag = select({
318-
"@platforms//os:windows": ["-C", "linker=wasm-component-ld.exe"],
319-
"//conditions:default": [],
320-
})
321-
profile_rustc_flags = profile_rustc_flags + windows_linker_flag
322-
323314
# Add wit-bindgen generated code if specified
324315
all_srcs = list(srcs)
325316

rust/transitions.bzl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,33 @@
1717
def _wasm_transition_impl(settings, attr):
1818
"""Transition to WASM platform for component builds"""
1919

20-
# Use WASI Preview 2 - now Tier 2 support in Rust 1.82+
20+
# Detect Windows execution platform for wasm-component-ld.exe workaround
21+
# On Windows, the linker needs .exe extension but Rust's wasm32-wasip2 target spec doesn't add it
22+
host_platform = str(settings["//command_line_option:host_platform"])
23+
is_windows = "windows" in host_platform
24+
25+
# Get current rustc flags
26+
rustc_flags = list(settings.get("@rules_rust//:extra_rustc_flags", []))
27+
28+
# Add Windows-specific linker configuration
29+
if is_windows:
30+
# Override the linker for wasm32-wasip2 on Windows hosts
31+
rustc_flags.extend(["-C", "linker=wasm-component-ld.exe"])
32+
2133
return {
2234
"//command_line_option:platforms": "//platforms:wasm32-wasip2",
35+
"@rules_rust//:extra_rustc_flags": rustc_flags,
2336
}
2437

2538
wasm_transition = transition(
2639
implementation = _wasm_transition_impl,
27-
inputs = [],
40+
inputs = [
41+
"//command_line_option:host_platform",
42+
"@rules_rust//:extra_rustc_flags",
43+
],
2844
outputs = [
2945
"//command_line_option:platforms",
46+
"@rules_rust//:extra_rustc_flags",
3047
],
3148
)
3249

0 commit comments

Comments
 (0)