Skip to content

Commit 6268cd5

Browse files
committed
fix(provider-core): pnpm-global updates approve the provider's build
pnpm 10+ skips unapproved install scripts and still exits 0, so an update left a package like @opencode/cli on its placeholder binary. Pass --allow-build=<package> when the probed pnpm is 10.4 or newer; older pnpm rejects the flag. Fixes #20783
1 parent 8653c8f commit 6268cd5

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

‎packages/provider-core/src/server/maintenanceResolver.test.ts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,32 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
395395
"package-tool",
396396
]);
397397

398-
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
399-
packageToolUpdate,
400-
{
398+
const updateCommand = (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) =>
399+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
401400
binaryPath: link,
402401
env: { PATH: "" },
403-
},
404-
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
405-
406-
expect(capabilities.update).toMatchObject({
407-
command: "pnpm add -g @example/package-tool@latest",
402+
}).pipe(
403+
Effect.map((capabilities) => capabilities.update),
404+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
405+
);
406+
const pnpmVersion = (version: string) =>
407+
stdoutSpawner((command, args) => {
408+
expect([command, ...args].join(" ")).toMatch(/pnpm(\.cmd)?"? --version$/);
409+
return `${version}
410+
`;
411+
});
412+
413+
expect(yield* updateCommand(pnpmVersion("11.1.3"))).toMatchObject({
414+
command: "pnpm add -g --allow-build=@example/package-tool @example/package-tool@latest",
408415
lockKey: "pnpm-global",
409416
});
417+
// Older pnpm rejects the unknown flag, and runs install scripts before 10.
418+
expect((yield* updateCommand(pnpmVersion("10.3.0")))?.command).toBe(
419+
"pnpm add -g @example/package-tool@latest",
420+
);
421+
expect((yield* updateCommand(noSpawn))?.command).toBe(
422+
"pnpm add -g @example/package-tool@latest",
423+
);
410424
}),
411425
);
412426

‎packages/provider-core/src/server/maintenanceResolver.ts‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
type ServerProvider,
1212
type ServerProviderVersionAdvisory,
1313
} from "@t3tools/contracts";
14-
import { compareSemverVersions } from "@t3tools/shared/semver";
14+
import { compareSemverVersions, satisfiesSemverRange } from "@t3tools/shared/semver";
1515
import * as HostProcess from "@t3tools/shared/HostProcess";
1616
import { causeErrorTag } from "@t3tools/shared/observability";
17-
import { resolveCommandPath } from "@t3tools/shared/shell";
17+
import { resolveCommandPath, resolveSpawnCommand } from "@t3tools/shared/shell";
1818
import * as Cache from "effect/Cache";
1919
import * as Config from "effect/Config";
2020
import * as DateTime from "effect/DateTime";
@@ -34,8 +34,8 @@ import { collectUint8StreamText } from "./collectStreamText.ts";
3434
import * as ProviderLatestVersions from "./ProviderLatestVersions.ts";
3535

3636
const LATEST_VERSION_TIMEOUT_MS = 4_000;
37-
const HOMEBREW_INFO_TIMEOUT_MS = 10_000;
38-
const HOMEBREW_INFO_MAX_BYTES = 256 * 1_024;
37+
const PROBE_TIMEOUT_MS = 10_000;
38+
const PROBE_MAX_BYTES = 256 * 1_024;
3939
const MISE_WRAPPER_MAX_BYTES = 16 * 1_024;
4040
const PROVIDER_UPDATE_ACTION_TOAST_MESSAGE = "Install the update now or review provider settings.";
4141

@@ -293,19 +293,27 @@ export function parseHomebrewLatestVersion(
293293
return nonEmptyString(raw);
294294
}
295295

296-
const runHomebrew = Effect.fn("runHomebrew")(function* (
297-
brewPath: string,
296+
const runProbe = Effect.fn("runProbe")(function* (
297+
command: string,
298298
args: ReadonlyArray<string>,
299299
env: NodeJS.ProcessEnv,
300300
) {
301301
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
302302
const collect = Effect.gen(function* () {
303-
const child = yield* spawner.spawn(ChildProcess.make(brewPath, args, { env, extendEnv: true }));
303+
// Windows package managers are `.cmd` shims that need the shell.
304+
const resolved = yield* resolveSpawnCommand(command, args, { env, extendEnv: true });
305+
const child = yield* spawner.spawn(
306+
ChildProcess.make(resolved.command, resolved.args, {
307+
env,
308+
extendEnv: true,
309+
shell: resolved.shell,
310+
}),
311+
);
304312
yield* Effect.addFinalizer(() => child.kill().pipe(Effect.ignore));
305-
// stderr is drained so a chatty brew cannot block on a full pipe.
313+
// stderr is drained so a chatty tool cannot block on a full pipe.
306314
const [stdout, exitCode] = yield* Effect.all(
307315
[
308-
collectUint8StreamText({ stream: child.stdout, maxBytes: HOMEBREW_INFO_MAX_BYTES }),
316+
collectUint8StreamText({ stream: child.stdout, maxBytes: PROBE_MAX_BYTES }),
309317
child.exitCode,
310318
Stream.runDrain(child.stderr),
311319
],
@@ -315,10 +323,11 @@ const runHomebrew = Effect.fn("runHomebrew")(function* (
315323
});
316324
return yield* collect.pipe(
317325
Effect.scoped,
318-
Effect.timeoutOption(Duration.millis(HOMEBREW_INFO_TIMEOUT_MS)),
326+
Effect.timeoutOption(Duration.millis(PROBE_TIMEOUT_MS)),
319327
Effect.map(Option.getOrNull),
320328
Effect.catchCause((cause) =>
321-
Effect.logWarning("Homebrew probe failed", {
329+
Effect.logWarning("Maintenance probe failed", {
330+
command,
322331
subcommand: args[0],
323332
errorTag: causeErrorTag(cause),
324333
}).pipe(Effect.as(null)),
@@ -383,11 +392,20 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
383392
});
384393
}
385394
if (commandPaths.some(isPnpmGlobalCommandPath)) {
395+
// pnpm 10 skips unapproved install scripts and still exits 0, leaving a
396+
// package whose postinstall finishes the install broken. Global installs
397+
// can only approve on the command line, and pnpm before 10.4 rejects the
398+
// flag as an unknown option.
399+
const pnpmVersion = yield* runProbe("pnpm", ["--version"], context.env);
400+
const allowBuild =
401+
pnpmVersion !== null && satisfiesSemverRange(pnpmVersion, ">=10.4.0")
402+
? [`--allow-build=${packageName}`]
403+
: [];
386404
return makeProviderMaintenanceCapabilities({
387405
provider: definition.provider,
388406
packageName,
389407
updateExecutable: "pnpm",
390-
updateArgs: ["add", "-g", `${packageName}@latest`],
408+
updateArgs: ["add", "-g", ...allowBuild, `${packageName}@latest`],
391409
updateLockKey: "pnpm-global",
392410
});
393411
}
@@ -458,7 +476,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
458476
if (!brewPath) {
459477
return fallback;
460478
}
461-
const brewPrefix = nonEmptyString(yield* runHomebrew(brewPath, ["--prefix"], context.env));
479+
const brewPrefix = nonEmptyString(yield* runProbe(brewPath, ["--prefix"], context.env));
462480
const realBrewPrefix = brewPrefix
463481
? yield* fileSystem.realPath(brewPrefix).pipe(Effect.orElseSucceed(() => brewPrefix))
464482
: null;
@@ -472,7 +490,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
472490
homebrew.kind === "cask" ? ["upgrade", "--cask", homebrew.name] : ["upgrade", homebrew.name];
473491
// Homebrew lags npm by hours on every release, so compare against what
474492
// `brew upgrade` can actually deliver.
475-
const info = yield* runHomebrew(brewPath, ["info", "--json=v2", homebrew.name], context.env);
493+
const info = yield* runProbe(brewPath, ["info", "--json=v2", homebrew.name], context.env);
476494
return makeProviderMaintenanceCapabilities({
477495
provider: definition.provider,
478496
packageName,

0 commit comments

Comments
 (0)