Skip to content

Commit a523b00

Browse files
authored
fix(provider-core): pnpm-global updates approve the provider's build (pingdotgg#20796)
1 parent e918741 commit a523b00

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
@@ -549,18 +549,32 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
549549
"package-tool",
550550
]);
551551

552-
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
553-
packageToolUpdate,
554-
{
552+
const updateCommand = (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) =>
553+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
555554
binaryPath: link,
556555
env: { PATH: "" },
557-
},
558-
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
559-
560-
expect(capabilities.update).toMatchObject({
561-
command: "pnpm add -g @example/package-tool@latest",
556+
}).pipe(
557+
Effect.map((capabilities) => capabilities.update),
558+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
559+
);
560+
const pnpmVersion = (version: string) =>
561+
stdoutSpawner((command, args) => {
562+
expect([command, ...args].join(" ")).toMatch(/pnpm(\.cmd)?"? --version$/);
563+
return `${version}
564+
`;
565+
});
566+
567+
expect(yield* updateCommand(pnpmVersion("11.1.3"))).toMatchObject({
568+
command: "pnpm add -g --allow-build=@example/package-tool @example/package-tool@latest",
562569
lockKey: "pnpm-global",
563570
});
571+
// Older pnpm rejects the unknown flag, and runs install scripts before 10.
572+
expect((yield* updateCommand(pnpmVersion("10.3.0")))?.command).toBe(
573+
"pnpm add -g @example/package-tool@latest",
574+
);
575+
expect((yield* updateCommand(noSpawn))?.command).toBe(
576+
"pnpm add -g @example/package-tool@latest",
577+
);
564578
}),
565579
);
566580

‎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

@@ -310,19 +310,27 @@ export function parseHomebrewLatestVersion(
310310
return version === "latest" ? null : version;
311311
}
312312

313-
const runHomebrew = Effect.fn("runHomebrew")(function* (
314-
brewPath: string,
313+
const runProbe = Effect.fn("runProbe")(function* (
314+
command: string,
315315
args: ReadonlyArray<string>,
316316
env: NodeJS.ProcessEnv,
317317
) {
318318
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
319319
const collect = Effect.gen(function* () {
320-
const child = yield* spawner.spawn(ChildProcess.make(brewPath, args, { env, extendEnv: true }));
320+
// Windows package managers are `.cmd` shims that need the shell.
321+
const resolved = yield* resolveSpawnCommand(command, args, { env, extendEnv: true });
322+
const child = yield* spawner.spawn(
323+
ChildProcess.make(resolved.command, resolved.args, {
324+
env,
325+
extendEnv: true,
326+
shell: resolved.shell,
327+
}),
328+
);
321329
yield* Effect.addFinalizer(() => child.kill().pipe(Effect.ignore));
322-
// stderr is drained so a chatty brew cannot block on a full pipe.
330+
// stderr is drained so a chatty tool cannot block on a full pipe.
323331
const [stdout, exitCode] = yield* Effect.all(
324332
[
325-
collectUint8StreamText({ stream: child.stdout, maxBytes: HOMEBREW_INFO_MAX_BYTES }),
333+
collectUint8StreamText({ stream: child.stdout, maxBytes: PROBE_MAX_BYTES }),
326334
child.exitCode,
327335
Stream.runDrain(child.stderr),
328336
],
@@ -332,10 +340,11 @@ const runHomebrew = Effect.fn("runHomebrew")(function* (
332340
});
333341
return yield* collect.pipe(
334342
Effect.scoped,
335-
Effect.timeoutOption(Duration.millis(HOMEBREW_INFO_TIMEOUT_MS)),
343+
Effect.timeoutOption(Duration.millis(PROBE_TIMEOUT_MS)),
336344
Effect.map(Option.getOrNull),
337345
Effect.catchCause((cause) =>
338-
Effect.logWarning("Homebrew probe failed", {
346+
Effect.logWarning("Maintenance probe failed", {
347+
command,
339348
subcommand: args[0],
340349
errorTag: causeErrorTag(cause),
341350
}).pipe(Effect.as(null)),
@@ -413,11 +422,20 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
413422
});
414423
}
415424
if (commandPaths.some(isPnpmGlobalCommandPath)) {
425+
// pnpm 10 skips unapproved install scripts and still exits 0, leaving a
426+
// package whose postinstall finishes the install broken. Global installs
427+
// can only approve on the command line, and pnpm before 10.4 rejects the
428+
// flag as an unknown option.
429+
const pnpmVersion = yield* runProbe("pnpm", ["--version"], context.env);
430+
const allowBuild =
431+
pnpmVersion !== null && satisfiesSemverRange(pnpmVersion, ">=10.4.0")
432+
? [`--allow-build=${packageName}`]
433+
: [];
416434
return makeProviderMaintenanceCapabilities({
417435
provider: definition.provider,
418436
packageName,
419437
updateExecutable: "pnpm",
420-
updateArgs: ["add", "-g", `${packageName}@latest`],
438+
updateArgs: ["add", "-g", ...allowBuild, `${packageName}@latest`],
421439
updateLockKey: "pnpm-global",
422440
});
423441
}
@@ -488,7 +506,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
488506
if (!brewPath) {
489507
return fallback;
490508
}
491-
const brewPrefix = nonEmptyString(yield* runHomebrew(brewPath, ["--prefix"], context.env));
509+
const brewPrefix = nonEmptyString(yield* runProbe(brewPath, ["--prefix"], context.env));
492510
const realBrewPrefix = brewPrefix
493511
? yield* fileSystem.realPath(brewPrefix).pipe(Effect.orElseSucceed(() => brewPrefix))
494512
: null;
@@ -502,7 +520,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
502520
homebrew.kind === "cask" ? ["upgrade", "--cask", homebrew.name] : ["upgrade", homebrew.name];
503521
// Homebrew lags npm by hours on every release, so compare against what
504522
// `brew upgrade` can actually deliver.
505-
const info = yield* runHomebrew(brewPath, ["info", "--json=v2", homebrew.name], context.env);
523+
const info = yield* runProbe(brewPath, ["info", "--json=v2", homebrew.name], context.env);
506524
return makeProviderMaintenanceCapabilities({
507525
provider: definition.provider,
508526
packageName,

0 commit comments

Comments
 (0)