Skip to content

Commit 2a3c955

Browse files
fix(cli): npm launcher never deletes a complete install another run published
Two first runs, or a launcher racing `t3 update`, could both stage a version; the slower one removed the folder the faster one had just published and was running from. Publishing is now a plain rename that keeps any complete install, and knip skips the launcher files, which are copied, not imported. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fd10ab6 commit 2a3c955

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎knip.jsonc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// Knip loads its preprocessor through a CLI option; native verification and
2121
// worktree setup (from t3.json) run directly.
2222
"entry": ["knip-schemas.ts", "mobile-native-client.ts", "setup-worktree.ts"],
23+
// The `t3` npm package's files: copied into the package, never imported.
24+
"ignore": ["npm-launcher/**"],
2325
},
2426
"apps/server": {
2527
// Vite+ pack entries and the launcher used by installed background services.

‎packages/shared/src/cliRelease.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
1919
*/
2020
// No darwin-x64: Node single-executables are unsupported on x64 macOS (the
2121
// SEA docs list macOS as arm64 only) and the binary segfaults on start.
22-
export const CLI_ARCHIVE_PLATFORM_KEYS = [
22+
const CLI_ARCHIVE_PLATFORM_KEYS = [
2323
"darwin-arm64",
2424
"linux-arm64",
2525
"linux-x64",

‎scripts/build-npm-launcher.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
187187
assert.deepStrictEqual(await exit, [23, null]);
188188
});
189189

190+
// Two first runs at once both start, and neither removes the other's install.
191+
const raceHome = path.join(root, "race-home");
192+
const launch = run(process.execPath, [launcher, "--version"], {
193+
cwd: root,
194+
env: { ...env, T3CODE_HOME: raceHome },
195+
});
196+
const [one, two] = yield* Effect.all([launch, launch], { concurrency: "unbounded" });
197+
assert.equal(one.exitCode, 7, one.stderr);
198+
assert.equal(two.exitCode, 7, two.stderr);
199+
assert.deepStrictEqual(yield* fs.readDirectory(path.join(raceHome, "runtime/versions")), [
200+
VERSION,
201+
]);
202+
190203
// A download that does not match the release checksums is never installed.
191204
release.corruptChecksum();
192205
const corrupt = yield* run(process.execPath, [launcher, "--version"], {

‎scripts/npm-launcher/t3.mjs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,38 @@ async function install(dir, key) {
9898
if (extract.status !== 0) throw new Error(`could not extract ${archive}`);
9999
fs.rmSync(file);
100100
fs.writeFileSync(path.join(staging, ".install-complete"), `${version}\n`);
101-
fs.rmSync(dir, { recursive: true, force: true });
102-
fs.renameSync(staging, dir);
101+
publish(staging, dir);
103102
} finally {
104103
fs.rmSync(staging, { recursive: true, force: true });
105104
}
106105
}
107106

107+
const isComplete = (dir) => {
108+
try {
109+
return fs.readFileSync(path.join(dir, ".install-complete"), "utf8").trim() === version;
110+
} catch {
111+
return false;
112+
}
113+
};
114+
115+
/**
116+
* Moves a finished staging folder to `dir`. Rename never replaces an existing
117+
* folder, so a complete install another launcher (or `t3 update`) published
118+
* first is kept, never deleted from under a running process. Only a folder
119+
* without the completion marker, left by an interrupted older installer, is
120+
* cleared before trying again.
121+
*/
122+
function publish(staging, dir) {
123+
try {
124+
fs.renameSync(staging, dir);
125+
} catch (error) {
126+
if (isComplete(dir)) return;
127+
if (!fs.existsSync(dir)) throw error;
128+
fs.rmSync(dir, { recursive: true, force: true });
129+
fs.renameSync(staging, dir);
130+
}
131+
}
132+
108133
async function main() {
109134
const key = `${process.platform}-${process.arch}`;
110135
if (!SUPPORTED.includes(key)) {
@@ -114,11 +139,7 @@ async function main() {
114139
);
115140
}
116141
const dir = path.join(t3Home(), "runtime", "versions", version);
117-
let installed = false;
118-
try {
119-
installed = fs.readFileSync(path.join(dir, ".install-complete"), "utf8").trim() === version;
120-
} catch {}
121-
if (!installed) await install(dir, key);
142+
if (!isComplete(dir)) await install(dir, key);
122143

123144
// A service launcher talks to its server over IPC; pass that channel and
124145
// signals through. In a terminal, Ctrl-C already reaches the child directly,

0 commit comments

Comments
 (0)