Skip to content

Commit 8095c65

Browse files
committed
fix(release): preserve updates from npm-based services
1 parent 6f00d38 commit 8095c65

7 files changed

Lines changed: 174 additions & 3 deletions

File tree

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
12
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import { assert, it } from "@effect/vitest";
34
import * as Deferred from "effect/Deferred";
@@ -94,6 +95,19 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
9495
assert.deepEqual(commands, ["tar"]);
9596
assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
9697
assert.isFalse(yield* fs.exists(path.join(paths.versionDir, "t3-runtime-archive")));
98+
if ((yield* HostProcessPlatform) !== "win32") {
99+
// The old launcher must still be able to start this archive after the
100+
// first npm-to-executable update, including from the final directory.
101+
yield* fs.writeFileString(paths.entryPath, '#!/bin/sh\nprintf "%s\\n" "$@"\n');
102+
yield* fs.chmod(paths.entryPath, 0o755);
103+
const runner = yield* ProcessRunner.make();
104+
const legacyStart = yield* runner.run({
105+
command: process.execPath,
106+
args: [path.join(paths.versionDir, "node_modules/t3/dist/bin.mjs"), "serve"],
107+
});
108+
assert.equal(Number(legacyStart.code), 0, legacyStart.stderr);
109+
assert.equal(legacyStart.stdout.trim(), "serve");
110+
}
97111
}),
98112
);
99113

‎apps/server/src/cloud/pinnedRuntime.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Option from "effect/Option";
88
import * as Semaphore from "effect/Semaphore";
99
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
1010

11+
import { legacyCliLauncherScript } from "@t3tools/shared/legacyCliLauncher";
1112
import {
1213
CLI_RELEASE_CHECKSUMS_FILE,
1314
cliArchiveFileName,
@@ -290,6 +291,22 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
290291
return yield* Effect.gen(function* () {
291292
yield* installFromArchive(input, stagingDir);
292293

294+
// A launcher already running the old npm layout cannot discover the
295+
// archive entry point. Keep its path working on subsequent updates too.
296+
const legacyDir = input.path.join(stagingDir, "node_modules", "t3", "dist");
297+
yield* fs.makeDirectory(legacyDir, { recursive: true }).pipe(
298+
Effect.andThen(
299+
fs.writeFileString(
300+
input.path.join(legacyDir, "bin.mjs"),
301+
legacyCliLauncherScript("archive"),
302+
),
303+
),
304+
Effect.mapError(
305+
(cause) =>
306+
new PinnedRuntimeInstallError({ step: "writing the legacy service entry point", cause }),
307+
),
308+
);
309+
293310
yield* input.validate(stagingPaths);
294311
yield* fs
295312
.writeFileString(stagingPaths.sentinelPath, `${input.version}\n`)

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"private": true,
44
"type": "module",
55
"exports": {
6+
"./legacyCliLauncher": {
7+
"types": "./src/legacyCliLauncher.ts",
8+
"import": "./src/legacyCliLauncher.ts"
9+
},
610
"./delimitedPreview": {
711
"types": "./src/delimitedPreview.ts",
812
"import": "./src/delimitedPreview.ts"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Exercises real Node IPC and process signals.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeEvents from "node:events";
4+
import * as NodeFSP from "node:fs/promises";
5+
import * as NodeOS from "node:os";
6+
import * as NodePath from "node:path";
7+
import { expect, it } from "vite-plus/test";
8+
9+
import { legacyCliLauncherScript } from "./legacyCliLauncher.ts";
10+
11+
// oxlint-disable-next-line t3code/no-global-process-runtime -- This test launches a real host executable.
12+
const hostPlatform = NodeOS.platform();
13+
// oxlint-disable-next-line t3code/no-global-process-runtime -- Match the real executable used by the subprocess.
14+
const hostArch = NodeOS.arch();
15+
16+
// The fixture executable uses a POSIX shebang. The wrapper itself also runs on Windows.
17+
it.skipIf(hostPlatform === "win32").each(["npm", "archive"] as const)(
18+
"keeps %s service IPC, arguments, and termination connected",
19+
async (distribution) => {
20+
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-legacy-launcher-"));
21+
const entry = NodePath.join(root, "node_modules/t3/dist/bin.mjs");
22+
const executable =
23+
distribution === "archive"
24+
? NodePath.join(root, "t3")
25+
: NodePath.join(root, `node_modules/@t3code/t3-${hostPlatform}-${hostArch}/t3`);
26+
await NodeFSP.mkdir(NodePath.dirname(entry), { recursive: true });
27+
await NodeFSP.mkdir(NodePath.dirname(executable), { recursive: true });
28+
await NodeFSP.writeFile(
29+
NodePath.join(NodePath.dirname(executable), "package.json"),
30+
'{"type":"commonjs"}',
31+
);
32+
await NodeFSP.writeFile(entry, legacyCliLauncherScript(distribution));
33+
await NodeFSP.writeFile(
34+
executable,
35+
`#!${process.execPath}
36+
process.on("SIGTERM", () => process.exit(23));
37+
process.on("message", message => process.send({ reply: message }));
38+
process.send({ args: process.argv.slice(2) });
39+
`,
40+
);
41+
await NodeFSP.chmod(executable, 0o755);
42+
const child = NodeChildProcess.fork(entry, ["serve", "a path with spaces"], { silent: true });
43+
try {
44+
expect((await NodeEvents.EventEmitter.once(child, "message"))[0]).toEqual({
45+
args: ["serve", "a path with spaces"],
46+
});
47+
const reply = NodeEvents.EventEmitter.once(child, "message");
48+
child.send({ type: "trial-accepted" });
49+
expect((await reply)[0]).toEqual({ reply: { type: "trial-accepted" } });
50+
const exit = NodeEvents.EventEmitter.once(child, "exit");
51+
child.kill("SIGTERM");
52+
expect(await exit).toEqual([23, null]);
53+
} finally {
54+
if (child.exitCode === null && child.signalCode === null) {
55+
const exit = NodeEvents.EventEmitter.once(child, "exit");
56+
child.kill("SIGTERM");
57+
await exit;
58+
}
59+
await NodeFSP.rm(root, { recursive: true, force: true });
60+
}
61+
},
62+
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** Node entry point for service launchers installed before executable releases. */
2+
export function legacyCliLauncherScript(distribution: "npm" | "archive"): string {
3+
const executable =
4+
distribution === "npm"
5+
? 'join(dirname(require.resolve("@t3code/t3-" + process.platform + "-" + process.arch + "/package.json")), executableName)'
6+
: 'resolve(dirname(fileURLToPath(import.meta.url)), "../../..", executableName)';
7+
return `import { spawn } from "node:child_process";
8+
import { constants } from "node:os";
9+
import { dirname, join, resolve } from "node:path";
10+
import { fileURLToPath } from "node:url";
11+
import { createRequire } from "node:module";
12+
const require = createRequire(import.meta.url);
13+
const executableName = process.platform === "win32" ? "t3.exe" : "t3";
14+
const executable = ${executable};
15+
const ipc = process.send !== undefined;
16+
const child = spawn(executable, process.argv.slice(2), {
17+
stdio: ipc ? ["inherit", "inherit", "inherit", "ipc"] : "inherit",
18+
});
19+
const fail = (error) => {
20+
if (!error) return;
21+
process.stderr.write("t3: " + error.message + "\\n");
22+
child.kill("SIGTERM");
23+
process.exitCode = 1;
24+
};
25+
if (ipc) {
26+
process.on("message", (message) => { if (child.connected) child.send(message, fail); });
27+
child.on("message", (message) => { if (process.connected) process.send(message, fail); });
28+
process.on("disconnect", () => { if (child.connected) child.disconnect(); });
29+
}
30+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
31+
process.on(signal, () => child.kill(signal));
32+
}
33+
child.on("error", (error) => { fail(error); process.exit(1); });
34+
child.on("exit", (code, signal) => process.exit(code ?? 128 + (constants.signals[signal] || 1)));
35+
`;
36+
}

‎scripts/build-npm-platform-packages.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
12
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import { assert, it } from "@effect/vitest";
34
import * as Effect from "effect/Effect";
@@ -151,7 +152,7 @@ it.layer(NodeServices.layer)("build-npm-platform-packages", (it) => {
151152
assert.equal(launcherManifest.name, "t3");
152153
assert.equal(launcherManifest.version, VERSION);
153154
assert.deepStrictEqual(launcherManifest.bin, { t3: "./bin/t3.js" });
154-
assert.deepStrictEqual(launcherManifest.files, ["bin"]);
155+
assert.deepStrictEqual(launcherManifest.files, ["bin", "dist"]);
155156
assert.deepStrictEqual(launcherManifest.optionalDependencies, {
156157
"@t3code/t3-darwin-arm64": VERSION,
157158
"@t3code/t3-linux-x64": VERSION,
@@ -182,14 +183,46 @@ it.layer(NodeServices.layer)("build-npm-platform-packages", (it) => {
182183

183184
// NODE_PATH stands in for node_modules: require.resolve finds the
184185
// platform package there exactly as it would after `npm install`.
186+
const hostPlatform = yield* HostProcessPlatform;
187+
const hostArch = yield* HostProcessArchitecture;
185188
const env = { ...process.env, NODE_PATH: fixture.outputDir } as Record<string, string>;
186189
const passthrough = yield* run(process.execPath, ["bin/t3.js", "serve", "--port", "1234"], {
187190
cwd: launcherDir,
188191
env,
189192
});
190-
assert.equal(passthrough.stdout.trim(), "stub linux-x64 serve --port 1234");
193+
assert.equal(passthrough.stdout.trim(), `stub ${hostPlatform}-${hostArch} serve --port 1234`);
191194
assert.equal(passthrough.exitCode, 7);
192195

196+
// Run the entry point used by already-installed service updaters from
197+
// the published tarball, including their preflight arguments.
198+
const installedLauncher = path.join(fixture.root, "installed-launcher");
199+
yield* fs.makeDirectory(installedLauncher);
200+
const unpack = yield* run(
201+
"tar",
202+
["-xf", path.join(fixture.outputDir, "t3.tgz"), "-C", installedLauncher],
203+
{
204+
cwd: fixture.root,
205+
},
206+
);
207+
assert.equal(unpack.exitCode, 0, unpack.stderr);
208+
const legacy = yield* run(
209+
process.execPath,
210+
[
211+
"dist/bin.mjs",
212+
"__service-preflight",
213+
"--database-path",
214+
"a database.sqlite",
215+
"--launcher-protocol",
216+
"2",
217+
],
218+
{ cwd: path.join(installedLauncher, "package"), env },
219+
);
220+
assert.equal(
221+
legacy.stdout.trim(),
222+
`stub ${hostPlatform}-${hostArch} __service-preflight --database-path a database.sqlite --launcher-protocol 2`,
223+
);
224+
assert.equal(legacy.exitCode, 7);
225+
193226
const unsupported = yield* run(process.execPath, ["bin/t3.js", "--version"], {
194227
cwd: launcherDir,
195228
env: { ...env, NODE_PATH: path.join(fixture.root, "nowhere") },

‎scripts/build-npm-platform-packages.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* bundleDependencies needs an arborist tree these flattened installs are
2020
* not), whereas `npm publish <tarball>` uploads the bytes as given.
2121
*/
22+
import { legacyCliLauncherScript } from "@t3tools/shared/legacyCliLauncher";
2223
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2324
import * as NodeServices from "@effect/platform-node/NodeServices";
2425
import * as Effect from "effect/Effect";
@@ -137,7 +138,7 @@ export function npmLauncherPackageManifest(
137138
license: serverPackageJson.license,
138139
repository: serverPackageJson.repository,
139140
bin: { t3: "./bin/t3.js" },
140-
files: ["bin"],
141+
files: ["bin", "dist"],
141142
optionalDependencies: Object.fromEntries(
142143
platformKeys.map((key) => [npmPlatformPackageName(key), version]),
143144
),
@@ -342,6 +343,10 @@ const stageLauncherPackage = Effect.fn("stageLauncherPackage")(function* (input:
342343
const launcherScript = path.join(stageDir, "bin/t3.js");
343344
yield* fs.writeFileString(launcherScript, NPM_LAUNCHER_SCRIPT);
344345
yield* fs.chmod(launcherScript, 0o755);
346+
// Older service updaters and launchers run this exact path with Node.
347+
// Keep it in the package so they can preflight and start the new executable.
348+
yield* fs.makeDirectory(path.join(stageDir, "dist"));
349+
yield* fs.writeFileString(path.join(stageDir, "dist/bin.mjs"), legacyCliLauncherScript("npm"));
345350
const readme = yield* path.fromFileUrl(new URL("../apps/server/README.md", import.meta.url));
346351
if (yield* fs.exists(readme)) {
347352
yield* fs.copyFile(readme, path.join(stageDir, "README.md"));

0 commit comments

Comments
 (0)