Skip to content

Commit 2998a23

Browse files
committed
fix(desktop): clean external update failures and match bundle identity
1 parent 24a9556 commit 2998a23

6 files changed

Lines changed: 353 additions & 278 deletions

File tree

‎apps/desktop/scripts/install-macos-update.fixtures.mjs‎

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@ export function createFixture(options = {}) {
8282
try {
8383
const bin = NodePath.join(dir, "fake-bin");
8484
NodeFS.mkdirSync(bin);
85-
const app = NodePath.join(dir, "Fixture App.app");
85+
const app = NodePath.join(dir, options.installedName ?? "Fixture App.app");
8686
createBundle(app, "1.0.0");
87-
const source = NodePath.join(dir, "release/Fixture App.app");
87+
const source = NodePath.join(dir, "release", options.archiveName ?? "Fixture App.app");
8888
createBundle(source, options.version ?? "2.0.0", options);
89+
for (const [index, bundle] of (options.extraBundles ?? []).entries()) {
90+
createBundle(NodePath.join(dir, "release", `Extra ${index}.app`), "2.0.0", bundle);
91+
}
8992
const expected = snapshot(source);
9093
const archive = NodePath.join(dir, "selected.zip");
9194
const archived = run("/usr/bin/ditto", [
9295
"-c",
9396
"-k",
9497
"--sequesterRsrc",
95-
"--keepParent",
96-
source,
98+
NodePath.dirname(source),
9799
archive,
98100
]);
99101
NodeAssert.equal(archived.status, 0, archived.stderr);
@@ -102,6 +104,10 @@ export function createFixture(options = {}) {
102104

103105
// Only signature, Gatekeeper and launch boundaries are simulated; these are not signed-app fixtures.
104106
const stubs = {
107+
sleep: `#!/bin/sh
108+
printf 'waiting\\n' >&5
109+
exec /bin/sleep "$@"
110+
`,
105111
codesign: `#!/bin/sh
106112
for arg do bundle=$arg; done
107113
if [ "$1" = -dv ]; then printf 'TeamIdentifier=%s\\n' "$(cat "$bundle/.team")" >&2; exit 0; fi
@@ -143,15 +149,16 @@ printf 'open:%s\\n' "$*" >> "$FIXTURE_EVENTS"
143149
FIXTURE_EVENTS: events,
144150
FIXTURE_PARENT: "99999999",
145151
};
146-
const prepare = () => run("/bin/sh", [helper, "prepare", archive, app, "2.0.0"], { env });
147-
return { dir, app, archive, expected, helper, env, events, prepare, clean };
152+
const attempt = () =>
153+
run("/bin/sh", [helper, archive, app, "2.0.0", "99999999", "false"], { env });
154+
return { dir, app, archive, expected, helper, env, events, attempt, clean };
148155
} catch (error) {
149156
clean();
150157
throw error;
151158
}
152159
}
153160

154-
export async function startParent() {
161+
async function startParent() {
155162
const child = NodeChildProcess.spawn(
156163
process.execPath,
157164
["-e", "process.stdin.resume();process.stdin.on('end',()=>process.exit(0));"],
@@ -161,35 +168,43 @@ export async function startParent() {
161168
return child;
162169
}
163170

164-
export async function stopParent(child) {
171+
async function stopParent(child) {
165172
if (child.exitCode === null && child.signalCode === null) {
166173
const exited = NodeEvents.once(child, "exit");
167174
child.stdin.end();
168175
await exited;
169176
}
170177
}
171178

172-
export async function install(fixture, { relaunch = true, failInstalled = false } = {}) {
179+
export async function install(
180+
fixture,
181+
{ relaunch = true, failInstalled = false, cancel = false, timeout = false } = {},
182+
) {
173183
const parent = await startParent();
174184
let child;
175185
let watchdog;
176186
let stdout = "";
177187
let stderr = "";
178188
try {
179-
const prepared = fixture.prepare();
180-
NodeAssert.equal(prepared.status, 0, prepared.stderr);
181-
const candidate = prepared.stdout.trim();
182-
const stage = NodePath.dirname(candidate);
189+
if (timeout) {
190+
// Exercise the production timeout branch without waiting two minutes.
191+
const source = NodeFS.readFileSync(fixture.helper, "utf8");
192+
NodeAssert.equal(source.split('"$attempts" -lt 120').length, 2);
193+
NodeFS.writeFileSync(
194+
fixture.helper,
195+
source.replace('"$attempts" -lt 120', '"$attempts" -lt 0'),
196+
);
197+
}
183198
child = NodeChildProcess.spawn(
184199
"/bin/sh",
185-
[fixture.helper, "install", candidate, fixture.app, String(parent.pid), String(relaunch)],
200+
[fixture.helper, fixture.archive, fixture.app, "2.0.0", String(parent.pid), String(relaunch)],
186201
{
187202
env: {
188203
...fixture.env,
189204
FIXTURE_PARENT: String(parent.pid),
190205
FIXTURE_FAIL_INSTALLED: failInstalled ? "1" : "0",
191206
},
192-
stdio: ["ignore", "pipe", "pipe"],
207+
stdio: ["ignore", "pipe", "pipe", "ignore", "ignore", "pipe"],
193208
},
194209
);
195210
const closed = NodeEvents.once(child, "close");
@@ -221,15 +236,37 @@ export async function install(fixture, { relaunch = true, failInstalled = false
221236
});
222237
NodeAssert.equal(isAlive(parent.pid), true, "ready precedes fixture parent exit");
223238
NodeAssert.equal(NodeFS.readFileSync(NodePath.join(fixture.app, ".version"), "utf8"), "1.0.0");
224-
NodeAssert.equal(NodeFS.existsSync(candidate), true, "candidate remains staged at readiness");
225-
NodeAssert.equal(
226-
NodeFS.existsSync(`${fixture.app}.update-lock`),
227-
true,
228-
"ready follows lock acquisition",
239+
const stages = NodeFS.readdirSync(fixture.dir).filter(
240+
(name) =>
241+
name.startsWith(".t3-update.") &&
242+
NodeFS.existsSync(NodePath.join(fixture.dir, name, "install.log")),
229243
);
244+
NodeAssert.equal(stages.length, 1, "one stage belongs to this invocation");
245+
const stage = NodePath.join(fixture.dir, stages[0]);
246+
const candidate = NodePath.join(stage, "candidate.app");
247+
if (!timeout) {
248+
NodeAssert.equal(NodeFS.existsSync(candidate), true, "candidate remains staged at readiness");
249+
NodeAssert.equal(
250+
NodeFS.existsSync(`${fixture.app}.update-lock`),
251+
true,
252+
"ready follows lock acquisition",
253+
);
254+
}
230255
NodeAssert.equal(NodeFS.readFileSync(fixture.events, "utf8").includes("open:"), false);
231-
await stopParent(parent);
256+
if (cancel) child.kill("SIGTERM");
257+
else if (!timeout) {
258+
await new Promise((resolve, reject) => {
259+
child.stdio[5].once("data", resolve);
260+
child.once("close", () =>
261+
reject(new Error("installer exited without waiting for its live parent")),
262+
);
263+
if (child.exitCode !== null) reject(new Error("installer already exited without waiting"));
264+
});
265+
await stopParent(parent);
266+
}
232267
const [status] = await closed;
268+
if (cancel || timeout)
269+
NodeAssert.equal(isAlive(parent.pid), true, "failed handoff leaves parent running");
233270
return {
234271
status,
235272
stdout,

‎apps/desktop/scripts/install-macos-update.sh‎

Lines changed: 79 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,86 +14,82 @@ verify() {
1414
[ "$(identifier "$1")" = "$bundle_id" ] || fail "Update bundle identifier does not match the installed app."
1515
}
1616

17-
[ "$#" -ge 1 ] || fail "Expected prepare or install."
18-
phase=$1; shift
19-
case "$phase" in
20-
prepare)
21-
[ "$#" -eq 3 ] || fail "Expected prepare ARCHIVE APP VERSION."
22-
archive=$1; app=$2; version=$3
23-
case "$app" in /*.app) ;; *) fail "Expected an absolute app bundle path." ;; esac
24-
[ -d "$app" ] && [ ! -L "$app" ] || fail "Invalid app bundle."
25-
codesign --verify --deep --strict "$app" >&2
26-
signer=$(team "$app"); bundle_id=$(identifier "$app")
27-
[ -n "$signer" ] && [ "$signer" != 'not set' ] || fail "Installed app has no signing team."
28-
stage=$(mktemp -d "$(dirname "$app")/.t3-update.XXXXXXXX")
29-
trap 'rm -rf "$stage"' EXIT
30-
trap 'exit 1' HUP INT TERM
31-
ditto -x -k "$archive" "$stage/extracted" >&2
32-
candidate="$stage/extracted/$(basename "$app")"
33-
verify "$candidate"
34-
actual=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$candidate/Contents/Info.plist")
35-
[ "$actual" = "$version" ] || fail "Downloaded update version does not match T3's selected version."
36-
# Stock nightlies would remove this integration on the next restart.
37-
grep -a -q 'T3CODE_DESKTOP_UPDATE_INSTALLER' "$candidate/Contents/Resources/app.asar" ||
38-
fail "Downloaded build lacks external installer support; a signed build containing the patch is required."
39-
spctl --assess --type execute "$candidate" >&2
40-
mv "$candidate" "$stage/candidate.app"
41-
rm -rf "$stage/extracted"
42-
printf '%s\n' "$stage/candidate.app"
43-
trap - EXIT HUP INT TERM
44-
;;
45-
install)
46-
[ "$#" -eq 4 ] || fail "Expected install CANDIDATE APP PARENT_PID RELAUNCH."
47-
candidate=$1; app=$2; parent_pid=$3; relaunch=$4
48-
case "$parent_pid" in ''|*[!0-9]*|0|1) fail "Invalid parent PID." ;; esac
49-
case "$relaunch" in true|false) ;; *) fail "Invalid relaunch value." ;; esac
50-
stage=$(dirname "$candidate")
51-
case "$stage" in "$(dirname "$app")"/.t3-update.*) ;; *) fail "Invalid staging directory." ;; esac
52-
[ "$candidate" = "$stage/candidate.app" ] || fail "Invalid staged app path."
53-
[ -d "$app" ] && [ ! -L "$app" ] && [ ! -L "$stage" ] || fail "Invalid app or staging directory."
54-
signer=$(team "$app"); bundle_id=$(identifier "$app")
55-
[ -n "$signer" ] && [ "$signer" != 'not set' ] || fail "Installed app has no signing team."
56-
verify "$candidate"
57-
shlock -p "$$" -f "$app.update-lock" || fail "Another installation is running."
58-
moved_old=false
59-
rollback() {
60-
status=$?
61-
if [ "$moved_old" = true ]; then
62-
if [ -e "$app" ]; then mv "$app" "$stage/failed.app"; fi
63-
mv "$stage/previous.app" "$app"
64-
fi
65-
if [ "$status" -ne 0 ] && [ "$relaunch" = true ] && ! kill -0 "$parent_pid" 2>/dev/null; then
66-
launch || true
67-
fi
68-
rm -f "$app.update-lock"
69-
exit "$status"
70-
}
71-
trap rollback EXIT
72-
trap 'exit 1' HUP INT TERM
73-
exec 3>&1
74-
exec >> "$stage/install.log" 2>&1
75-
printf 'ready\n' >&3
76-
exec 3>&-
77-
attempts=0
78-
while kill -0 "$parent_pid" 2>/dev/null; do
79-
[ "$attempts" -lt 120 ] || fail "T3 did not exit; update was not installed."
80-
sleep 1
81-
attempts=$((attempts + 1))
82-
done
83-
mv "$app" "$stage/previous.app"
84-
moved_old=true
85-
mv "$candidate" "$app"
86-
verify "$app"
87-
if [ "$relaunch" = true ]; then launch; fi
88-
moved_old=false
89-
printf '%s\n' "$app" > "$stage/installed-app"
90-
# Keep one verified rollback bundle, leaving unrelated backups untouched.
91-
for previous in "$(dirname "$app")"/.t3-update.*; do
92-
[ "$previous" != "$stage" ] || continue
93-
[ ! -L "$previous" ] && [ -f "$previous/installed-app" ] || continue
94-
[ "$(cat "$previous/installed-app")" = "$app" ] || continue
95-
rm -rf "$previous"
96-
done
97-
;;
98-
*) fail "Unknown installer phase: $phase" ;;
99-
esac
17+
[ "$#" -eq 5 ] || fail "Expected ARCHIVE APP VERSION PARENT_PID RELAUNCH."
18+
archive=$1; app=$2; version=$3; parent_pid=$4; relaunch=$5
19+
case "$app" in /*.app) ;; *) fail "Expected an absolute app bundle path." ;; esac
20+
case "$parent_pid" in ''|*[!0-9]*|0|1) fail "Invalid parent PID." ;; esac
21+
case "$relaunch" in true|false) ;; *) fail "Invalid relaunch value." ;; esac
22+
[ -d "$app" ] && [ ! -L "$app" ] || fail "Invalid app bundle."
23+
codesign --verify --deep --strict "$app" >&2
24+
signer=$(team "$app"); bundle_id=$(identifier "$app")
25+
[ -n "$signer" ] && [ "$signer" != 'not set' ] || fail "Installed app has no signing team."
26+
exec 4>&2
27+
stage=$(mktemp -d "$(dirname "$app")/.t3-update.XXXXXXXX")
28+
candidate="$stage/candidate.app"
29+
moved_old=false; locked=false; ready=false
30+
cleanup() {
31+
status=$?
32+
if [ "$moved_old" = true ]; then
33+
if [ -e "$app" ]; then mv "$app" "$stage/failed.app"; fi
34+
mv "$stage/previous.app" "$app"
35+
fi
36+
if [ "$status" -ne 0 ]; then
37+
rm -rf "$candidate" "$stage/extracted"
38+
if [ "$ready" = true ] && [ "$relaunch" = true ] && ! kill -0 "$parent_pid" 2>/dev/null; then
39+
launch || true
40+
fi
41+
fi
42+
if [ "$locked" = true ]; then rm -f "$app.update-lock"; fi
43+
if [ "$ready" = false ]; then cat "$stage/install.log" >&4 || true; fi
44+
exit "$status"
45+
}
46+
trap cleanup EXIT
47+
trap 'exit 1' HUP INT TERM
48+
exec 2>> "$stage/install.log"
49+
50+
ditto -x -k "$archive" "$stage/extracted" >&2
51+
selected=
52+
for bundle in "$stage/extracted/"*.app; do
53+
[ -d "$bundle" ] && [ ! -L "$bundle" ] || continue
54+
bundle_identifier=$(identifier "$bundle" 2>/dev/null) || continue
55+
[ "$bundle_identifier" = "$bundle_id" ] || continue
56+
[ -z "$selected" ] || fail "Multiple update bundles match the installed bundle identifier."
57+
selected=$bundle
58+
done
59+
[ -n "$selected" ] || fail "Update bundle identifier does not match the installed app."
60+
verify "$selected"
61+
actual=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$selected/Contents/Info.plist")
62+
[ "$actual" = "$version" ] || fail "Downloaded update version does not match T3's selected version."
63+
# Stock nightlies would remove this integration on the next restart.
64+
grep -a -q 'T3CODE_DESKTOP_UPDATE_INSTALLER' "$selected/Contents/Resources/app.asar" ||
65+
fail "Downloaded build lacks external installer support; a signed build containing the patch is required."
66+
spctl --assess --type execute "$selected" >&2
67+
mv "$selected" "$candidate"
68+
rm -rf "$stage/extracted"
69+
shlock -p "$$" -f "$app.update-lock" || fail "Another installation is running."
70+
locked=true
71+
exec 3>&1
72+
exec >> "$stage/install.log"
73+
ready=true
74+
printf 'ready\n' >&3
75+
exec 3>&- 4>&-
76+
attempts=0
77+
while kill -0 "$parent_pid" 2>/dev/null; do
78+
[ "$attempts" -lt 120 ] || fail "T3 did not exit; update was not installed."
79+
sleep 1
80+
attempts=$((attempts + 1))
81+
done
82+
mv "$app" "$stage/previous.app"
83+
moved_old=true
84+
mv "$candidate" "$app"
85+
verify "$app"
86+
if [ "$relaunch" = true ]; then launch; fi
87+
moved_old=false
88+
printf '%s\n' "$app" > "$stage/installed-app"
89+
# Keep one verified rollback bundle, leaving unrelated backups untouched.
90+
for previous in "$(dirname "$app")"/.t3-update.*; do
91+
[ "$previous" != "$stage" ] || continue
92+
[ ! -L "$previous" ] && [ -f "$previous/installed-app" ] || continue
93+
[ "$(cat "$previous/installed-app")" = "$app" ] || continue
94+
rm -rf "$previous"
95+
done

0 commit comments

Comments
 (0)