Skip to content

Commit 290182d

Browse files
fix(provider-core): Vite+ provider updates respect npm's release-age policy (#32340)
1 parent 7e1a058 commit 290182d

2 files changed

Lines changed: 129 additions & 10 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,74 @@ it.layer(
429429
{ skip: !symlinksSupported || windowsHost },
430430
);
431431

432+
it.effect.skipIf(windowsHost)(
433+
"does not offer a Vite+ update npm's release-age policy will not install",
434+
() =>
435+
Effect.gen(function* () {
436+
const tempDir = yield* makeTempDir("t3-release-age");
437+
const npmBinDir = NodePath.join(tempDir, "npm-bin");
438+
const npmPath = NodePath.join(npmBinDir, "npm");
439+
writeExecutable(npmPath);
440+
const vitePlusNodePath = NodePath.join(tempDir, "vite-plus-node/bin/node");
441+
const binaryPath = NodePath.join(tempDir, ".vite-plus/bin/package-tool");
442+
writeExecutable(binaryPath);
443+
const advise = (env: NodeJS.ProcessEnv, vitePlusPacks: string, nodeSource = "lts") =>
444+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
445+
binaryPath,
446+
env: { PATH: npmBinDir, ...env },
447+
}).pipe(
448+
Effect.provideService(
449+
ChildProcessSpawner.ChildProcessSpawner,
450+
// `vp i -g` runs the npm beside Vite+'s Node; `npm view` ignores min-release-age.
451+
stdoutSpawner((command, args) => {
452+
if (command === "vp") {
453+
return JSON.stringify({
454+
node: { source: nodeSource, bin_path: vitePlusNodePath, installed: true },
455+
});
456+
}
457+
if (command === npmPath) {
458+
return args[0] === "view" ? '"1.2.0"' : "example-package-tool-1.2.0.tgz\n";
459+
}
460+
return command === NodePath.join(NodePath.dirname(vitePlusNodePath), "npm") &&
461+
args[0] === "pack"
462+
? `example-package-tool-${vitePlusPacks}.tgz\n`
463+
: "";
464+
}),
465+
),
466+
Effect.flatMap((capabilities) =>
467+
enrichProviderSnapshotWithVersionAdvisory(
468+
{ ...installedPackageToolProvider, version: "1.1.0" },
469+
capabilities,
470+
),
471+
),
472+
Effect.map((snapshot) => snapshot.versionAdvisory),
473+
);
474+
475+
expect(yield* advise({ npm_config_min_release_age: "7" }, "1.1.0")).toMatchObject({
476+
status: "current",
477+
currentVersion: "1.1.0",
478+
latestVersion: "1.1.0",
479+
});
480+
expect((yield* advise({}, "1.2.0"))?.latestVersion).toBe("1.2.0");
481+
// Install never uses the system Node `vp env off` reports, so this falls back to `npm view`.
482+
expect(
483+
(yield* advise(
484+
{ npm_config_min_release_age: "7", VP_HOME: "system-first" },
485+
"1.1.0",
486+
"system PATH",
487+
))?.latestVersion,
488+
).toBe("1.2.0");
489+
}).pipe(
490+
Effect.provide(ProviderLatestVersions.layer),
491+
Effect.provideService(
492+
HttpClient.HttpClient,
493+
HttpClient.make(() =>
494+
Effect.die("a configured npm lookup must not fall back to public npm"),
495+
),
496+
),
497+
),
498+
);
499+
432500
it.effect.skipIf(!symlinksSupported || windowsHost)(
433501
"keeps configured npm registries' latest versions apart for one install",
434502
() =>

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

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,49 @@ const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion
482482
return nonEmptyString(listedEntries?.[0]?.dependencies?.[packageName]?.version);
483483
});
484484

485+
// npm names the tarball `${name}-${version}.tgz`, dropping a scope's `@` and its `/`.
486+
function parseNpmPackVersion(stdout: string, packageName: string): string | null {
487+
const prefix = `${packageName.replace(/^@/, "").replace("/", "-")}-`;
488+
const filename = stdout.trim();
489+
if (!filename.startsWith(prefix) || !filename.endsWith(".tgz")) return null;
490+
const version = filename.slice(prefix.length, -".tgz".length);
491+
return version && !/\s/.test(version) ? version : null;
492+
}
493+
494+
const decodeVitePlusCurrentEnv = Schema.decodeUnknownOption(
495+
Schema.fromJsonString(
496+
Schema.Struct({
497+
node: Schema.Struct({
498+
source: Schema.String,
499+
bin_path: Schema.String,
500+
installed: Schema.Boolean,
501+
}),
502+
}),
503+
),
504+
);
505+
506+
// `vp i -g` installs through the npm beside Vite+'s managed Node, even under `vp env off`, and
507+
// `vp env current` names that Node unless it reports a system one, which install does not use.
508+
// Pack honours npm's min-release-age and `before`, which `npm view` ignores, and resolves
509+
// `@latest` the way install does. `--json=false` keeps its output to the tarball name.
510+
const resolveVitePlusInstallableLatestVersion = Effect.fn(
511+
"resolveVitePlusInstallableLatestVersion",
512+
)(function* (packageName: string, env: NodeJS.ProcessEnv, platform: NodeJS.Platform) {
513+
const path = yield* Path.Path;
514+
const current = yield* runProbe("vp", ["env", "current", "--json"], env);
515+
const node = current ? Option.getOrUndefined(decodeVitePlusCurrentEnv(current))?.node : undefined;
516+
if (!node?.installed || node.source === "system PATH" || !path.isAbsolute(node.bin_path)) {
517+
return null;
518+
}
519+
const npmPath = path.join(path.dirname(node.bin_path), platform === "win32" ? "npm.cmd" : "npm");
520+
const packed = yield* runProbe(
521+
npmPath,
522+
["pack", "--dry-run", "--json=false", "--global", `${packageName}@latest`],
523+
env,
524+
);
525+
return packed ? parseNpmPackVersion(packed, packageName) : null;
526+
});
527+
485528
// `bun outdated` has no JSON output; its `Latest` column is what `@latest` installs
486529
// under minimumReleaseAge (marked `*`). `Update` is only the global package.json range.
487530
function parseBunOutdatedLatestVersion(output: string, packageName: string): string | null {
@@ -613,14 +656,6 @@ export const resolveMiseProviderMaintenance = Effect.fn("resolveMiseProviderMain
613656
}
614657
const args = ["upgrade", miseTool];
615658
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
616-
// Mise reads its settings from this environment, so each one gets its own cached answer.
617-
const envHash = NodeCrypto.createHash("sha256");
618-
for (const [name, value] of Object.entries(context.env).toSorted(([a], [b]) =>
619-
a < b ? -1 : 1,
620-
)) {
621-
envHash.update(`${name}\0${value ?? ""}\0`);
622-
}
623-
const envKey = envHash.digest("hex");
624659
const capabilities = makeProviderMaintenanceCapabilities({
625660
...owner,
626661
updateExecutable: misePath,
@@ -643,7 +678,9 @@ export const resolveMiseProviderMaintenance = Effect.fn("resolveMiseProviderMain
643678
});
644679
return {
645680
...capabilities,
646-
latestVersionCacheKey: ["mise", misePath, miseTool, envKey].join("\0"),
681+
latestVersionCacheKey: ["mise", misePath, miseTool, environmentCacheKey(context.env)].join(
682+
"\0",
683+
),
647684
};
648685
},
649686
);
@@ -797,14 +834,28 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
797834
? manual
798835
: native;
799836
if (commandPaths.some(isVitePlusGlobalCommandPath)) {
800-
return makeProviderMaintenanceCapabilities({
837+
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
838+
const capabilities = makeProviderMaintenanceCapabilities({
801839
provider: definition.provider,
802840
packageName,
803841
updateExecutable: "vp",
804842
updateArgs: ["i", "-g", packageName],
805843
updateLockKey: "vite-plus-global",
806844
env: context.env,
845+
lookupLatestVersion: resolveVitePlusInstallableLatestVersion(
846+
packageName,
847+
context.env,
848+
context.platform,
849+
).pipe(Effect.provideContext(services)),
807850
});
851+
return {
852+
...capabilities,
853+
latestVersionCacheKey: [
854+
"vite-plus-global",
855+
packageName,
856+
environmentCacheKey(context.env),
857+
].join("\0"),
858+
};
808859
}
809860
if (commandPaths.some(isBunGlobalCommandPath)) {
810861
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();

0 commit comments

Comments
 (0)