|
3 | 3 | const os = require("os"); |
4 | 4 | const path = require("path"); |
5 | 5 |
|
6 | | -// The same cache `yarn setup:firefox` installs into, and the one puppeteer's |
| 6 | +// The same cache `yarn setup:browsers` installs into, and the one puppeteer's |
7 | 7 | // own tooling reads, so the two agree wherever it has been pointed elsewhere. |
8 | 8 | const CACHE_DIR = |
9 | 9 | process.env.PUPPETEER_CACHE_DIR || |
10 | 10 | path.join(os.homedir(), ".cache", "puppeteer"); |
11 | 11 |
|
12 | 12 | /** |
13 | | - * Where an already-downloaded Firefox sits, or `undefined` when none is. The |
| 13 | + * Where the build of `engine` puppeteer-core pins sits once downloaded, or |
| 14 | + * `undefined` when it is not. Only that build counts: an older one left in the |
| 15 | + * cache would hold the suites to an engine the lockfile no longer names. The |
14 | 16 | * suites do not download one themselves: a cold fetch is tens of megabytes and |
15 | 17 | * would expire the callers' hook budget rather than report a browser problem. |
| 18 | + * @param {"chrome" | "firefox"} engine the engine |
16 | 19 | * @returns {Promise<string | undefined>} the executable path |
17 | 20 | */ |
18 | | -const installedFirefox = async () => { |
| 21 | +const installedBrowser = async (engine) => { |
19 | 22 | // ESM-only, like puppeteer-core itself, so it is loaded the same way. |
20 | 23 | const { Browser, detectBrowserPlatform, getInstalledBrowsers } = |
21 | 24 | await import("@puppeteer/browsers"); |
| 25 | + // Exported at runtime, but kept out of puppeteer-core's types as internal. |
| 26 | + const { PUPPETEER_REVISIONS } = |
| 27 | + /** @type {{ PUPPETEER_REVISIONS: Readonly<Record<"chrome" | "firefox", string>> }} */ ( |
| 28 | + /** @type {unknown} */ (await import("puppeteer-core")) |
| 29 | + ); |
22 | 30 |
|
23 | 31 | const platform = detectBrowserPlatform(); |
24 | 32 | if (platform === undefined) return undefined; |
| 33 | + const browser = engine === "chrome" ? Browser.CHROME : Browser.FIREFOX; |
| 34 | + const buildId = PUPPETEER_REVISIONS[engine]; |
25 | 35 | for (const one of await getInstalledBrowsers({ cacheDir: CACHE_DIR })) { |
26 | | - if (one.browser === Browser.FIREFOX && one.platform === platform) { |
| 36 | + if ( |
| 37 | + one.browser === browser && |
| 38 | + one.platform === platform && |
| 39 | + one.buildId === buildId |
| 40 | + ) { |
27 | 41 | return one.executablePath; |
28 | 42 | } |
29 | 43 | } |
@@ -143,17 +157,23 @@ module.exports = async (options) => { |
143 | 157 | const executablePath = |
144 | 158 | launchOptions.executablePath || |
145 | 159 | process.env.FIREFOX_EXECUTABLE_PATH || |
146 | | - (await installedFirefox()); |
| 160 | + (await installedBrowser("firefox")); |
147 | 161 | if (executablePath === undefined) { |
148 | 162 | throw new Error( |
149 | | - "no Firefox to launch: run `yarn setup:firefox`, or point FIREFOX_EXECUTABLE_PATH at one" |
| 163 | + "no Firefox to launch: run `yarn setup:browsers firefox`, or point FIREFOX_EXECUTABLE_PATH at one" |
150 | 164 | ); |
151 | 165 | } |
152 | 166 | launchOptions.executablePath = executablePath; |
153 | 167 | } else if (!launchOptions.executablePath) { |
154 | | - if (process.env.PUPPETEER_EXECUTABLE_PATH) { |
155 | | - launchOptions.executablePath = process.env.PUPPETEER_EXECUTABLE_PATH; |
| 168 | + const executablePath = |
| 169 | + process.env.PUPPETEER_EXECUTABLE_PATH || |
| 170 | + (await installedBrowser("chrome")); |
| 171 | + if (executablePath !== undefined) { |
| 172 | + launchOptions.executablePath = executablePath; |
156 | 173 | } else { |
| 174 | + // The system Chrome moves with the machine rather than the lockfile, |
| 175 | + // which is fine for a suite that only needs a Chrome, but not for one |
| 176 | + // holding output to an engine: CI installs the pinned build for those. |
157 | 177 | launchOptions.channel = "chrome"; |
158 | 178 | } |
159 | 179 | } |
|
0 commit comments