Skip to content

Commit d872eeb

Browse files
MeAkibalan-agius4
authored andcommitted
fix(@angular/cli): handle modern yarn registry error output and 404 detection
Modern yarn (v2+) reports registry errors as JSON lines on `stdout`, one per message line, such as `{"type":"error","displayName":"YN0035","data":"Package not found"}` followed by a `Response Code: 404 (Not Found)` line. Because `parseNpmLikeError` did not recognize this format, querying a package that does not exist under modern yarn threw the original `Process exited with code 1.` error instead of returning `null`. (cherry picked from commit a9aaff5)
1 parent 67ea148 commit d872eeb

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

‎packages/angular/cli/src/package-managers/package-manager-descriptor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
parseYarnClassicManifest,
3131
parseYarnClassicMetadata,
3232
parseYarnModernDependencies,
33+
parseYarnModernError,
3334
parseYarnReleaseAge,
3435
} from './parsers';
3536

@@ -231,7 +232,7 @@ export const SUPPORTED_PACKAGE_MANAGERS = {
231232
listDependencies: parseYarnModernDependencies,
232233
getRegistryManifest: parseNpmLikeManifest,
233234
getRegistryMetadata: parseNpmLikeMetadata,
234-
getError: parseNpmLikeError,
235+
getError: parseYarnModernError,
235236
getReleaseAge: parseYarnReleaseAge,
236237
},
237238
isNotFound: isKnownNotFound,

‎packages/angular/cli/src/package-managers/package-manager_spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,23 @@ describe('PackageManager', () => {
422422

423423
expect(await pm.getRegistryMetadata('non-existent')).toBeNull();
424424
});
425+
426+
it('should return null when yarn npm info fails with a missing package', async () => {
427+
const pm = new PackageManager(host, '/tmp', SUPPORTED_PACKAGE_MANAGERS['yarn']);
428+
runCommandSpy.and.rejectWith(
429+
new PackageManagerError(
430+
'Process exited with code 1.',
431+
[
432+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":"Package not found"}',
433+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":" Response Code: 404 (Not Found)"}',
434+
].join('\n'),
435+
'',
436+
1,
437+
),
438+
);
439+
440+
expect(await pm.getRegistryMetadata('non-existent')).toBeNull();
441+
});
425442
});
426443

427444
describe('getCurrentPackageName', () => {

‎packages/angular/cli/src/package-managers/parsers.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,62 @@ export function parseYarnClassicError(output: string, logger?: Logger): ErrorInf
636636
return null;
637637
}
638638

639+
/**
640+
* Parses the `stdout` or `stderr` output of modern yarn (v2+) to extract structured error information.
641+
*
642+
* Modern yarn reports errors as JSON lines, one per message line. The first line holds the summary,
643+
* and registry errors include the HTTP status, which is returned as a standardized error code
644+
* (`E${statusCode}`). Other output is delegated to `parseNpmLikeError`.
645+
*
646+
* Example output (`yarn npm info <missing-package> --json`):
647+
* ```json
648+
* {"type":"error","name":35,"displayName":"YN0035","indent":"","data":"Package not found"}
649+
* {"type":"error","name":35,"displayName":"YN0035","indent":"","data":" Response Code: 404 (Not Found)"}
650+
* ```
651+
*
652+
* @param output The standard output or standard error of the command.
653+
* @param logger An optional logger instance.
654+
* @returns An `ErrorInfo` object if parsing is successful, otherwise `null`.
655+
*/
656+
export function parseYarnModernError(output: string, logger?: Logger): ErrorInfo | null {
657+
logger?.debug(`Parsing modern yarn error output...`);
658+
logStdout(output, logger); // Log output for debugging purposes
659+
660+
if (!output) {
661+
logger?.debug(' output is empty. No error found.');
662+
663+
return null;
664+
}
665+
666+
const messages: { code: string; data: string }[] = [];
667+
for (const json of parseJsonLines(output, logger)) {
668+
if (json?.type === 'error' && typeof json.data === 'string') {
669+
messages.push({ code: String(json.displayName ?? 'UNKNOWN_ERROR'), data: json.data });
670+
}
671+
}
672+
673+
if (messages.length === 0) {
674+
return parseNpmLikeError(output, logger);
675+
}
676+
677+
const [first, ...rest] = messages;
678+
const statusCodeMatch = messages
679+
.map(({ data }) => data.match(/Response Code: (\d{3})/))
680+
.find((match) => match !== null);
681+
const code = statusCodeMatch ? `E${statusCodeMatch[1]}` : first.code;
682+
logger?.debug(` Successfully parsed modern yarn error with code '${code}'.`);
683+
684+
return {
685+
code,
686+
summary: first.data.trim(),
687+
detail:
688+
rest
689+
.map(({ data }) => data.trim())
690+
.filter(Boolean)
691+
.join('\n') || undefined,
692+
};
693+
}
694+
639695
/**
640696
* Known Bun JSON error messages that indicate a requested version or dist-tag does not exist.
641697
*/

‎packages/angular/cli/src/package-managers/parsers_spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
parseYarnClassicError,
2020
parseYarnClassicManifest,
2121
parseYarnModernDependencies,
22+
parseYarnModernError,
2223
parseYarnReleaseAge,
2324
} from './parsers';
2425

@@ -641,6 +642,33 @@ project node_modules
641642
});
642643
});
643644

645+
describe('parseYarnModernError', () => {
646+
it('should parse a missing package from yarn npm info as E404', () => {
647+
const stdout = [
648+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":"Package not found"}',
649+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":" Response Code: 404 (Not Found)"}',
650+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":" Request Method: GET"}',
651+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":" Request URL: https://registry.yarnpkg.com/@angular%2fnon-existent"}',
652+
].join('\n');
653+
expect(parseYarnModernError(stdout)).toEqual(
654+
jasmine.objectContaining({ code: 'E404', summary: 'Package not found' }),
655+
);
656+
});
657+
658+
it('should use the yarn error code when there is no HTTP status', () => {
659+
const stdout =
660+
'{"type":"error","name":35,"displayName":"YN0035","indent":"","data":"Request timed out"}';
661+
expect(parseYarnModernError(stdout)).toEqual(
662+
jasmine.objectContaining({ code: 'YN0035', summary: 'Request timed out' }),
663+
);
664+
});
665+
666+
it('should return null for successful or empty output', () => {
667+
expect(parseYarnModernError('{"name":"@angular/core","version":"22.2.4"}')).toBeNull();
668+
expect(parseYarnModernError('')).toBeNull();
669+
});
670+
});
671+
644672
describe('parseYarnModernDependencies', () => {
645673
it('should parse yarn info --name-only --json output', () => {
646674
const stdout = `

0 commit comments

Comments
 (0)