Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update caching for swc turbo builds (#42929)
Updates to leverage turbo remote caching for the swc builds and normalize the package versions to prevent repo version bumps from un-necessarily invalidating the cache. Full cache hits can be seen in this test run https://github.com/vercel/next.js/actions/runs/3469932131/jobs/5797559609
- Loading branch information
Showing
5 changed files
with
171 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env node | ||
// @ts-check | ||
|
||
/* | ||
This prevents busting the turbo cache un-necessarily due | ||
to bumping the version in the repo's package.json files | ||
*/ | ||
const path = require('path') | ||
const fs = require('fs/promises') | ||
|
||
const cwd = process.cwd() | ||
const NORMALIZED_VERSION = '0.0.0' | ||
|
||
const readJson = async (filePath) => | ||
JSON.parse(await fs.readFile(filePath, 'utf8')) | ||
|
||
const writeJson = async (filePath, data) => | ||
fs.writeFile(filePath, JSON.stringify(data, null, 2) + '\n') | ||
|
||
;(async function () { | ||
const packages = await fs.readdir(path.join(cwd, 'packages')) | ||
const pkgJsonData = new Map() | ||
const pkgNames = [] | ||
await Promise.all( | ||
packages.map(async (pkgDir) => { | ||
const data = await readJson( | ||
path.join(cwd, 'packages', pkgDir, 'package.json') | ||
) | ||
pkgNames.push(data.name) | ||
pkgJsonData.set(pkgDir, data) | ||
}) | ||
) | ||
const normalizeVersions = async (filePath, data) => { | ||
data = data || (await readJson(filePath)) | ||
const version = data.version | ||
|
||
if (version) { | ||
data.version = NORMALIZED_VERSION | ||
const normalizeEntry = (type, key) => { | ||
const pkgVersion = data[type][key] | ||
|
||
if (pkgNames.includes(key) && pkgVersion === version) { | ||
data[type][key] = NORMALIZED_VERSION | ||
} | ||
} | ||
for (const key of Object.keys(data.dependencies || {})) { | ||
normalizeEntry('dependencies', key) | ||
} | ||
for (const key of Object.keys(data.devDependencies || {})) { | ||
normalizeEntry('devDependencies', key) | ||
} | ||
await writeJson(filePath, data) | ||
} | ||
} | ||
await Promise.all( | ||
packages.map((pkgDir) => | ||
normalizeVersions( | ||
path.join(cwd, 'packages', pkgDir, 'package.json'), | ||
pkgJsonData.get(pkgDir) | ||
) | ||
) | ||
) | ||
await normalizeVersions(path.join(cwd, 'lerna.json')) | ||
await fs.unlink(path.join(cwd, 'pnpm-lock.yaml')) | ||
await fs.writeFile(path.join(cwd, 'pnpm-lock.yaml'), '') | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
// @ts-check | ||
|
||
const { execSync } = require('child_process') | ||
|
||
;(async function () { | ||
const turboResult = execSync( | ||
`pnpm turbo run cache-build-native --dry=json -- --platform --release --target x86_64-unknown-freebsd` | ||
).toString() | ||
|
||
const turboData = JSON.parse(turboResult) | ||
|
||
const task = turboData.tasks.find((t) => t.command !== '<NONEXISTENT>') | ||
|
||
if (!task) { | ||
console.warn(`Failed to find related turbo task`, turboResult) | ||
return | ||
} | ||
|
||
// pull cache if it was available | ||
if (task.cacheState.local || task.cacheState.remote) { | ||
const pullResult = execSync( | ||
`pnpm turbo run cache-build-native -- --platform --release --target x86_64-unknown-freebsd` | ||
).toString() | ||
console.log(pullResult) | ||
} else { | ||
console.warn(`No turbo cache was available, continuing...`) | ||
console.warn(task) | ||
} | ||
})() |
Oops, something went wrong.