wine-windows-gates.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/usr/bin/env bash
  2. # Run the blocking Windows gates (workspace build, production site) with real
  3. # win-x64 Node.js under Wine — the same script the pull-request `windows` job
  4. # in ci.yml executes and the optional local gate `pnpm run check:windows-wine`
  5. # wraps. Owning rationale and fidelity limits:
  6. # .agents/notes/implemented/process/2026-08-08-native-windows-pull-request-ci.md
  7. #
  8. # The working tree is never mutated: tracked plus untracked-unignored files
  9. # are snapshotted into a scratch directory, the Wine-specific pnpm overrides
  10. # (hoisted layout, win32-x64 platform packages) are appended to the SNAPSHOT's
  11. # pnpm-workspace.yaml, and the install and gates run there against the shared
  12. # pnpm store. The Wine prefix and the checksum-verified Windows Node zip
  13. # persist in .cache/wine-windows/ so reruns skip provisioning.
  14. #
  15. # Environment: DSH_WINE_NODE_MAJOR (default $PRIMARY_NODE_VERSION, then 24)
  16. # picks the Windows Node line; DSH_WINE_GATE_CACHE_DIR relocates the cache;
  17. # DSH_WINE_GATE_KEEP=1 preserves the scratch tree for inspection.
  18. set -euo pipefail
  19. repo_root="$(git rev-parse --show-toplevel)"
  20. node_major="${DSH_WINE_NODE_MAJOR:-${PRIMARY_NODE_VERSION:-24}}"
  21. cache_dir="${DSH_WINE_GATE_CACHE_DIR:-$repo_root/.cache/wine-windows}"
  22. export WINEDEBUG='-all'
  23. export WINEARCH=win64
  24. # Skip Wine Mono / Gecko installers: Node needs neither.
  25. export WINEDLLOVERRIDES='mscoree,mshtml='
  26. export WINEPREFIX="$cache_dir/prefix"
  27. # ---- preflight: fail loud before any expensive work --------------------
  28. wine_bin=''
  29. for candidate in "$(command -v wine || true)" "$(command -v wine64 || true)" /usr/lib/wine/wine64; do
  30. if [ -n "$candidate" ] && [ -x "$candidate" ]; then wine_bin="$candidate"; break; fi
  31. done
  32. # GNU coreutils sha256sum on Linux; perl shasum ships with macOS. Both
  33. # accept the same "<hash> <file>" --check input.
  34. checksum_tool=''
  35. if command -v sha256sum > /dev/null; then
  36. checksum_tool='sha256sum'
  37. elif command -v shasum > /dev/null; then
  38. checksum_tool='shasum'
  39. fi
  40. missing=()
  41. [ -n "$wine_bin" ] || missing+=('wine (apt: wine | brew: wine-stable)')
  42. command -v curl > /dev/null || missing+=('curl')
  43. command -v unzip > /dev/null || missing+=('unzip')
  44. [ -n "$checksum_tool" ] || missing+=('sha256sum or shasum (apt: coreutils | macOS ships shasum)')
  45. if ! command -v pnpm > /dev/null; then corepack enable > /dev/null 2>&1 || true; fi
  46. command -v pnpm > /dev/null || missing+=('pnpm (corepack enable)')
  47. if (( ${#missing[@]} > 0 )); then
  48. printf 'wine-windows-gates: missing required tool: %s\n' "${missing[@]}" >&2
  49. exit 1
  50. fi
  51. # Verify file $2 against SHA-256 hex $1 with whichever tool preflight found.
  52. verify_sha256() {
  53. case "$checksum_tool" in
  54. sha256sum) printf '%s %s\n' "$1" "$2" | sha256sum --check - > /dev/null ;;
  55. shasum) printf '%s %s\n' "$1" "$2" | shasum -a 256 --check - > /dev/null ;;
  56. esac
  57. }
  58. scratch="$(mktemp -d "${TMPDIR:-/tmp}/dsh-wine-gates.XXXXXX")"
  59. cleanup() {
  60. wineserver -k > /dev/null 2>&1 || true
  61. if [ "${DSH_WINE_GATE_KEEP:-0}" = '1' ]; then
  62. echo "wine-windows-gates: scratch tree kept at $scratch"
  63. else
  64. rm -rf "$scratch"
  65. fi
  66. }
  67. trap cleanup EXIT
  68. mkdir -p "$cache_dir" "$scratch/logs"
  69. # ---- provision Windows Node, boot Wine, snapshot + install concurrently ----
  70. curl_metadata_args=(
  71. --fail --silent --show-error --location
  72. --retry 3 --retry-all-errors --retry-delay 2
  73. --http1.1 --connect-timeout 10 --max-time 30 --retry-max-time 120
  74. )
  75. download_node_archive() {
  76. local version="$1" output="$2" attempt status=0
  77. local archive="node-$version-win-x64.zip"
  78. local primary_url="https://nodejs.org/dist/$version/$archive"
  79. local mirror_url="https://npmmirror.com/mirrors/node/$version/$archive"
  80. if curl --fail --silent --show-error --location --http1.1 \
  81. --connect-timeout 10 --max-time 300 --speed-limit 1024 --speed-time 30 \
  82. -o "$output" "$primary_url"; then
  83. return 0
  84. fi
  85. echo 'wine-windows-gates: nodejs.org archive transfer stalled; resuming from the checksum-untrusted transport mirror' >&2
  86. for attempt in 1 2 3; do
  87. if curl --fail --silent --show-error --location --http1.1 \
  88. --continue-at - --connect-timeout 10 --max-time 300 \
  89. --speed-limit 1024 --speed-time 30 \
  90. -o "$output" "$mirror_url"; then
  91. return 0
  92. else
  93. status=$?
  94. fi
  95. (( attempt < 3 )) || break
  96. echo "wine-windows-gates: mirror transfer failed (exit $status) on attempt $attempt; resuming partial download" >&2
  97. done
  98. return "$status"
  99. }
  100. provision_node() {
  101. # Latest release of the primary line, checksum-verified against the same
  102. # dist directory. Bound and retry every transfer so a stalled nodejs.org
  103. # response cannot consume the entire CI job. Offline runs fall back to the
  104. # newest cached zip, loudly.
  105. local version zip
  106. version="$(curl "${curl_metadata_args[@]}" https://nodejs.org/dist/index.json 2> /dev/null \
  107. | node -e "let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{const v=JSON.parse(d).find(r=>r.version.startsWith('v$node_major.'));if(v)console.log(v.version)})" \
  108. || true)"
  109. if [ -n "$version" ]; then
  110. zip="$cache_dir/node-$version-win-x64.zip"
  111. if [ ! -f "$zip" ]; then
  112. download_node_archive "$version" "$zip.tmp"
  113. local expected
  114. expected="$(curl "${curl_metadata_args[@]}" "https://nodejs.org/dist/$version/SHASUMS256.txt" \
  115. | awk -v a="node-$version-win-x64.zip" '$2 == a { print $1; exit }')"
  116. [ -n "$expected" ] || { echo "wine-windows-gates: no SHASUMS256 entry for node-$version-win-x64.zip" >&2; exit 1; }
  117. verify_sha256 "$expected" "$zip.tmp"
  118. mv "$zip.tmp" "$zip"
  119. fi
  120. else
  121. zip="$(ls -t "$cache_dir"/node-v"$node_major".*-win-x64.zip 2> /dev/null | head -1 || true)"
  122. [ -n "$zip" ] || { echo "wine-windows-gates: nodejs.org unreachable and no cached Windows Node v$node_major zip in $cache_dir" >&2; exit 1; }
  123. echo "wine-windows-gates: nodejs.org unreachable; using cached $(basename "$zip")" >&2
  124. fi
  125. unzip -q -o "$zip" -d "$scratch/node-win"
  126. echo "$scratch/node-win/$(basename "$zip" .zip)/node.exe" > "$scratch/node-win-path"
  127. }
  128. boot_wine() {
  129. "$wine_bin" wineboot --init > /dev/null 2>&1 || true
  130. wineserver -w || true
  131. }
  132. snapshot_and_install() {
  133. # Tracked + untracked-unignored files, minus agent-session litter; the
  134. # existence filter drops paths staged as deleted. Then the Wine-specific
  135. # install-time overrides go on the SNAPSHOT only: hoisted because Windows
  136. # Node under Wine does not realpath pnpm's isolated-layout symlinks, and
  137. # win32-x64 so the Windows esbuild/rolldown/rollup binaries materialize.
  138. # Neither is recorded in the lockfile, so --frozen-lockfile stays valid;
  139. # --ignore-scripts skips host lifecycle scripts no gate loads.
  140. git -C "$repo_root" ls-files -z --cached --others --exclude-standard -- . ':!:.claude' ':!:.codex' \
  141. | while IFS= read -r -d '' file; do [ -e "$repo_root/$file" ] && printf '%s\0' "$file"; done \
  142. | tar -C "$repo_root" --null --files-from=- -cf - \
  143. | tar -C "$scratch/tree" -xf -
  144. cat >> "$scratch/tree/pnpm-workspace.yaml" << 'EOF'
  145. nodeLinker: hoisted
  146. supportedArchitectures:
  147. os: [current, win32]
  148. cpu: [current, x64]
  149. EOF
  150. # The hoisted linker — used only by this lane — has an upstream rename
  151. # race (pnpm/pnpm#12880): parallel linkers staging a nested package copy
  152. # (observed on the tree's nested esbuild versions) rename their _tmp_*
  153. # directory onto a path another racer already claimed, and the loser
  154. # exits ERR_PNPM_ENOENT although an identical re-install succeeds.
  155. # Exactly that signature earns up to two retries on a clean tree — the
  156. # snapshot contains no node_modules, so wiping them restores the
  157. # pre-install state; any other failure, or the race still standing after
  158. # the final attempt, fails loud with the log tail.
  159. local attempt
  160. for attempt in 1 2 3; do
  161. (cd "$scratch/tree" && pnpm install --frozen-lockfile --ignore-scripts > "$scratch/logs/install.log" 2>&1) \
  162. && return 0
  163. grep -q 'ERR_PNPM_ENOENT.*rename.*_tmp_' "$scratch/logs/install.log" || break
  164. (( attempt < 3 )) || break
  165. echo "wine-windows-gates: pnpm hoisted-linker rename race (pnpm/pnpm#12880) on install attempt $attempt; retrying on a clean tree" >&2
  166. find "$scratch/tree" -name node_modules -type d -prune -exec rm -rf {} +
  167. done
  168. tail -40 "$scratch/logs/install.log" >&2
  169. return 1
  170. }
  171. mkdir "$scratch/tree"
  172. start=$SECONDS
  173. provision_node & node_pid=$!
  174. boot_wine & wine_pid=$!
  175. snapshot_and_install & install_pid=$!
  176. # Wait for EVERY child before judging any: a bare `wait` under set -e would
  177. # exit on the first failure and let the EXIT trap delete $scratch while the
  178. # other children still run inside it. Named statuses also make the report
  179. # point at the root cause instead of a downstream symptom.
  180. node_status=0; wait "$node_pid" || node_status=$?
  181. wine_status=0; wait "$wine_pid" || wine_status=$?
  182. install_status=0; wait "$install_pid" || install_status=$?
  183. provision_failed=0
  184. report_provision() {
  185. if (( $2 != 0 )); then
  186. echo "wine-windows-gates: FAILED $1 (exit $2)" >&2
  187. provision_failed=$2
  188. fi
  189. }
  190. report_provision 'Windows Node provisioning' "$node_status"
  191. report_provision 'wineboot' "$wine_status"
  192. report_provision 'workspace snapshot + pnpm install' "$install_status"
  193. if (( provision_failed != 0 )); then exit "$provision_failed"; fi
  194. node_win="$(cat "$scratch/node-win-path")"
  195. echo "wine-windows-gates: provisioned in $((SECONDS - start))s (wine $("$wine_bin" --version 2> /dev/null), node $(basename "$(dirname "$node_win")"))"
  196. # ---- resolve entrypoints, lay the vue link, smoke ------------------------
  197. # Node under Wine cannot attach stdio to pipes the caller owns (Socket open
  198. # EBADF at bootstrap), so every invocation routes stdio through a file.
  199. wine_node() {
  200. local log="$1"
  201. shift
  202. local status=0
  203. "$wine_bin" "$node_win" "$@" < /dev/null > "$log" 2>&1 || status=$?
  204. return "$status"
  205. }
  206. cd "$scratch/tree"
  207. tsc_js='node_modules/typescript/bin/tsc'
  208. tsdown_js='node_modules/tsdown/dist/run.mjs'
  209. vitepress_js='node_modules/vitepress/bin/vitepress.js'
  210. [ -f "$vitepress_js" ] || vitepress_js='website/node_modules/vitepress/bin/vitepress.js'
  211. for entry in "$tsc_js" "$tsdown_js" "$vitepress_js"; do
  212. [ -f "$entry" ] || { echo "wine-windows-gates: expected entrypoint missing after hoisted install: $entry" >&2; exit 1; }
  213. done
  214. # VitePress links vue into the site's node_modules at build time; Wine cannot
  215. # CREATE Windows symlinks (ENOTSUP) but follows pre-existing Unix ones.
  216. if [ -d node_modules/vue ] && [ ! -e website/node_modules/vue ]; then
  217. mkdir -p website/node_modules
  218. ln -s ../../node_modules/vue website/node_modules/vue
  219. fi
  220. wine_node "$scratch/logs/smoke.log" -p "'smoke: ' + process.platform + ' ' + process.arch + ' ' + process.version"
  221. cat "$scratch/logs/smoke.log"
  222. grep -q '^smoke: win32 x64' "$scratch/logs/smoke.log" || { echo 'wine-windows-gates: Windows Node smoke did not report win32 x64' >&2; exit 1; }
  223. # ---- the two blocking surfaces, concurrently ------------------------------
  224. # The build preserves the face order from package.json: compile and bundle the
  225. # Host face before compiling and bundling the Client face.
  226. # Both statuses are captured so one failure cannot hide the other's result.
  227. build_gate() {
  228. wine_node "$scratch/logs/host-tsc.log" --max-old-space-size=4096 "$tsc_js" -b tsconfig.host.json --pretty false || return $?
  229. wine_node "$scratch/logs/host-tsdown.log" "$tsdown_js" --env.DSH_BUILD_FACE host || return $?
  230. wine_node "$scratch/logs/client-tsc.log" "$tsc_js" -b tsconfig.client.json --pretty false || return $?
  231. wine_node "$scratch/logs/client-tsdown.log" "$tsdown_js" --env.DSH_BUILD_FACE client
  232. }
  233. site_gate() {
  234. cd website
  235. wine_node "$scratch/logs/site.log" "../$vitepress_js" build .
  236. }
  237. start=$SECONDS
  238. build_gate & build_pid=$!
  239. site_gate & site_pid=$!
  240. build_status=0
  241. wait "$build_pid" || build_status=$?
  242. site_status=0
  243. wait "$site_pid" || site_status=$?
  244. elapsed=$((SECONDS - start))
  245. report() {
  246. local label="$1" status="$2"
  247. shift 2
  248. if (( status == 0 )); then
  249. echo "wine-windows-gates: PASS $label (${elapsed}s window)"
  250. else
  251. echo "== FAILED $label (exit $status) ==" >&2
  252. for log in "$@"; do tail -n 200 "$log" >&2 || true; done
  253. fi
  254. }
  255. report 'build (Host tsc/tsdown, Client tsc/tsdown)' "$build_status" \
  256. "$scratch/logs/host-tsc.log" \
  257. "$scratch/logs/host-tsdown.log" \
  258. "$scratch/logs/client-tsc.log" \
  259. "$scratch/logs/client-tsdown.log"
  260. report 'production site (vitepress build)' "$site_status" "$scratch/logs/site.log"
  261. if (( build_status != 0 )); then exit "$build_status"; fi
  262. exit "$site_status"