Skip to content

Commit 9c20c1a

Browse files
fix(server): Vite+ update checks ask Vite+'s own npm, per environment
`vp i -g` installs through the npm beside the Node that Vite+ resolves, not whichever npm comes first on PATH, and the two can disagree on min-release-age. The probe now asks `vp env exec node -p process.execPath` and runs the npm next to it. Its answer is cached per provider environment, using the env hash the mise lookup already used. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1bcd452 commit 9c20c1a

2 files changed

Lines changed: 81 additions & 66 deletions

File tree

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

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -429,53 +429,52 @@ it.layer(
429429
const npmBinDir = NodePath.join(tempDir, "npm-bin");
430430
const npmPath = NodePath.join(npmBinDir, "npm");
431431
writeExecutable(npmPath);
432+
const vitePlusNodePath = NodePath.join(tempDir, "vite-plus-node/bin/node");
432433
const binaryPath = NodePath.join(tempDir, ".vite-plus/bin/package-tool");
433434
writeExecutable(binaryPath);
434-
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
435-
packageToolUpdate,
436-
{
435+
const advise = (env: NodeJS.ProcessEnv, vitePlusPacks: string) =>
436+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
437437
binaryPath,
438-
env: { PATH: npmBinDir },
439-
},
440-
).pipe(
441-
Effect.provideService(HostProcess.Environment, { PATH: npmBinDir }),
442-
Effect.provideService(
443-
ChildProcessSpawner.ChildProcessSpawner,
444-
// `npm view` ignores min-release-age; install-time resolution honours it.
445-
stdoutSpawner(
446-
(command, args) => {
447-
if (command !== npmPath) return "";
448-
if (args[0] === "view") return '"1.2.0"';
449-
return args[0] === "pack" && args.includes("--dry-run")
450-
? "example-package-tool-1.1.0.tgz\n"
438+
env: { PATH: npmBinDir, ...env },
439+
}).pipe(
440+
Effect.provideService(
441+
ChildProcessSpawner.ChildProcessSpawner,
442+
// `vp i -g` runs the npm beside Vite+'s Node; `npm view` ignores min-release-age.
443+
stdoutSpawner((command, args) => {
444+
if (command === "vp") return `${vitePlusNodePath}\n`;
445+
if (command === npmPath) {
446+
return args[0] === "view" ? '"1.2.0"' : "example-package-tool-1.2.0.tgz\n";
447+
}
448+
return command === NodePath.join(NodePath.dirname(vitePlusNodePath), "npm") &&
449+
args[0] === "pack"
450+
? `example-package-tool-${vitePlusPacks}.tgz\n`
451451
: "";
452-
},
453-
(args) => (["view", "pack"].includes(args[0] ?? "") ? 0 : 1),
452+
}),
454453
),
455-
),
456-
);
457-
458-
const provider = yield* enrichProviderSnapshotWithVersionAdvisory(
459-
{ ...installedPackageToolProvider, version: "1.1.0" },
460-
capabilities,
461-
).pipe(
462-
Effect.provideServiceEffect(
463-
ProviderLatestVersions.ProviderLatestVersions,
464-
ProviderLatestVersions.make([]),
465-
),
466-
Effect.provideService(
467-
HttpClient.HttpClient,
468-
HttpClient.make(() =>
469-
Effect.die("a configured npm lookup must not fall back to public npm"),
454+
Effect.flatMap((capabilities) =>
455+
enrichProviderSnapshotWithVersionAdvisory(
456+
{ ...installedPackageToolProvider, version: "1.1.0" },
457+
capabilities,
458+
),
470459
),
471-
),
472-
);
473-
expect(provider.versionAdvisory).toMatchObject({
460+
Effect.map((snapshot) => snapshot.versionAdvisory),
461+
);
462+
463+
expect(yield* advise({ npm_config_min_release_age: "7" }, "1.1.0")).toMatchObject({
474464
status: "current",
475465
currentVersion: "1.1.0",
476466
latestVersion: "1.1.0",
477467
});
478-
}),
468+
expect((yield* advise({}, "1.2.0"))?.latestVersion).toBe("1.2.0");
469+
}).pipe(
470+
Effect.provide(ProviderLatestVersions.layer),
471+
Effect.provideService(
472+
HttpClient.HttpClient,
473+
HttpClient.make(() =>
474+
Effect.die("a configured npm lookup must not fall back to public npm"),
475+
),
476+
),
477+
),
479478
{ skip: windowsHost },
480479
);
481480

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

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,34 @@ function parseNpmPackVersion(stdout: string, packageName: string): string | null
486486
return version && !/\s/.test(version) ? version : null;
487487
}
488488

489-
// `vp i -g` installs through npm, whose min-release-age and `before` `npm view` ignores;
490-
// pack resolves `@latest` the way install does. `--json=false` keeps its output to the tarball name.
491-
const resolveNpmInstallableLatestVersion = Effect.fn("resolveNpmInstallableLatestVersion")(
492-
function* (packageName: string, env: NodeJS.ProcessEnv) {
493-
const npmPath = yield* resolveCommandPath("npm", {
494-
env: { ...(yield* HostProcess.Environment), ...env },
495-
});
496-
if (!npmPath) return null;
497-
const packed = yield* runProbe(
498-
npmPath,
499-
["pack", "--dry-run", "--json=false", "--global", `${packageName}@latest`],
500-
env,
501-
);
502-
return packed ? parseNpmPackVersion(packed, packageName) : null;
503-
},
504-
);
489+
// `vp i -g` installs through the npm beside Vite+'s Node, which can differ from PATH's npm.
490+
// Pack honours npm's min-release-age and `before`, which `npm view` ignores, and resolves
491+
// `@latest` the way install does. `--json=false` keeps its output to the tarball name.
492+
const resolveVitePlusInstallableLatestVersion = Effect.fn(
493+
"resolveVitePlusInstallableLatestVersion",
494+
)(function* (packageName: string, env: NodeJS.ProcessEnv, platform: NodeJS.Platform) {
495+
const path = yield* Path.Path;
496+
const nodePath = nonEmptyString(
497+
yield* runProbe("vp", ["env", "exec", "node", "-p", "process.execPath"], env),
498+
);
499+
if (!nodePath || !path.isAbsolute(nodePath)) return null;
500+
const npmPath = path.join(path.dirname(nodePath), platform === "win32" ? "npm.cmd" : "npm");
501+
const packed = yield* runProbe(
502+
npmPath,
503+
["pack", "--dry-run", "--json=false", "--global", `${packageName}@latest`],
504+
env,
505+
);
506+
return packed ? parseNpmPackVersion(packed, packageName) : null;
507+
});
508+
509+
/** Installers read settings from their environment, so each one gets its own cached answer. */
510+
function environmentCacheKey(env: NodeJS.ProcessEnv): string {
511+
const hash = NodeCrypto.createHash("sha256");
512+
for (const [name, value] of Object.entries(env).toSorted(([a], [b]) => (a < b ? -1 : 1))) {
513+
hash.update(`${name}\0${value ?? ""}\0`);
514+
}
515+
return hash.digest("hex");
516+
}
505517

506518
const MiseInstall = Schema.Struct({
507519
install_path: Schema.String,
@@ -603,14 +615,6 @@ export const resolveMiseProviderMaintenance = Effect.fn("resolveMiseProviderMain
603615
}
604616
const args = ["upgrade", miseTool];
605617
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
606-
// Mise reads its settings from this environment, so each one gets its own cached answer.
607-
const envHash = NodeCrypto.createHash("sha256");
608-
for (const [name, value] of Object.entries(context.env).toSorted(([a], [b]) =>
609-
a < b ? -1 : 1,
610-
)) {
611-
envHash.update(`${name}\0${value ?? ""}\0`);
612-
}
613-
const envKey = envHash.digest("hex");
614618
const capabilities = makeProviderMaintenanceCapabilities({
615619
...owner,
616620
updateExecutable: misePath,
@@ -633,7 +637,9 @@ export const resolveMiseProviderMaintenance = Effect.fn("resolveMiseProviderMain
633637
});
634638
return {
635639
...capabilities,
636-
latestVersionCacheKey: ["mise", misePath, miseTool, envKey].join("\0"),
640+
latestVersionCacheKey: ["mise", misePath, miseTool, environmentCacheKey(context.env)].join(
641+
"\0",
642+
),
637643
};
638644
},
639645
);
@@ -731,17 +737,27 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
731737
: native;
732738
if (commandPaths.some(isVitePlusGlobalCommandPath)) {
733739
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
734-
return makeProviderMaintenanceCapabilities({
740+
const capabilities = makeProviderMaintenanceCapabilities({
735741
provider: definition.provider,
736742
packageName,
737743
updateExecutable: "vp",
738744
updateArgs: ["i", "-g", packageName],
739745
updateLockKey: "vite-plus-global",
740746
env: context.env,
741-
lookupLatestVersion: resolveNpmInstallableLatestVersion(packageName, context.env).pipe(
742-
Effect.provideContext(services),
743-
),
747+
lookupLatestVersion: resolveVitePlusInstallableLatestVersion(
748+
packageName,
749+
context.env,
750+
context.platform,
751+
).pipe(Effect.provideContext(services)),
744752
});
753+
return {
754+
...capabilities,
755+
latestVersionCacheKey: [
756+
"vite-plus-global",
757+
packageName,
758+
environmentCacheKey(context.env),
759+
].join("\0"),
760+
};
745761
}
746762
if (commandPaths.some(isBunGlobalCommandPath)) {
747763
return makeProviderMaintenanceCapabilities({

0 commit comments

Comments
 (0)