Skip to content

Commit a127ae2

Browse files
authored
fix(installer): make init.sh POSIX-compatible and stop mangling $VERSION (#1578)
Two bugs broke `curl https://wasm-bindgen.github.io/wasm-pack/installer/init.sh -sSf | sh`: 1. The arg-parsing block introduced in 15e4f4f used bash arrays (`ORIG_ARGS=("$@")` / `"${ORIG_ARGS[@]}"`), so any non-bash POSIX shell (dash, ash, BusyBox sh) failed with a syntax error. This is issue #1527. 2. `docs/_installer/build-installer.rs` does a literal `$VERSION` -> `vX.Y.Z` text replacement on the script. Since 15e4f4f also added real `$VERSION` shell expansions (`if [ -z "$VERSION" ]`, the `case "$VERSION"`, etc.), every one of those got mangled in the published artifact, e.g. `if [ -z "v0.14.0" ]` and `VERSION="vv0.14.0"`. The runtime --version / VERSION-env override has been silently broken in every published release since. This commit: * switches the build-time placeholder to `@@WASM_PACK_VERSION@@` so it cannot collide with real shell variable references (both in init.sh and index.html). * rewrites init.sh as POSIX sh: no arrays, no `local`, scans args via `for _arg in "$@"` without consuming them so they still pass through to wasm-pack-init. * keeps the precedence: explicit `VERSION=` env > `--version X` arg > build-time baked version > "latest" (resolved via GitHub API). Verified syntax-clean and behaviourally correct under dash, /bin/sh, and bash for piped invocation (`curl ... | sh`), `sh -s -- --version X`, env override, and arg passthrough. Supersedes #1550, closes #1527.
1 parent 1e6c1de commit a127ae2

3 files changed

Lines changed: 57 additions & 52 deletions

File tree

docs/_installer/build-installer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ fn fixup(input: &str) -> String {
2626
.unwrap();
2727
let version = &version[version.find('"').unwrap() + 1..version.rfind('"').unwrap()];
2828

29-
input.replace("$VERSION", &format!("v{}", version))
29+
input.replace("@@WASM_PACK_VERSION@@", &format!("v{}", version))
3030
}

docs/_installer/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ <h1>Install <code>wasm-pack</code></h1>
6666
You appear to be running Windows 64-bit. Download and run
6767
<a
6868
class="winlink"
69-
href="https://github.com/wasm-bindgen/wasm-pack/releases/download/$VERSION/wasm-pack-init.exe"
69+
href="https://github.com/wasm-bindgen/wasm-pack/releases/download/@@WASM_PACK_VERSION@@/wasm-pack-init.exe"
7070
>wasm-pack-init.exe</a
7171
>
7272
then follow the onscreen instructions.

docs/_installer/init.sh

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# Copyright 2016 The Rust Project Developers. See the COPYRIGHT
33
# file at the top-level directory of this distribution and at
44
# http://rust-lang.org/COPYRIGHT.
@@ -15,29 +15,43 @@
1515

1616
set -u
1717

18-
# Parse VERSION from environment or --version argument, default to "latest"
19-
if [ -z "${VERSION:-}" ]; then
20-
VERSION=""
21-
ORIG_ARGS=("$@")
22-
23-
while [ $# -gt 0 ]; do
24-
case $1 in
25-
--version)
26-
VERSION="$2"
27-
shift 2
28-
;;
29-
*)
30-
shift
31-
;;
32-
esac
18+
# The version baked in at build time by docs/_installer/build-installer.rs.
19+
# The sentinel below is replaced with the published release tag (e.g. "v0.14.0").
20+
DEFAULT_VERSION="@@WASM_PACK_VERSION@@"
21+
22+
say() {
23+
echo "wasm-pack-init: $1"
24+
}
25+
26+
err() {
27+
say "$1" >&2
28+
exit 1
29+
}
30+
31+
# Resolve VERSION precedence: explicit env > --version arg > build-time default > "latest".
32+
# We scan args without consuming them so they pass through unchanged to the installer.
33+
resolve_version() {
34+
if [ -n "${VERSION:-}" ]; then
35+
return
36+
fi
37+
38+
_prev=""
39+
for _arg in "$@"; do
40+
if [ "$_prev" = "--version" ]; then
41+
VERSION="$_arg"
42+
return
43+
fi
44+
_prev="$_arg"
3345
done
34-
35-
set -- "${ORIG_ARGS[@]}"
36-
37-
if [ -z "$VERSION" ]; then
46+
47+
if [ -n "$DEFAULT_VERSION" ]; then
48+
VERSION="$DEFAULT_VERSION"
49+
else
3850
VERSION="latest"
3951
fi
40-
fi
52+
}
53+
54+
resolve_version "$@"
4155

4256
# Resolve "latest" to actual version number
4357
if [ "$VERSION" = "latest" ]; then
@@ -47,7 +61,7 @@ if [ "$VERSION" = "latest" ]; then
4761
fi
4862
fi
4963

50-
# Normalize version format for download URL
64+
# Normalize version format for download URL (ensure leading "v")
5165
case "$VERSION" in
5266
v*) ;;
5367
*) VERSION="v$VERSION" ;;
@@ -68,10 +82,10 @@ main() {
6882
need_cmd dirname
6983

7084
get_architecture || return 1
71-
local _arch="$RETVAL"
85+
_arch="$RETVAL"
7286
assert_nz "$_arch" "arch"
7387

74-
local _ext=""
88+
_ext=""
7589
case "$_arch" in
7690
*windows*)
7791
_ext=".exe"
@@ -80,13 +94,13 @@ main() {
8094

8195
which rustup > /dev/null 2>&1
8296
need_ok "failed to find Rust installation, is rustup installed?"
83-
local _rustup=$(which rustup)
84-
local _tardir="wasm-pack-$VERSION-${_arch}"
85-
local _url="$UPDATE_ROOT/${_tardir}.tar.gz"
86-
local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t wasm-pack)"
87-
local _file="$_dir/input.tar.gz"
88-
local _wasmpack="$_dir/wasm-pack$_ext"
89-
local _wasmpackinit="$_dir/wasm-pack-init$_ext"
97+
_rustup=$(which rustup)
98+
_tardir="wasm-pack-$VERSION-${_arch}"
99+
_url="$UPDATE_ROOT/${_tardir}.tar.gz"
100+
_dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t wasm-pack)"
101+
_file="$_dir/input.tar.gz"
102+
_wasmpack="$_dir/wasm-pack$_ext"
103+
_wasmpackinit="$_dir/wasm-pack-init$_ext"
90104

91105
printf '%s\n' 'info: downloading wasm-pack' 1>&2
92106

@@ -112,16 +126,16 @@ main() {
112126
"$_wasmpackinit" "$@"
113127
fi
114128

115-
local _retval=$?
129+
_retval=$?
116130

117131
ignore rm -rf "$_dir"
118132

119133
return "$_retval"
120134
}
121135

122136
get_architecture() {
123-
local _ostype="$(uname -s)"
124-
local _cputype="$(uname -m)"
137+
_ostype="$(uname -s)"
138+
_cputype="$(uname -m)"
125139

126140
# This is when installing inside docker, or can be useful to side-step
127141
# the script's built-in platform detection heuristic (if it drifts again in the future)
@@ -139,21 +153,21 @@ get_architecture() {
139153
if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then
140154
# Darwin `uname -s` lies
141155
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
142-
local _cputype=x86_64
156+
_cputype=x86_64
143157
fi
144158
fi
145159

146160
case "$_ostype" in
147161
Linux | linux)
148-
local _ostype=unknown-linux-musl
162+
_ostype=unknown-linux-musl
149163
;;
150164

151165
Darwin)
152-
local _ostype=apple-darwin
166+
_ostype=apple-darwin
153167
;;
154168

155169
MINGW* | MSYS* | CYGWIN*)
156-
local _ostype=pc-windows-msvc
170+
_ostype=pc-windows-msvc
157171
;;
158172

159173
*)
@@ -163,10 +177,10 @@ get_architecture() {
163177

164178
case "$_cputype" in
165179
x86_64 | x86-64 | x64 | amd64)
166-
local _cputype=x86_64
180+
_cputype=x86_64
167181
;;
168182
arm64 | aarch64)
169-
local _cputype=aarch64
183+
_cputype=aarch64
170184
;;
171185
*)
172186
err "no precompiled binaries available for CPU architecture: $_cputype"
@@ -178,20 +192,11 @@ get_architecture() {
178192
_cputype="x86_64"
179193
fi
180194

181-
local _arch="$_cputype-$_ostype"
195+
_arch="$_cputype-$_ostype"
182196

183197
RETVAL="$_arch"
184198
}
185199

186-
say() {
187-
echo "wasm-pack-init: $1"
188-
}
189-
190-
err() {
191-
say "$1" >&2
192-
exit 1
193-
}
194-
195200
need_cmd() {
196201
if ! check_cmd "$1"
197202
then err "need '$1' (command not found)"

0 commit comments

Comments
 (0)