setup-node/src/cache-save.ts
Satishchoudhary94 dff445bec7 fix(#1357): Gracefully handle missing pnpm installation during cache
This change prevents the action from failing immediately when pnpm is specified
in packageManager but not yet installed (e.g., when using corepack).

Changes:
- Add isPackageManagerInstalled() function to check if a package manager exists
- Update restoreCache to skip caching with a warning if package manager not found
- Update cachePackages to skip cache save with a warning if package manager not found
- This allows workflows to continue instead of failing
- Users can either install pnpm first or disable caching with package-manager-cache: false

Fixes #1357
Related: https://github.com/actions/setup-node/issues/1357
2026-01-18 14:08:11 +00:00

84 lines
2.6 KiB
TypeScript

import * as core from '@actions/core';
import * as cache from '@actions/cache';
import {State} from './constants';
import {getPackageManagerInfo, isPackageManagerInstalled} from './cache-utils';
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on('uncaughtException', e => {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${e.message}`);
});
// Added early exit to resolve issue with slow post action step:
export async function run(earlyExit?: boolean) {
try {
const cacheLock = core.getState(State.CachePackageManager);
if (cacheLock) {
await cachePackages(cacheLock);
if (earlyExit) {
process.exit(0);
}
} else {
core.debug(`Caching for '${cacheLock}' is not supported`);
}
} catch (error) {
core.setFailed((error as Error).message);
}
}
const cachePackages = async (packageManager: string) => {
const state = core.getState(State.CacheMatchedKey);
const primaryKey = core.getState(State.CachePrimaryKey);
const cachePaths = JSON.parse(
core.getState(State.CachePaths) || '[]'
) as string[];
const packageManagerInfo = await getPackageManagerInfo(packageManager);
if (!packageManagerInfo) {
core.debug(`Caching for '${packageManager}' is not supported`);
return;
}
// Check if the package manager is installed before attempting to save cache
// This prevents cache save failures for package managers that may not be installed
const isInstalled = await isPackageManagerInstalled(packageManager);
if (!isInstalled) {
core.warning(
`Package manager '${packageManager}' was not found in the PATH. ` +
`Skipping cache save.`
);
return;
}
if (!cachePaths.length) {
// TODO: core.getInput has a bug - it can return undefined despite its definition (tests only?)
// export declare function getInput(name: string, options?: InputOptions): string;
const cacheDependencyPath = core.getInput('cache-dependency-path') || '';
throw new Error(
`Cache folder paths are not retrieved for ${packageManager} with cache-dependency-path = ${cacheDependencyPath}`
);
}
if (primaryKey === state) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
}
const cacheId = await cache.saveCache(cachePaths, primaryKey);
if (cacheId == -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
};
run(true);