Skip to content

Commit 1d67bac

Browse files
refactor(cli): npm launcher is ESM
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eef8dd3 commit 1d67bac

7 files changed

Lines changed: 27 additions & 23 deletions

File tree

‎apps/server/src/cli/invocation.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
239239
yield* fs.writeFileString(executable, "");
240240
for (const [relative, kind] of [
241241
["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
242-
["npm/_npx/hash/node_modules/t3/bin/t3.js", "npx"],
242+
["npm/_npx/hash/node_modules/t3/bin/t3.mjs", "npx"],
243243
["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
244244
[".bun/install/cache/t3/dist/bin.mjs", "bunx"],
245245
] as const) {
@@ -248,9 +248,9 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
248248
yield* fs.writeFileString(entry, "");
249249
yield* fs.writeFileString(
250250
path.join(path.dirname(entry), "../package.json"),
251-
`{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.js"}}`,
251+
`{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.mjs"}}`,
252252
);
253-
const launchedBy = entry.endsWith("t3.js");
253+
const launchedBy = entry.endsWith("t3.mjs");
254254
const installation = yield* resolveServerInstallation.pipe(
255255
Effect.provideService(HostProcess.Arguments, [launchedBy ? executable : "node", entry]),
256256
Effect.provideService(HostProcess.ExecutablePath, executable),
@@ -303,7 +303,7 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
303303
const root = yield* fs.makeTempDirectoryScoped();
304304
const prefix = path.join(root, "bunx-tools");
305305
const packageRoot = path.join(prefix, "lib/node_modules/t3");
306-
const launcher = path.join(packageRoot, "bin/t3.js");
306+
const launcher = path.join(packageRoot, "bin/t3.mjs");
307307
const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
308308
yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
309309
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
@@ -326,7 +326,7 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
326326
] as const) {
327327
yield* fs.writeFileString(
328328
path.join(packageRoot, "package.json"),
329-
`{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.js"}}`,
329+
`{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.mjs"}}`,
330330
);
331331
expect(yield* resolve).toEqual(expected);
332332
}

‎apps/server/src/cli/invocation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const resolveServerInstallation = Effect.gen(function* () {
8585
const path = yield* Path.Path;
8686
const platform = yield* HostProcess.Platform;
8787
const entry = yield* fs.realPath(yield* resolveEntryPath);
88-
const match = /^(.*)\/lib\/node_modules\/t3\/(?:dist\/bin\.mjs|bin\/t3\.js)$/.exec(entry);
88+
const match = /^(.*)\/lib\/node_modules\/t3\/(?:dist\/bin\.mjs|bin\/t3\.mjs)$/.exec(entry);
8989
if (!match) {
9090
const runner = detectCliRunner(entry);
9191
return runner === null

‎docs/operations/release.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ the version.
4747
- The executable is built with a Node that supports `--build-sea` (`VP_NODE_VERSION=26.8.2`, kept in step with `SEA_NODE_VERSION` in `apps/server/vite.config.ts`), while the repo stays on `engines.node`.
4848
- macOS archives are signed with the Developer ID certificate and notarized when the Apple secrets are present (ad hoc otherwise, which still runs from `curl`/`tar` installs). Windows executables use the same Azure Trusted Signing setup as the installer. Every native addon in the macOS archive is signed too, since the hardened runtime refuses unsigned libraries.
4949
- Each archive is extracted and executed on its build runner (`scripts/smoke-cli-archive.ts`) before it is uploaded.
50-
- After the GitHub Release is published, publishes the `t3` npm package with OIDC trusted publishing from the same workflow file. It is only a launcher (`scripts/npm-launcher/t3.js`): on first run it downloads its exact version's CLI archive from the GitHub Release, verifies it against `SHA256SUMS`, and unpacks it into `<T3 home>/runtime/versions/<version>`, the layout the install scripts and `t3 update` share. The executables are not on npm. They used to be, as `@t3code/t3-<platform>-<arch>` optional dependencies, but npm's publish-time scanning held those 80-250 MB packages back for up to an hour after the launcher was live, and every install in that window got a launcher with no executable. Those packages stay on npm at their old versions and get no new ones. Because the launcher reads from the release, `publish_cli` runs after `release`, never before.
50+
- After the GitHub Release is published, publishes the `t3` npm package with OIDC trusted publishing from the same workflow file. It is only a launcher (`scripts/npm-launcher/t3.mjs`): on first run it downloads its exact version's CLI archive from the GitHub Release, verifies it against `SHA256SUMS`, and unpacks it into `<T3 home>/runtime/versions/<version>`, the layout the install scripts and `t3 update` share. The executables are not on npm. They used to be, as `@t3code/t3-<platform>-<arch>` optional dependencies, but npm's publish-time scanning held those 80-250 MB packages back for up to an hour after the launcher was live, and every install in that window got a launcher with no executable. Those packages stay on npm at their old versions and get no new ones. Because the launcher reads from the release, `publish_cli` runs after `release`, never before.
5151
- stable releases publish npm dist-tag `latest`
5252
- nightly releases publish npm dist-tag `nightly`
5353
- preview releases publish npm dist-tag `preview`, which nothing resolves unless asked for by name

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
116116
);
117117
assert.equal(manifest.name, "t3");
118118
assert.equal(manifest.version, VERSION);
119-
assert.deepStrictEqual(manifest.bin, { t3: "./bin/t3.js" });
119+
assert.deepStrictEqual(manifest.bin, { t3: "./bin/t3.mjs" });
120120
// Nothing for npm to resolve, so nothing that can lag behind the launcher.
121121
assert.isUndefined(manifest.dependencies);
122122
assert.isUndefined(manifest.optionalDependencies);
@@ -138,7 +138,7 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
138138
T3CODE_HOME: home,
139139
T3CODE_RELEASE_BASE_URL: release.baseUrl,
140140
};
141-
const launcher = path.join(packageDir, "bin/t3.js");
141+
const launcher = path.join(packageDir, "bin/t3.mjs");
142142
const first = yield* run(process.execPath, [launcher, "serve", "a b"], {
143143
cwd: root,
144144
env,

‎scripts/build-npm-launcher.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Builds the `t3` npm package behind `npx t3` / `npm i -g t3`: a launcher
44
* with no dependencies that downloads this exact version's CLI archive from
5-
* the GitHub Release on first run (scripts/npm-launcher/t3.js).
5+
* the GitHub Release on first run (scripts/npm-launcher/t3.mjs).
66
* The executables are not published to npm. npm's publish-time scanning held
77
* 250 MB platform packages back for up to an hour after the launcher went
88
* live, and every install in that window ran a launcher with nothing to
@@ -50,7 +50,7 @@ export function npmLauncherPackageManifest(version: string) {
5050
"T3 Code CLI. Downloads the self-contained executable for this platform from the GitHub Release.",
5151
license: serverPackageJson.license,
5252
repository: serverPackageJson.repository,
53-
bin: { t3: "./bin/t3.js" },
53+
bin: { t3: "./bin/t3.mjs" },
5454
files: ["bin", "dist"],
5555
engines: { node: ">=18" },
5656
};
@@ -77,8 +77,8 @@ export const buildNpmLauncher = Effect.fn("buildNpmLauncher")(function* (input:
7777
path.join(stageDir, "package.json"),
7878
`${yield* encodePackageJson(npmLauncherPackageManifest(input.version))}\n`,
7979
);
80-
yield* fs.copyFile(path.join(sourceDir, "t3.js"), path.join(stageDir, "bin/t3.js"));
81-
yield* fs.chmod(path.join(stageDir, "bin/t3.js"), 0o755);
80+
yield* fs.copyFile(path.join(sourceDir, "t3.mjs"), path.join(stageDir, "bin/t3.mjs"));
81+
yield* fs.chmod(path.join(stageDir, "bin/t3.mjs"), 0o755);
8282
yield* fs.copyFile(path.join(sourceDir, "bin.mjs"), path.join(stageDir, "dist/bin.mjs"));
8383
const readme = yield* path.fromFileUrl(new URL("../apps/server/README.md", import.meta.url));
8484
if (yield* fs.exists(readme)) {

‎scripts/npm-launcher/bin.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Background services installed before 0.0.41 start the npm package from this path.
2-
import "../bin/t3.js";
2+
import "../bin/t3.mjs";
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/usr/bin/env node
2-
"use strict";
32
// The `t3` npm package. Downloads this version's CLI archive from the GitHub
43
// Release into <T3 home>/runtime/versions/<version> (the folder the install
54
// scripts and `t3 update` use), verifies it against the release's SHA256SUMS,
65
// then runs it. `dist/bin.mjs` re-enters here for background services
76
// installed before 0.0.41, which start the npm package with Node and talk to
87
// it over an IPC channel.
9-
const { spawn, spawnSync } = require("node:child_process");
10-
const crypto = require("node:crypto");
11-
const fs = require("node:fs");
12-
const os = require("node:os");
13-
const path = require("node:path");
14-
const { version } = require("../package.json");
8+
import { spawn, spawnSync } from "node:child_process";
9+
import crypto from "node:crypto";
10+
import fs from "node:fs";
11+
import os from "node:os";
12+
import path from "node:path";
13+
import { fileURLToPath } from "node:url";
14+
15+
const launcherPath = fileURLToPath(import.meta.url);
16+
const { version } = JSON.parse(
17+
fs.readFileSync(path.join(path.dirname(launcherPath), "../package.json"), "utf8"),
18+
);
1519

1620
const SUPPORTED = ["darwin-arm64", "linux-arm64", "linux-x64", "win32-arm64", "win32-x64"];
1721

@@ -124,7 +128,7 @@ async function main() {
124128
{
125129
stdio: ipc ? ["inherit", "inherit", "inherit", "ipc"] : "inherit",
126130
// The executable lives in the T3 home, so this is how it knows npm launched it.
127-
env: { ...process.env, T3CODE_NPM_LAUNCHER_PATH: __filename },
131+
env: { ...process.env, T3CODE_NPM_LAUNCHER_PATH: launcherPath },
128132
},
129133
);
130134
if (ipc) {
@@ -146,4 +150,4 @@ function fail(error) {
146150
process.exit(1);
147151
}
148152

149-
main().catch(fail);
153+
await main().catch(fail);

0 commit comments

Comments
 (0)