From 6d1bab313570ec1e6e7c88e7b379d0a6f510f341 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Thu, 3 Dec 2026 14:59:30 +0200 Subject: [PATCH] fix: pick a crawlee version the latest apify SDK supports --- .../src/matrices/node/normal.ts | 3 +- .../src/matrices/node/playwright.ts | 3 +- .../src/matrices/node/puppeteer.ts | 3 +- .../version-matrix/src/shared/crawlee.test.ts | 21 +++++++++ .../version-matrix/src/shared/crawlee.ts | 46 +++++++++++++++++++ 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 .github/actions/version-matrix/src/shared/crawlee.test.ts create mode 100644 .github/actions/version-matrix/src/shared/crawlee.ts diff --git a/.github/actions/version-matrix/src/matrices/node/normal.ts b/.github/actions/version-matrix/src/matrices/node/normal.ts index 61a043a8a..06f5e5640 100644 --- a/.github/actions/version-matrix/src/matrices/node/normal.ts +++ b/.github/actions/version-matrix/src/matrices/node/normal.ts @@ -5,6 +5,7 @@ import { setParametersForTriggeringUpdateWorkflowOnActorTemplates, supportedNodeVersions, } from '../../shared/constants.ts'; +import { resolveCrawleeVersionForApify } from '../../shared/crawlee.ts'; import { fetchPackageVersions } from '../../shared/npm.ts'; import { fetchNodeRuntimeVersions } from '../../shared/runtime-versions.ts'; @@ -12,8 +13,8 @@ const apifyVersions = await fetchPackageVersions('apify'); const crawleeVersions = await fetchPackageVersions('crawlee'); const nodeRuntimeVersions = await fetchNodeRuntimeVersions(supportedNodeVersions); -let latestCrawleeVersion = crawleeVersions.at(-1)!; let latestApifyVersion = apifyVersions.at(-1)!; +let latestCrawleeVersion = await resolveCrawleeVersionForApify(latestApifyVersion, crawleeVersions); console.error('Latest crawlee version:', latestCrawleeVersion); console.error('Latest apify version:', latestApifyVersion); diff --git a/.github/actions/version-matrix/src/matrices/node/playwright.ts b/.github/actions/version-matrix/src/matrices/node/playwright.ts index 5247d32e3..6f2569923 100644 --- a/.github/actions/version-matrix/src/matrices/node/playwright.ts +++ b/.github/actions/version-matrix/src/matrices/node/playwright.ts @@ -12,6 +12,7 @@ import { shouldUseLastFive, supportedNodeVersions, } from '../../shared/constants.ts'; +import { resolveCrawleeVersionForApify } from '../../shared/crawlee.ts'; import { fetchPackageVersions } from '../../shared/npm.ts'; import { fetchNodeRuntimeVersions } from '../../shared/runtime-versions.ts'; @@ -28,7 +29,7 @@ if (!shouldUseLastFive) { const latestFivePlaywrightVersions = playwrightVersions.slice(shouldUseLastFive ? -5 : -1); const latestPlaywrightVersion = latestFivePlaywrightVersions.at(-1)!; let latestApifyVersion = apifyVersions.at(-1)!; -let latestCrawleeVersion = crawleeVersions.at(-1)!; +let latestCrawleeVersion = await resolveCrawleeVersionForApify(latestApifyVersion, crawleeVersions); let latestCamoufoxVersion = camoufoxVersions.at(-1)!; // Camoufox does not support every Playwright release, so its images are built with the newest Playwright versions it diff --git a/.github/actions/version-matrix/src/matrices/node/puppeteer.ts b/.github/actions/version-matrix/src/matrices/node/puppeteer.ts index 51621294c..674997d72 100644 --- a/.github/actions/version-matrix/src/matrices/node/puppeteer.ts +++ b/.github/actions/version-matrix/src/matrices/node/puppeteer.ts @@ -6,6 +6,7 @@ import { shouldUseLastFive, supportedNodeVersions, } from '../../shared/constants.ts'; +import { resolveCrawleeVersionForApify } from '../../shared/crawlee.ts'; import { fetchPackageVersions } from '../../shared/npm.ts'; import { fetchNodeRuntimeVersions } from '../../shared/runtime-versions.ts'; @@ -21,7 +22,7 @@ if (!shouldUseLastFive) { const latestFivePuppeteerVersions = puppeteerVersions.slice(shouldUseLastFive ? -5 : -1); const latestPuppeteerVersion = latestFivePuppeteerVersions.at(-1)!; let latestApifyVersion = apifyVersions.at(-1)!; -let latestCrawleeVersion = crawleeVersions.at(-1)!; +let latestCrawleeVersion = await resolveCrawleeVersionForApify(latestApifyVersion, crawleeVersions); console.error('Latest five versions', latestFivePuppeteerVersions); console.error('Latest apify version', latestApifyVersion); diff --git a/.github/actions/version-matrix/src/shared/crawlee.test.ts b/.github/actions/version-matrix/src/shared/crawlee.test.ts new file mode 100644 index 000000000..ecf38b913 --- /dev/null +++ b/.github/actions/version-matrix/src/shared/crawlee.test.ts @@ -0,0 +1,21 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { resolveCrawleeVersionForApify } from './crawlee.ts'; + +const crawleeVersions = ['3.17.0', '3.18.1', '3.18.2', '4.0.0']; + +describe('resolveCrawleeVersionForApify', () => { + it('picks the newest crawlee version inside the range apify declares', async () => { + // apify 3.8.0 declares `@crawlee/core: ^3.18.2` + assert.equal(await resolveCrawleeVersionForApify('3.8.0', crawleeVersions), '3.18.2'); + }); + + it('falls back to the newest crawlee version when nothing satisfies the range', async () => { + assert.equal(await resolveCrawleeVersionForApify('3.8.0', ['2.0.0', '4.0.0']), '4.0.0'); + }); + + it('falls back to the newest crawlee version when the apify manifest cannot be read', async () => { + assert.equal(await resolveCrawleeVersionForApify('0.0.0-does-not-exist', crawleeVersions), '4.0.0'); + }); +}); diff --git a/.github/actions/version-matrix/src/shared/crawlee.ts b/.github/actions/version-matrix/src/shared/crawlee.ts new file mode 100644 index 000000000..f358d88df --- /dev/null +++ b/.github/actions/version-matrix/src/shared/crawlee.ts @@ -0,0 +1,46 @@ +import { satisfies } from 'semver'; +import { fetchPackageManifest } from './npm.ts'; + +/** + * Picks the newest crawlee version the given apify version can run with, based on the range apify declares for + * `@crawlee/core`. + * + * The images force apify's `@crawlee/*` dependencies to the installed crawlee version, so pairing apify with a + * crawlee major it was not built for (e.g. crawlee 4 released before apify 4) produces an image that crashes on + * `require('apify')`. + * + * Falls back to the newest crawlee version if the range cannot be read or nothing satisfies it. + */ +export async function resolveCrawleeVersionForApify(apifyVersion: string, crawleeVersions: string[]) { + const latestCrawleeVersion = crawleeVersions.at(-1)!; + + let range: string | undefined; + + try { + const manifest = await fetchPackageManifest('apify', apifyVersion); + + range = manifest.dependencies?.['@crawlee/core'] ?? manifest.peerDependencies?.['@crawlee/core']; + } catch (error) { + console.error(`Failed to read the supported crawlee range from apify@${apifyVersion}`, error); + } + + if (range === undefined) { + return latestCrawleeVersion; + } + + const compatibleVersion = crawleeVersions.findLast((version) => satisfies(version, range)); + + if (compatibleVersion === undefined) { + console.error(`No crawlee version satisfies ${range} (from apify@${apifyVersion}), using the latest one`); + + return latestCrawleeVersion; + } + + if (compatibleVersion !== latestCrawleeVersion) { + console.error( + `apify@${apifyVersion} supports crawlee ${range}, using crawlee@${compatibleVersion} instead of ${latestCrawleeVersion}`, + ); + } + + return compatibleVersion; +}