|
| 1 | +"""Debug rules for investigating Windows Rust toolchain issues""" |
| 2 | + |
| 3 | +def _debug_rust_toolchain_impl(ctx): |
| 4 | + """Debug rule that dumps information about the Rust toolchain on Windows""" |
| 5 | + |
| 6 | + output = ctx.actions.declare_file(ctx.label.name + "_debug.txt") |
| 7 | + |
| 8 | + # Get rustc from the toolchain |
| 9 | + rust_toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"] |
| 10 | + rustc = rust_toolchain.rustc |
| 11 | + |
| 12 | + # Create a debug script |
| 13 | + if ctx.attr.is_windows: |
| 14 | + # Windows PowerShell script |
| 15 | + script = """ |
| 16 | +@echo off |
| 17 | +echo === Rust Toolchain Debug Info === > {output} |
| 18 | +echo. >> {output} |
| 19 | +echo Rustc path: {rustc} >> {output} |
| 20 | +echo. >> {output} |
| 21 | +
|
| 22 | +REM Get rustc's sysroot |
| 23 | +{rustc} --print sysroot >> {output} 2>&1 |
| 24 | +
|
| 25 | +REM Check if lib directory exists relative to rustc |
| 26 | +FOR %%I IN ("{rustc}") DO SET "rustc_dir=%%~dpI" |
| 27 | +echo. >> {output} |
| 28 | +echo Rustc directory: %rustc_dir% >> {output} |
| 29 | +echo. >> {output} |
| 30 | +
|
| 31 | +REM Try to find wasm-component-ld.exe |
| 32 | +echo Searching for wasm-component-ld.exe: >> {output} |
| 33 | +where wasm-component-ld.exe >> {output} 2>&1 || echo Not found in PATH >> {output} |
| 34 | +echo. >> {output} |
| 35 | +
|
| 36 | +REM List what's in the rustc bin directory |
| 37 | +echo Contents of rustc bin directory: >> {output} |
| 38 | +dir /B "%rustc_dir%" >> {output} 2>&1 |
| 39 | +
|
| 40 | +REM Check if lib/rustlib exists |
| 41 | +echo. >> {output} |
| 42 | +echo Checking lib/rustlib structure: >> {output} |
| 43 | +dir /B "%rustc_dir%..\\lib\\rustlib" >> {output} 2>&1 |
| 44 | +
|
| 45 | +REM Check x86_64-pc-windows-msvc bin directory |
| 46 | +echo. >> {output} |
| 47 | +echo Checking x86_64-pc-windows-msvc bin directory: >> {output} |
| 48 | +dir /B "%rustc_dir%..\\lib\\rustlib\\x86_64-pc-windows-msvc\\bin" >> {output} 2>&1 |
| 49 | +""".format( |
| 50 | + output = output.path, |
| 51 | + rustc = rustc.path, |
| 52 | + ) |
| 53 | + |
| 54 | + ctx.actions.run_shell( |
| 55 | + outputs = [output], |
| 56 | + inputs = [rustc], |
| 57 | + command = script, |
| 58 | + mnemonic = "DebugRustToolchain", |
| 59 | + execution_requirements = { |
| 60 | + "no-sandbox": "1", # Disable sandbox to see actual file system |
| 61 | + }, |
| 62 | + ) |
| 63 | + else: |
| 64 | + # Linux/Mac bash script |
| 65 | + script = """ |
| 66 | +#!/bin/bash |
| 67 | +{{ |
| 68 | + echo "=== Rust Toolchain Debug Info ===" |
| 69 | + echo |
| 70 | + echo "Rustc path: {rustc}" |
| 71 | + echo |
| 72 | +
|
| 73 | + # Get rustc's sysroot |
| 74 | + {rustc} --print sysroot |
| 75 | +
|
| 76 | + # Get rustc directory |
| 77 | + rustc_dir=$(dirname "{rustc}") |
| 78 | + echo |
| 79 | + echo "Rustc directory: $rustc_dir" |
| 80 | + echo |
| 81 | +
|
| 82 | + # Try to find wasm-component-ld |
| 83 | + echo "Searching for wasm-component-ld:" |
| 84 | + which wasm-component-ld || echo "Not found in PATH" |
| 85 | + echo |
| 86 | +
|
| 87 | + # List what's in the rustc bin directory |
| 88 | + echo "Contents of rustc bin directory:" |
| 89 | + ls -la "$rustc_dir" || echo "Directory not accessible" |
| 90 | +
|
| 91 | + # Check if lib/rustlib exists |
| 92 | + echo |
| 93 | + echo "Checking lib/rustlib structure:" |
| 94 | + ls -la "$rustc_dir/../lib/rustlib" || echo "Directory not accessible" |
| 95 | +
|
| 96 | +}} > {output} 2>&1 |
| 97 | +""".format( |
| 98 | + output = output.path, |
| 99 | + rustc = rustc.path, |
| 100 | + ) |
| 101 | + |
| 102 | + ctx.actions.run_shell( |
| 103 | + outputs = [output], |
| 104 | + inputs = [rustc], |
| 105 | + command = script, |
| 106 | + mnemonic = "DebugRustToolchain", |
| 107 | + ) |
| 108 | + |
| 109 | + return [DefaultInfo(files = depset([output]))] |
| 110 | + |
| 111 | +debug_rust_toolchain = rule( |
| 112 | + implementation = _debug_rust_toolchain_impl, |
| 113 | + attrs = { |
| 114 | + "is_windows": attr.bool( |
| 115 | + default = False, |
| 116 | + doc = "Whether this is running on Windows", |
| 117 | + ), |
| 118 | + }, |
| 119 | + toolchains = [ |
| 120 | + "@rules_rust//rust:toolchain_type", |
| 121 | + ], |
| 122 | + doc = """ |
| 123 | + Debug rule that dumps information about the Rust toolchain. |
| 124 | +
|
| 125 | + On Windows, this will show: |
| 126 | + - Where rustc is located |
| 127 | + - What rustc's sysroot is |
| 128 | + - Whether wasm-component-ld.exe can be found |
| 129 | + - What files are visible in the rustlib directory |
| 130 | +
|
| 131 | + Example: |
| 132 | + debug_rust_toolchain( |
| 133 | + name = "debug_windows", |
| 134 | + is_windows = select({ |
| 135 | + "@platforms//os:windows": True, |
| 136 | + "//conditions:default": False, |
| 137 | + }), |
| 138 | + ) |
| 139 | + """, |
| 140 | +) |
0 commit comments