Skip to content

Commit 1a2983f

Browse files
ashx-jclaudemaria-rcks
authored
fix(provider-core): provider updates find npm on the provider's custom PATH (#27321)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 0743ba5 commit 1a2983f

5 files changed

Lines changed: 61 additions & 22 deletions

File tree

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,17 +856,31 @@ describe("providerMaintenanceRunner", () => {
856856
),
857857
);
858858

859-
it.effect("resolves npm to a .cmd shim and routes through the shell on win32", () => {
859+
it.effect("resolves npm to a .cmd shim from the provider's PATH on win32", () => {
860860
const captured: Array<{
861861
readonly command: string;
862862
readonly args: ReadonlyArray<string>;
863863
readonly shell: boolean | string | undefined;
864+
readonly env: NodeJS.ProcessEnv | undefined;
864865
}> = [];
865866
return Effect.gen(function* () {
866867
const { registry } = yield* makeRegistry(baseProvider, {
867868
installedVersionAfterUpdate: "1.0.0",
868869
});
869-
const runner = yield* makeTestRunner(registry);
870+
const runner = yield* makeTestRunner({
871+
...registry,
872+
getProviderMaintenanceCapabilitiesForInstance: () =>
873+
Effect.succeed(
874+
makeProviderMaintenanceCapabilities({
875+
provider: CODEX_DRIVER,
876+
packageName: "@openai/codex",
877+
updateExecutable: "npm",
878+
updateArgs: ["install", "-g", "@openai/codex@latest"],
879+
updateLockKey: "npm-global",
880+
env: { PATH: "C:\\fake\\npm" },
881+
}),
882+
),
883+
});
870884

871885
const result = yield* runner.updateProvider(CODEX_DRIVER);
872886

@@ -879,17 +893,21 @@ describe("providerMaintenanceRunner", () => {
879893
);
880894
assert.deepEqual(call.args, []);
881895
assert.strictEqual(call.shell, true);
896+
assert.deepStrictEqual(call.env, { PATH: "C:\\fake\\npm" });
882897
assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
883898
}).pipe(
884899
Effect.provide(
885900
Layer.mergeAll(
886901
Layer.succeed(HostProcess.Platform, "win32"),
902+
// Only the provider's PATH has npm.
887903
Layer.succeed(HostProcess.Environment, {
888-
PATH: "C:\\fake\\npm",
904+
PATH: "C:\\Windows\\System32",
889905
PATHEXT: ".COM;.EXE;.BAT;.CMD",
890906
}),
891-
Layer.succeed(SpawnExecutableResolution, (command) =>
892-
command === "npm" ? "C:\\fake\\npm\\npm.cmd" : undefined,
907+
Layer.succeed(SpawnExecutableResolution, (command, _platform, env) =>
908+
command === "npm" && env.PATH === "C:\\fake\\npm"
909+
? "C:\\fake\\npm\\npm.cmd"
910+
: undefined,
893911
),
894912
layerLatestVersionHttpClient("1.0.0"),
895913
Layer.succeed(
@@ -898,12 +916,16 @@ describe("providerMaintenanceRunner", () => {
898916
const childProcess = command as unknown as {
899917
readonly command: string;
900918
readonly args: ReadonlyArray<string>;
901-
readonly options: { readonly shell?: boolean | string | undefined };
919+
readonly options: {
920+
readonly shell?: boolean | string | undefined;
921+
readonly env?: NodeJS.ProcessEnv | undefined;
922+
};
902923
};
903924
captured.push({
904925
command: childProcess.command,
905926
args: childProcess.args,
906927
shell: childProcess.options.shell,
928+
env: childProcess.options.env,
907929
});
908930
return Effect.succeed(mockHandle({ stdout: "updated" }));
909931
}),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ const runProviderMaintenanceCommandWithSpawner = Effect.fn("ProviderMaintenanceR
9898
}) {
9999
const collectCommandResult = Effect.fn("ProviderMaintenanceRunner.collectCommandResult")(
100100
function* () {
101+
const env = input.env ? { env: input.env, extendEnv: true } : {};
101102
// Windows batch shims require shell dispatch through resolveSpawnCommand.
102-
const resolved = yield* resolveSpawnCommand(input.command, input.args);
103+
const resolved = yield* resolveSpawnCommand(input.command, input.args, env);
103104
const child = yield* input.spawner
104105
.spawn(
105106
ChildProcess.make(resolved.command, resolved.args, {
106107
shell: resolved.shell,
107-
...(input.env ? { env: input.env, extendEnv: true } : {}),
108+
...env,
108109
}),
109110
)
110111
.pipe(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export interface ProviderMaintenanceCommandAction {
2424
readonly executable: string;
2525
readonly args: ReadonlyArray<string>;
2626
readonly lockKey: string;
27-
/** Native updaters must use the instance's custom home (e.g. `CODEX_HOME`). */
27+
/** The provider's environment, including its PATH and custom home. */
2828
readonly env?: NodeJS.ProcessEnv;
2929
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ it.layer(
234234
let probes = 0;
235235
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
236236
fixture.install === "native" ? nativePackageToolUpdate : packageToolUpdate,
237-
{ binaryPath, env: { PATH: "" } },
237+
{ binaryPath, env: { PATH: fixture.install === "npm" ? npmBinDir : "" } },
238238
).pipe(
239239
Effect.provideService(HostProcess.Environment, { PATH: npmBinDir }),
240240
Effect.provideService(
@@ -416,7 +416,7 @@ it.layer(
416416
);
417417

418418
it.effect.skipIf(!symlinksSupported)(
419-
"pins npm updates to the global prefix that owns the package",
419+
"pins npm updates to the global prefix that owns the package and the provider's PATH",
420420
() =>
421421
Effect.gen(function* () {
422422
const tempDir = yield* makeTempDir("t3-npm-capabilities");
@@ -426,12 +426,14 @@ it.layer(
426426
"@example",
427427
"package-tool",
428428
]);
429+
// A version manager's Node, which only the provider's settings put on PATH.
430+
const providerEnv = { PATH: NodePath.join(tempDir, "bin") };
429431

430432
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
431433
packageToolUpdate,
432434
{
433435
binaryPath: link,
434-
env: { PATH: "" },
436+
env: providerEnv,
435437
},
436438
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
437439

@@ -450,6 +452,7 @@ it.layer(
450452
"@example/package-tool@latest",
451453
],
452454
lockKey: `npm-global:${normalizeCommandPath(tempDir)}`,
455+
env: providerEnv,
453456
},
454457
});
455458
}),
@@ -922,7 +925,8 @@ it.layer(
922925

923926
it.effect.each([
924927
{
925-
case: "the eligible release",
928+
case: "the eligible release with an inherited PATH",
929+
inheritPath: true,
926930
outdated: { "@example/package-tool": { current: "1.0.0", latest: "1.1.0" } },
927931
exitCode: 1,
928932
npmOutput: '"1.2.0"',
@@ -977,9 +981,11 @@ it.layer(
977981
let npmProbes = 0;
978982
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
979983
packageToolUpdate,
980-
{ binaryPath: link, env: { PATH: "" } },
984+
{ binaryPath: link, env: fixture.inheritPath ? {} : { PATH: pnpmBinDir } },
981985
).pipe(
982-
Effect.provideService(HostProcess.Environment, { PATH: pnpmBinDir }),
986+
Effect.provideService(HostProcess.Environment, {
987+
PATH: fixture.inheritPath ? pnpmBinDir : "",
988+
}),
983989
Effect.provideService(
984990
ChildProcessSpawner.ChildProcessSpawner,
985991
stdoutSpawner(

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,18 @@ const decodePnpmList = Schema.decodeUnknownOption(
424424
// Unlike npm's latest tag, pnpm outdated respects minimumReleaseAge and its excludes.
425425
const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion")(function* (
426426
packageName: string,
427+
env: NodeJS.ProcessEnv,
427428
) {
428-
// Match the updater's pnpm and server environment, rather than the instance's.
429-
const pnpmPath = yield* resolveCommandPath("pnpm");
429+
// Match the updater's pnpm and environment.
430+
const pnpmPath = yield* resolveCommandPath("pnpm", {
431+
env: { ...(yield* HostProcess.Environment), ...env },
432+
});
430433
if (!pnpmPath) return null;
431434
// Silent reporting keeps project package-manager warnings out of the JSON.
432435
const outdated = yield* runProbe(
433436
pnpmPath,
434437
["outdated", "-g", "--reporter=silent", "--format", "json", packageName],
435-
{},
438+
env,
436439
// pnpm outdated exits 1 when updates are available.
437440
[0, 1],
438441
);
@@ -446,7 +449,7 @@ const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion
446449
const listed = yield* runProbe(
447450
pnpmPath,
448451
["ls", "-g", "--reporter=silent", "--depth", "0", "--json", packageName],
449-
{},
452+
env,
450453
);
451454
const listedEntries = listed ? Option.getOrUndefined(decodePnpmList(listed)) : undefined;
452455
return nonEmptyString(listedEntries?.[0]?.dependencies?.[packageName]?.version);
@@ -544,6 +547,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
544547
updateExecutable: "vp",
545548
updateArgs: ["i", "-g", packageName],
546549
updateLockKey: "vite-plus-global",
550+
env: context.env,
547551
});
548552
}
549553
if (commandPaths.some(isBunGlobalCommandPath)) {
@@ -553,6 +557,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
553557
updateExecutable: "bun",
554558
updateArgs: ["i", "-g", `${packageName}@latest`],
555559
updateLockKey: "bun-global",
560+
env: context.env,
556561
});
557562
}
558563
if (commandPaths.some(isPnpmGlobalCommandPath)) {
@@ -569,7 +574,8 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
569574
updateExecutable: "pnpm",
570575
updateArgs: ["add", "-g", ...allowBuild, `${packageName}@latest`],
571576
updateLockKey: "pnpm-global",
572-
lookupLatestVersion: resolvePnpmGlobalLatestVersion(packageName).pipe(
577+
env: context.env,
578+
lookupLatestVersion: resolvePnpmGlobalLatestVersion(packageName, context.env).pipe(
573579
Effect.provideContext(services),
574580
),
575581
});
@@ -582,6 +588,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
582588
updateExecutable: "yarn",
583589
updateArgs: ["global", "add", `${packageName}@latest`],
584590
updateLockKey: "yarn-global",
591+
env: context.env,
585592
});
586593
}
587594
if (yield* isVoltaPackageInstall(context, packageName)) {
@@ -591,6 +598,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
591598
updateExecutable: "volta",
592599
updateArgs: ["install", `${packageName}@latest`],
593600
updateLockKey: "volta",
601+
env: context.env,
594602
});
595603
}
596604

@@ -611,6 +619,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
611619
`${packageName}@latest`,
612620
],
613621
updateLockKey: `npm-global:${normalizeCommandPath(npmPrefix)}`,
622+
env: context.env,
614623
});
615624
}
616625

@@ -781,7 +790,8 @@ const withConfiguredNpmVersionLookup = Effect.fn("withConfiguredNpmVersionLookup
781790
if (!packageName || capabilities.latestVersion !== undefined) {
782791
return capabilities;
783792
}
784-
const npmPath = yield* resolveCommandPath("npm");
793+
const env = { ...(yield* HostProcess.Environment), ...capabilities.update?.env };
794+
const npmPath = yield* resolveCommandPath("npm", { env });
785795
if (!npmPath) return capabilities;
786796
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
787797
const update = capabilities.update;
@@ -799,7 +809,7 @@ const withConfiguredNpmVersionLookup = Effect.fn("withConfiguredNpmVersionLookup
799809
`${packageName}@latest`,
800810
"version",
801811
],
802-
{},
812+
env,
803813
).pipe(
804814
Effect.map((stdout) => {
805815
const value = stdout ? Option.getOrNull(decodeNpmViewVersion(stdout.trim())) : null;

0 commit comments

Comments
 (0)