Retry SwiftPM artifact version lookups on transient failures (#59955)

This commit is contained in:
Lazizbek Ergashev authored and GitHub committed 2026-11-13 12:37:12 +00:00
1 parent 3f54cc20ed
commit fd6fb6671c
2 files changed
+84 -8

No files matched your search

@@ -671,11 +671,14 @@ describe('download', () => {
describe('resolveSnapshotUrl', () => {
let origFetch;
let warnSpy;
beforeEach(() => {
origFetch = globalThis.fetch;
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
globalThis.fetch = origFetch;
warnSpy.mockRestore();
});
const METADATA = `<?xml version="1.0"?>
@@ -716,7 +719,7 @@ describe('resolveSnapshotUrl', () => {
it('throws when the metadata request fails', async () => {
globalThis.fetch = routerFetch({
'maven-metadata.xml': {ok: false, status: 500},
'maven-metadata.xml': {ok: false, status: 404},
});
await expect(
resolveSnapshotUrl(
@@ -728,6 +731,25 @@ describe('resolveSnapshotUrl', () => {
).rejects.toThrow(/Failed to fetch snapshot metadata/);
});
it('retries the metadata request after a transient server error', async () => {
const router = routerFetch({'maven-metadata.xml': {text: METADATA}});
globalThis.fetch = jest
.fn()
.mockResolvedValueOnce({ok: false, status: 503})
.mockImplementation(router);
const pending = resolveSnapshotUrl(
'0.85.0',
'react',
'react-native-artifacts',
'reactnative-core-debug.tar.gz',
);
await jest.advanceTimersByTimeAsync(1_000);
expect(await pending).toContain('0.85.0-20260101.123456-7');
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
});
it('throws when timestamp/buildNumber are absent', async () => {
globalThis.fetch = routerFetch({
'maven-metadata.xml': {text: '<metadata></metadata>'},
@@ -749,11 +771,14 @@ describe('resolveSnapshotUrl', () => {
describe('npm dist-tag resolvers', () => {
let origFetch;
let warnSpy;
beforeEach(() => {
origFetch = globalThis.fetch;
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
globalThis.fetch = origFetch;
warnSpy.mockRestore();
});
it('resolveNightlyVersion returns the version from the npm registry', async () => {
@@ -765,6 +790,25 @@ describe('npm dist-tag resolvers', () => {
);
});
it('resolveNightlyVersion retries a dropped connection', async () => {
const router = routerFetch({
'react-native/nightly': {json: {version: '0.86.0-nightly-xyz'}},
});
globalThis.fetch = jest
.fn()
.mockRejectedValueOnce(new TypeError('fetch failed'))
.mockImplementation(router);
const pending = resolveNightlyVersion('react-native');
await jest.advanceTimersByTimeAsync(1_000);
expect(await pending).toBe('0.86.0-nightly-xyz');
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('retrying in 1000ms'),
);
});
it('resolveNightlyVersion throws on a failed npm lookup', async () => {
globalThis.fetch = routerFetch({
'react-native/nightly': {ok: false, status: 404},
@@ -785,7 +829,13 @@ describe('npm dist-tag resolvers', () => {
globalThis.fetch = routerFetch({
'hermes-compiler/latest-v1': {ok: false, status: 500},
});
await expect(resolveLatestV1Version()).rejects.toThrow(/npm lookup failed/);
await Promise.all([
expect(resolveLatestV1Version()).rejects.toThrow(/npm lookup failed/),
jest.advanceTimersByTimeAsync(3_000),
]);
expect(globalThis.fetch).toHaveBeenCalledTimes(3);
});
});
@@ -1018,7 +1068,7 @@ describe('resolveCacheSlotVersion', () => {
it('falls back to the raw label when the nightly lookup fails', async () => {
globalThis.fetch = routerFetch({
'react-native/nightly': {ok: false, status: 503},
'react-native/nightly': {ok: false, status: 404},
});
expect(await resolveCacheSlotVersion('nightly')).toBe('nightly');
});
+31 -5
View File
@@ -243,7 +243,7 @@ async function resolveSnapshotUrl(
`${version}-SNAPSHOT/maven-metadata.xml`;
log(` Fetching snapshot metadata: ${metadataUrl}`);
const res = await fetch(metadataUrl);
const res = await fetchWithRetry(metadataUrl);
if (!res.ok) {
throw new Error(
`Failed to fetch snapshot metadata (${res.status}): ${metadataUrl}`,
@@ -305,7 +305,9 @@ async function resolveNightlyVersion(
) /*: Promise<string> */ {
log(` Resolving nightly version from npm: ${npmPackage}`);
const res = await fetch(`https://registry.npmjs.org/${npmPackage}/nightly`);
const res = await fetchWithRetry(
`https://registry.npmjs.org/${npmPackage}/nightly`,
);
if (!res.ok) {
throw new Error(`npm lookup failed for ${npmPackage}: ${res.status}`);
}
@@ -358,8 +360,7 @@ async function resolveCacheSlotVersion(
async function resolveLatestV1Version() /*: Promise<string> */ {
log(' Resolving latest-v1 Hermes from npm...');
// $FlowFixMe[incompatible-call] global fetch not in Flow stubs
const res = await fetch(
const res = await fetchWithRetry(
'https://registry.npmjs.org/hermes-compiler/latest-v1',
);
if (!res.ok) {
@@ -651,7 +652,10 @@ function isRetryableDownloadError(error /*: Error */) /*: boolean */ {
if (httpStatus == null) {
return true;
}
const status = Number(httpStatus);
return isRetryableHttpStatus(Number(httpStatus));
}
function isRetryableHttpStatus(status /*: number */) /*: boolean */ {
return status === 408 || status === 429 || status >= 500;
}
@@ -819,6 +823,28 @@ async function download(
}
}
async function fetchWithRetry(url /*: string */) /*: Promise<Response> */ {
for (let attempt = 1; attempt < DOWNLOAD_MAX_ATTEMPTS; attempt++) {
let reason;
try {
const res = await fetch(url);
if (!isRetryableHttpStatus(res.status)) {
return res;
}
reason = `HTTP ${res.status}`;
} catch (error) {
reason = error.message;
}
const delayMs = DOWNLOAD_RETRY_BASE_DELAY_MS * attempt;
warn(
` Request failed (${reason}); retrying in ${delayMs}ms ` +
`(attempt ${attempt + 1}/${DOWNLOAD_MAX_ATTEMPTS})`,
);
await new Promise(resolve => setTimeout(resolve, delayMs));
}
return fetch(url);
}
/**
* Extracts a .tar.gz and returns the path to the first .xcframework found.
*/