Skip to content

Commit 4fe545c

Browse files
committed
fix(server): update mise-managed providers through mise
Provider update capabilities were resolved from the binary path and only knew npm, bun, pnpm, vp, Homebrew, and native installers. A provider installed with mise fell through to `npm install -g`, which fails or installs a second copy, and an Omarchy wrapper for claude at `~/.local/bin/claude` matched the native-installer check so T3 Code would run `claude update` against a binary that mise owns. Detect mise through its install tree, its shims, and small wrapper scripts that `mise x` the tool, and run `mise upgrade --bump <tool>` instead. Binaries under mise's own `node` install remain ordinary npm globals. Ref #8051
1 parent 70cd258 commit 4fe545c

2 files changed

Lines changed: 171 additions & 1 deletion

File tree

‎apps/server/src/provider/providerMaintenance.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,95 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
582582
});
583583
});
584584

585+
const miseCapabilities = (tool: string) => ({
586+
provider: driver("packageTool"),
587+
packageName: "@example/package-tool",
588+
update: {
589+
command: `mise upgrade --bump ${tool}`,
590+
executable: "mise",
591+
args: ["upgrade", "--bump", tool],
592+
lockKey: "mise",
593+
},
594+
});
595+
596+
it.effect("switches to mise upgrades when the binary lives in mise's install tree", () =>
597+
Effect.gen(function* () {
598+
const tempDir = yield* makeTempDir("t3-mise-capabilities");
599+
const installDir = NodePath.join(tempDir, "mise", "installs", "package-tool");
600+
const versionBinDir = NodePath.join(installDir, "1.0.0", "bin");
601+
NodeFS.mkdirSync(versionBinDir, { recursive: true });
602+
const packageToolPath = NodePath.join(versionBinDir, "package-tool");
603+
NodeFS.writeFileSync(packageToolPath, "ELF");
604+
NodeFS.chmodSync(packageToolPath, 0o755);
605+
NodeFS.symlinkSync("1.0.0", NodePath.join(installDir, "latest"));
606+
607+
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
608+
binaryPath: NodePath.join(installDir, "latest", "bin", "package-tool"),
609+
env: { PATH: "" },
610+
});
611+
612+
expect(capabilities).toEqual(miseCapabilities("package-tool"));
613+
}),
614+
);
615+
616+
it("maps mise's npm backend install directory back to the npm package", () => {
617+
expect(
618+
packageToolUpdate.resolve({
619+
binaryPath:
620+
"/home/u/.local/share/mise/installs/npm-example-package-tool/1.0.0/lib/node_modules/@example/package-tool/bin/package-tool.js",
621+
env: { PATH: "" },
622+
}),
623+
).toEqual(miseCapabilities("npm:@example/package-tool"));
624+
});
625+
626+
it("keeps npm updates for globals installed under a mise-managed node", () => {
627+
expect(
628+
packageToolUpdate.resolve({
629+
binaryPath:
630+
"/home/u/.local/share/mise/installs/node/26.7.0/lib/node_modules/@example/package-tool/bin/package-tool.js",
631+
env: { PATH: "" },
632+
}).update?.executable,
633+
).toBe("npm");
634+
});
635+
636+
it("names the tool by its bin name for mise shims", () => {
637+
expect(
638+
packageToolUpdate.resolve({
639+
binaryPath: "C:\\Users\\u\\AppData\\Local\\mise\\shims\\package-tool.exe",
640+
env: { PATH: "" },
641+
}),
642+
).toEqual(miseCapabilities("package-tool"));
643+
});
644+
645+
it.effect("prefers mise over native updates for wrapper scripts that exec through mise", () =>
646+
Effect.gen(function* () {
647+
const tempDir = yield* makeTempDir("t3-mise-wrapper-capabilities");
648+
const nativeBinDir = NodePath.join(tempDir, ".local", "bin");
649+
NodeFS.mkdirSync(nativeBinDir, { recursive: true });
650+
const wrapperPath = NodePath.join(nativeBinDir, "native-package-tool");
651+
NodeFS.writeFileSync(
652+
wrapperPath,
653+
'#!/bin/bash\nexport MISE_MINIMUM_RELEASE_AGE=0\nmise use -g --quiet "native-package-tool" || exit 1\nexec mise x "native-package-tool" -- "native-package-tool" "$@"\n',
654+
);
655+
NodeFS.chmodSync(wrapperPath, 0o755);
656+
657+
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
658+
nativePackageToolUpdate,
659+
{
660+
binaryPath: "native-package-tool",
661+
env: { PATH: nativeBinDir },
662+
},
663+
).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
664+
665+
expect(capabilities.update).toEqual({
666+
command: "mise upgrade --bump native-package-tool",
667+
executable: "mise",
668+
args: ["upgrade", "--bump", "native-package-tool"],
669+
lockKey: "mise",
670+
});
671+
}),
672+
);
673+
585674
it("disables one-click updates for explicit custom binary paths it cannot safely map", () => {
586675
expect(
587676
packageToolUpdate.resolve({

‎apps/server/src/provider/providerMaintenance.ts‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export interface ProviderMaintenanceCapabilityResolutionOptions {
5555
readonly env?: NodeJS.ProcessEnv;
5656
readonly resolvedCommandPath?: string | null;
5757
readonly realCommandPath?: string | null;
58+
/** Contents of the resolved command when it is a small script, so wrappers can be recognised. */
59+
readonly commandScript?: string | null;
5860
}
5961

6062
export interface ProviderMaintenanceCapabilitiesResolver {
@@ -188,6 +190,21 @@ function makeVitePlusGlobalProviderMaintenanceCapabilities(
188190
});
189191
}
190192

193+
function makeMiseProviderMaintenanceCapabilities(
194+
definition: PackageManagedProviderMaintenanceDefinition,
195+
tool: string,
196+
): ProviderMaintenanceCapabilities {
197+
return makeProviderMaintenanceCapabilities({
198+
provider: definition.provider,
199+
packageName: definition.npmPackageName,
200+
updateExecutable: "mise",
201+
// `--bump` moves past an exact version pin in the user's config; the
202+
// usual `latest` pin stays `latest`.
203+
updateArgs: ["upgrade", "--bump", tool],
204+
updateLockKey: "mise",
205+
});
206+
}
207+
191208
function makeHomebrewProviderMaintenanceCapabilities(
192209
definition: PackageManagedProviderMaintenanceDefinition,
193210
): ProviderMaintenanceCapabilities {
@@ -259,6 +276,40 @@ function isNpmGlobalCommandPath(commandPath: string): boolean {
259276
);
260277
}
261278

279+
const MISE_INSTALL_PATH_PATTERN = /\/mise\/installs\/([^/]+)\//;
280+
const MISE_SHIM_PATH_PATTERN = /\/mise\/shims\/([^/]+)$/;
281+
// Wrapper scripts such as Omarchy's `exec mise x "codex" -- "codex" "$@"`.
282+
const MISE_EXEC_SCRIPT_PATTERN = /\bmise\s+(?:x|exec)\s+"?([^\s"]+)"?/;
283+
284+
/**
285+
* Maps a mise-managed command to the tool spec `mise upgrade` expects, or
286+
* null when mise does not manage it. Recognises binaries under mise's install
287+
* tree, mise shims, and small wrapper scripts that `mise x` the tool. Binaries
288+
* under mise's `node` install are ordinary npm globals and are left to npm.
289+
* Install directories are kebab-cased tool specs, so only the npm backend can
290+
* be mapped back to a spec; shims and wrappers name the tool by its bin name.
291+
*/
292+
function resolveMiseTool(input: {
293+
readonly commandPaths: ReadonlyArray<string>;
294+
readonly commandScript: string | null | undefined;
295+
readonly npmPackageName: string;
296+
}): string | null {
297+
for (const commandPath of input.commandPaths) {
298+
const normalized = normalizeCommandPath(commandPath);
299+
const installDir = MISE_INSTALL_PATH_PATTERN.exec(normalized)?.[1];
300+
if (installDir && installDir !== "node") {
301+
return installDir.startsWith("npm-") ? `npm:${input.npmPackageName}` : installDir;
302+
}
303+
const shim = MISE_SHIM_PATH_PATTERN.exec(normalized)?.[1];
304+
if (shim) {
305+
return shim.replace(/\.(exe|cmd)$/, "");
306+
}
307+
}
308+
return input.commandScript
309+
? (MISE_EXEC_SCRIPT_PATTERN.exec(input.commandScript)?.[1] ?? null)
310+
: null;
311+
}
312+
262313
function isHomebrewCommandPath(commandPath: string): boolean {
263314
const normalized = normalizeCommandPath(commandPath);
264315
return (
@@ -291,6 +342,17 @@ export function resolvePackageManagedProviderMaintenance(
291342
...(options?.realCommandPath ? [options.realCommandPath] : []),
292343
];
293344

345+
// Checked before native installers: a mise wrapper for claude lives at
346+
// `~/.local/bin/claude`, where the native check would otherwise send
347+
// `claude update` at a binary that mise owns.
348+
const miseTool = resolveMiseTool({
349+
commandPaths,
350+
commandScript: options?.commandScript,
351+
npmPackageName: definition.npmPackageName,
352+
});
353+
if (miseTool) {
354+
return makeMiseProviderMaintenanceCapabilities(definition, miseTool);
355+
}
294356
const nativeUpdate = definition.nativeUpdate;
295357
if (
296358
nativeUpdate &&
@@ -353,11 +415,28 @@ function makeManualProviderMaintenanceCapabilities(
353415
});
354416
}
355417

418+
const COMMAND_SCRIPT_MAX_BYTES = 4_096n;
419+
420+
/** Reads the resolved command when it is a small file; binaries and large launchers yield null. */
421+
const readCommandScript = Effect.fn("readCommandScript")(function* (
422+
fileSystem: FileSystem.FileSystem,
423+
commandPath: string,
424+
) {
425+
const info = yield* fileSystem.stat(commandPath).pipe(Effect.orElseSucceed(() => null));
426+
if (!info || info.type !== "File" || info.size > COMMAND_SCRIPT_MAX_BYTES) {
427+
return null;
428+
}
429+
return yield* fileSystem.readFileString(commandPath).pipe(Effect.orElseSucceed(() => null));
430+
});
431+
356432
export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
357433
"resolveProviderMaintenanceCapabilitiesEffect",
358434
)(function* (
359435
resolver: ProviderMaintenanceCapabilitiesResolver,
360-
options?: Omit<ProviderMaintenanceCapabilityResolutionOptions, "realCommandPath">,
436+
options?: Omit<
437+
ProviderMaintenanceCapabilityResolutionOptions,
438+
"realCommandPath" | "commandScript"
439+
>,
361440
) {
362441
const binaryPath = nonEmptyString(options?.binaryPath);
363442
if (!binaryPath) {
@@ -377,11 +456,13 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
377456
const realCommandPath = yield* fileSystem
378457
.realPath(resolvedCommandPath)
379458
.pipe(Effect.orElseSucceed(() => resolvedCommandPath));
459+
const commandScript = yield* readCommandScript(fileSystem, realCommandPath);
380460
return resolver.resolve({
381461
...options,
382462
env,
383463
resolvedCommandPath,
384464
realCommandPath,
465+
commandScript,
385466
});
386467
});
387468

0 commit comments

Comments
 (0)