Skip to content

Commit 10b4c2e

Browse files
committed
fix: only tolerate a devEngines.packageManager mismatch in the npm cache lookup
`--force` downgrades every devEngines failure to a warning, not just the packageManager one, so the retried `npm config get cache --force` is only accepted when its EBADDEVENGINES warnings name no other engine. A runtime, os, cpu or libc mismatch keeps failing with npm's original error, exactly as without the retry. The packageManager requirement is the only one a later workflow step can satisfy, which is what actions#1553 is about. Verified against npm 10.9.9 and 11.19.1: packageManager-only mismatches succeed (and still honour a project .npmrc cache setting), runtime mismatches alone or combined with packageManager still fail. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_015TYAsofxj8KwHen4yw6uvj
1 parent 0e0d34e commit 10b4c2e

4 files changed

Lines changed: 202 additions & 25 deletions

File tree

‎__tests__/cache-utils.test.ts‎

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,28 @@ describe('cache-utils', () => {
159159
});
160160

161161
describe('npm getCacheFolderPath', () => {
162-
const devEnginesError = [
162+
const packageManagerError = [
163163
'npm error code EBADDEVENGINES',
164-
'npm error EBADDEVENGINES Invalid name "pnpm" does not match "npm" for "packageManager"'
164+
'npm error EBADDEVENGINES The developer of this package has specified the following through devEngines',
165+
'npm error EBADDEVENGINES Invalid devEngines.packageManager',
166+
'npm error EBADDEVENGINES Invalid semver version ">=12.2.2" does not match "11.19.1" for "packageManager"'
167+
].join('\n');
168+
const runtimeError = [
169+
'npm error code EBADDEVENGINES',
170+
'npm error EBADDEVENGINES The developer of this package has specified the following through devEngines',
171+
'npm error EBADDEVENGINES Invalid devEngines.runtime',
172+
'npm error EBADDEVENGINES Invalid semver version ">=30" does not match "24.13.0" for "runtime"'
173+
].join('\n');
174+
const forceWarning =
175+
'npm warn using --force Recommended protections disabled.';
176+
const packageManagerWarning = [
177+
'npm warn EBADDEVENGINES The developer of this package has specified the following through devEngines',
178+
'npm warn EBADDEVENGINES Invalid devEngines.packageManager',
179+
'npm warn EBADDEVENGINES Invalid semver version ">=12.2.2" does not match "11.19.1" for "packageManager"'
180+
].join('\n');
181+
const runtimeWarning = [
182+
'npm warn EBADDEVENGINES Invalid devEngines.runtime',
183+
'npm warn EBADDEVENGINES Invalid semver version ">=30" does not match "24.13.0" for "runtime"'
165184
].join('\n');
166185

167186
function mockExecResultOnce(result: {
@@ -190,9 +209,13 @@ describe('cache-utils', () => {
190209
);
191210
});
192211

193-
it('retries with --force when npm rejects devEngines', async () => {
194-
mockExecResultOnce({stderr: devEnginesError, exitCode: 1});
195-
mockExecResultOnce({stdout: '/home/runner/.npm', exitCode: 0});
212+
it('retries with --force when only devEngines.packageManager is unsatisfied', async () => {
213+
mockExecResultOnce({stderr: packageManagerError, exitCode: 1});
214+
mockExecResultOnce({
215+
stdout: '/home/runner/.npm\n',
216+
stderr: [forceWarning, packageManagerWarning].join('\n'),
217+
exitCode: 0
218+
});
196219

197220
await expect(
198221
supportedPackageManagers.npm.getCacheFolderPath()
@@ -202,13 +225,71 @@ describe('cache-utils', () => {
202225
2,
203226
'npm config get cache --force',
204227
undefined,
205-
expect.anything()
228+
expect.objectContaining({ignoreReturnCode: true})
206229
);
207230
expect(info).toHaveBeenCalledWith(
208231
expect.stringContaining(
209232
'retrying the npm cache path lookup with --force'
210233
)
211234
);
235+
expect(info).toHaveBeenCalledWith(
236+
expect.stringContaining('Only devEngines.packageManager is unsatisfied')
237+
);
238+
});
239+
240+
it('keeps the npm error when devEngines.runtime is unsatisfied as well', async () => {
241+
mockExecResultOnce({stderr: packageManagerError, exitCode: 1});
242+
mockExecResultOnce({
243+
stdout: '/home/runner/.npm\n',
244+
stderr: [forceWarning, packageManagerWarning, runtimeWarning].join(
245+
'\n'
246+
),
247+
exitCode: 0
248+
});
249+
250+
await expect(
251+
supportedPackageManagers.npm.getCacheFolderPath()
252+
).rejects.toThrow(packageManagerError);
253+
expect(getExecOutputSpy).toHaveBeenCalledTimes(2);
254+
expect(info).toHaveBeenCalledWith(
255+
'devEngines.runtime is not satisfied either, keeping the npm error'
256+
);
257+
});
258+
259+
it('keeps the npm error when devEngines.runtime is the only unsatisfied engine', async () => {
260+
mockExecResultOnce({stderr: runtimeError, exitCode: 1});
261+
mockExecResultOnce({
262+
stdout: '/home/runner/.npm\n',
263+
stderr: [forceWarning, runtimeWarning].join('\n'),
264+
exitCode: 0
265+
});
266+
267+
await expect(
268+
supportedPackageManagers.npm.getCacheFolderPath()
269+
).rejects.toThrow(runtimeError);
270+
expect(getExecOutputSpy).toHaveBeenCalledTimes(2);
271+
});
272+
273+
it('keeps the npm error when os, cpu or libc are unsatisfied as well', async () => {
274+
mockExecResultOnce({stderr: packageManagerError, exitCode: 1});
275+
mockExecResultOnce({
276+
stdout: '/home/runner/.npm\n',
277+
stderr: [
278+
forceWarning,
279+
packageManagerWarning,
280+
'npm warn EBADDEVENGINES Invalid devEngines.os',
281+
'npm warn EBADDEVENGINES Invalid devEngines.cpu',
282+
'npm warn EBADDEVENGINES Invalid devEngines.libc'
283+
].join('\n'),
284+
exitCode: 0
285+
});
286+
287+
await expect(
288+
supportedPackageManagers.npm.getCacheFolderPath()
289+
).rejects.toThrow(packageManagerError);
290+
expect(info).toHaveBeenCalledWith(
291+
'devEngines.os and devEngines.cpu and devEngines.libc are not satisfied either, keeping the npm error'
292+
);
212293
});
213294

214295
it('does not retry on other npm errors', async () => {
@@ -221,14 +302,23 @@ describe('cache-utils', () => {
221302
});
222303

223304
it('surfaces the error when the --force retry also fails', async () => {
224-
mockExecResultOnce({stderr: devEnginesError, exitCode: 1});
305+
mockExecResultOnce({stderr: packageManagerError, exitCode: 1});
225306
mockExecResultOnce({stderr: 'npm error code EFORCEFAILED', exitCode: 1});
226307

227308
await expect(
228309
supportedPackageManagers.npm.getCacheFolderPath()
229310
).rejects.toThrow('npm error code EFORCEFAILED');
230311
expect(getExecOutputSpy).toHaveBeenCalledTimes(2);
231312
});
313+
314+
it('fails when the --force retry prints no cache path', async () => {
315+
mockExecResultOnce({stderr: packageManagerError, exitCode: 1});
316+
mockExecResultOnce({stdout: '\n', stderr: forceWarning, exitCode: 0});
317+
318+
await expect(
319+
supportedPackageManagers.npm.getCacheFolderPath()
320+
).rejects.toThrow('Could not get npm cache folder path');
321+
});
232322
});
233323

234324
it('isCacheFeatureAvailable for GHES is false', () => {

‎dist/cache-save/index.js‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92268,21 +92268,44 @@ const getCommandOutputNotEmpty = async (toolCommand, error, cwd) => {
9226892268
}
9226992269
return stdOut;
9227092270
};
92271+
// One line per unsatisfied engine, e.g. `npm warn EBADDEVENGINES Invalid devEngines.runtime`
92272+
const devEnginesFailurePattern = /EBADDEVENGINES Invalid devEngines\.(\w+)/g;
9227192273
const getNpmCacheFolderPath = async () => {
9227292274
const error = 'Could not get npm cache folder path';
9227392275
try {
9227492276
return await getCommandOutputNotEmpty('npm config get cache', error);
9227592277
}
9227692278
catch (err) {
92277-
// npm checks devEngines.packageManager from the package.json in the
92278-
// workspace even for `npm config get`, so the cache lookup fails before
92279-
// the workflow had a chance to install the required package manager.
92280-
// `--force` turns that check into a warning and still honours .npmrc.
9228192279
if (!err.message.includes('EBADDEVENGINES')) {
9228292280
throw err;
9228392281
}
92284-
info("The installed npm doesn't satisfy devEngines.packageManager in package.json, retrying the npm cache path lookup with --force");
92285-
return getCommandOutputNotEmpty('npm config get cache --force', error);
92282+
// npm checks devEngines from the package.json in the workspace even for
92283+
// the read-only `npm config get`, so the cache lookup fails before the
92284+
// workflow had a chance to install the package manager it declares.
92285+
// Only a devEngines.packageManager mismatch is tolerated here, because it
92286+
// is the one requirement a later workflow step can satisfy. `--force`
92287+
// downgrades every devEngines failure to a warning, so the warnings of the
92288+
// retried command are inspected and any other engine (runtime, os, cpu,
92289+
// libc) keeps failing exactly as it does without the retry.
92290+
info('npm rejected the devEngines in package.json, retrying the npm cache path lookup with --force to check whether only devEngines.packageManager is unsatisfied');
92291+
const forcedCommand = 'npm config get cache --force';
92292+
const { stdout, stderr, exitCode } = await getExecOutput(forcedCommand, undefined, { ignoreReturnCode: true });
92293+
const otherEngines = [
92294+
...new Set(Array.from(stderr.matchAll(devEnginesFailurePattern), m => m[1]))
92295+
].filter(engine => engine !== 'packageManager');
92296+
if (otherEngines.length) {
92297+
info(`devEngines.${otherEngines.join(' and devEngines.')} ${otherEngines.length === 1 ? 'is' : 'are'} not satisfied either, keeping the npm error`);
92298+
throw err;
92299+
}
92300+
if (exitCode) {
92301+
throw new Error(stderr.trim() ||
92302+
`The '${forcedCommand}' command failed with exit code: ${exitCode}`, { cause: err });
92303+
}
92304+
if (!stdout.trim()) {
92305+
throw new Error(error, { cause: err });
92306+
}
92307+
info('Only devEngines.packageManager is unsatisfied, continuing with the npm cache path; later npm commands still enforce devEngines');
92308+
return stdout.trim();
9228692309
}
9228792310
};
9228892311
const getPackageManagerInfo = async (packageManager) => {

‎dist/setup/index.js‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97662,21 +97662,44 @@ const getCommandOutputNotEmpty = async (toolCommand, error, cwd) => {
9766297662
}
9766397663
return stdOut;
9766497664
};
97665+
// One line per unsatisfied engine, e.g. `npm warn EBADDEVENGINES Invalid devEngines.runtime`
97666+
const devEnginesFailurePattern = /EBADDEVENGINES Invalid devEngines\.(\w+)/g;
9766597667
const getNpmCacheFolderPath = async () => {
9766697668
const error = 'Could not get npm cache folder path';
9766797669
try {
9766897670
return await getCommandOutputNotEmpty('npm config get cache', error);
9766997671
}
9767097672
catch (err) {
97671-
// npm checks devEngines.packageManager from the package.json in the
97672-
// workspace even for `npm config get`, so the cache lookup fails before
97673-
// the workflow had a chance to install the required package manager.
97674-
// `--force` turns that check into a warning and still honours .npmrc.
9767597673
if (!err.message.includes('EBADDEVENGINES')) {
9767697674
throw err;
9767797675
}
97678-
core_info("The installed npm doesn't satisfy devEngines.packageManager in package.json, retrying the npm cache path lookup with --force");
97679-
return getCommandOutputNotEmpty('npm config get cache --force', error);
97676+
// npm checks devEngines from the package.json in the workspace even for
97677+
// the read-only `npm config get`, so the cache lookup fails before the
97678+
// workflow had a chance to install the package manager it declares.
97679+
// Only a devEngines.packageManager mismatch is tolerated here, because it
97680+
// is the one requirement a later workflow step can satisfy. `--force`
97681+
// downgrades every devEngines failure to a warning, so the warnings of the
97682+
// retried command are inspected and any other engine (runtime, os, cpu,
97683+
// libc) keeps failing exactly as it does without the retry.
97684+
core_info('npm rejected the devEngines in package.json, retrying the npm cache path lookup with --force to check whether only devEngines.packageManager is unsatisfied');
97685+
const forcedCommand = 'npm config get cache --force';
97686+
const { stdout, stderr, exitCode } = await getExecOutput(forcedCommand, undefined, { ignoreReturnCode: true });
97687+
const otherEngines = [
97688+
...new Set(Array.from(stderr.matchAll(devEnginesFailurePattern), m => m[1]))
97689+
].filter(engine => engine !== 'packageManager');
97690+
if (otherEngines.length) {
97691+
core_info(`devEngines.${otherEngines.join(' and devEngines.')} ${otherEngines.length === 1 ? 'is' : 'are'} not satisfied either, keeping the npm error`);
97692+
throw err;
97693+
}
97694+
if (exitCode) {
97695+
throw new Error(stderr.trim() ||
97696+
`The '${forcedCommand}' command failed with exit code: ${exitCode}`, { cause: err });
97697+
}
97698+
if (!stdout.trim()) {
97699+
throw new Error(error, { cause: err });
97700+
}
97701+
core_info('Only devEngines.packageManager is unsatisfied, continuing with the npm cache path; later npm commands still enforce devEngines');
97702+
return stdout.trim();
9768097703
}
9768197704
};
9768297705
const getPackageManagerInfo = async (packageManager) => {

‎src/cache-utils.ts‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,63 @@ export const getCommandOutputNotEmpty = async (
9494
return stdOut;
9595
};
9696

97+
// One line per unsatisfied engine, e.g. `npm warn EBADDEVENGINES Invalid devEngines.runtime`
98+
const devEnginesFailurePattern = /EBADDEVENGINES Invalid devEngines\.(\w+)/g;
99+
97100
const getNpmCacheFolderPath = async (): Promise<string> => {
98101
const error = 'Could not get npm cache folder path';
99102
try {
100103
return await getCommandOutputNotEmpty('npm config get cache', error);
101104
} catch (err) {
102-
// npm checks devEngines.packageManager from the package.json in the
103-
// workspace even for `npm config get`, so the cache lookup fails before
104-
// the workflow had a chance to install the required package manager.
105-
// `--force` turns that check into a warning and still honours .npmrc.
106105
if (!(err as Error).message.includes('EBADDEVENGINES')) {
107106
throw err;
108107
}
108+
// npm checks devEngines from the package.json in the workspace even for
109+
// the read-only `npm config get`, so the cache lookup fails before the
110+
// workflow had a chance to install the package manager it declares.
111+
// Only a devEngines.packageManager mismatch is tolerated here, because it
112+
// is the one requirement a later workflow step can satisfy. `--force`
113+
// downgrades every devEngines failure to a warning, so the warnings of the
114+
// retried command are inspected and any other engine (runtime, os, cpu,
115+
// libc) keeps failing exactly as it does without the retry.
116+
core.info(
117+
'npm rejected the devEngines in package.json, retrying the npm cache path lookup with --force to check whether only devEngines.packageManager is unsatisfied'
118+
);
119+
const forcedCommand = 'npm config get cache --force';
120+
const {stdout, stderr, exitCode} = await exec.getExecOutput(
121+
forcedCommand,
122+
undefined,
123+
{ignoreReturnCode: true}
124+
);
125+
126+
const otherEngines = [
127+
...new Set(
128+
Array.from(stderr.matchAll(devEnginesFailurePattern), m => m[1])
129+
)
130+
].filter(engine => engine !== 'packageManager');
131+
if (otherEngines.length) {
132+
core.info(
133+
`devEngines.${otherEngines.join(
134+
' and devEngines.'
135+
)} ${otherEngines.length === 1 ? 'is' : 'are'} not satisfied either, keeping the npm error`
136+
);
137+
throw err;
138+
}
139+
140+
if (exitCode) {
141+
throw new Error(
142+
stderr.trim() ||
143+
`The '${forcedCommand}' command failed with exit code: ${exitCode}`,
144+
{cause: err}
145+
);
146+
}
147+
if (!stdout.trim()) {
148+
throw new Error(error, {cause: err});
149+
}
109150
core.info(
110-
"The installed npm doesn't satisfy devEngines.packageManager in package.json, retrying the npm cache path lookup with --force"
151+
'Only devEngines.packageManager is unsatisfied, continuing with the npm cache path; later npm commands still enforce devEngines'
111152
);
112-
return getCommandOutputNotEmpty('npm config get cache --force', error);
153+
return stdout.trim();
113154
}
114155
};
115156

0 commit comments

Comments
 (0)