-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event for swc load failure and attempt patching lockfile (#36527)
- Loading branch information
Showing
7 changed files
with
221 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { promises } from 'fs' | ||
import '../server/node-polyfill-fetch' | ||
import * as Log from '../build/output/log' | ||
import findUp from 'next/dist/compiled/find-up' | ||
|
||
/** | ||
* Attempts to patch npm package-lock.json when it | ||
* fails to include optionalDependencies for other platforms | ||
* this can occur when the package-lock is rebuilt from a current | ||
* node_modules install instead of pulling fresh package data | ||
*/ | ||
export async function patchIncorrectLockfile(dir: string) { | ||
const lockfilePath = await findUp('package-lock.json', { cwd: dir }) | ||
|
||
if (!lockfilePath) { | ||
// if no lockfile present there is no action to take | ||
return | ||
} | ||
const content = await promises.readFile(lockfilePath, 'utf8') | ||
const lockfileParsed = JSON.parse(content) | ||
|
||
const packageKeys = Object.keys(lockfileParsed.dependencies) | ||
const foundSwcPkgs = new Set() | ||
const nextPkg = lockfileParsed.packages['node_modules/next'] | ||
|
||
if (!nextPkg) { | ||
return console.error('Failed to locate next in', lockfilePath) | ||
} | ||
const nextVersion = nextPkg.version | ||
|
||
const expectedSwcPkgs = Object.keys(nextPkg?.optionalDependencies).filter( | ||
(pkg) => pkg.startsWith('@next/swc-') | ||
) | ||
|
||
packageKeys.forEach((pkgKey) => { | ||
const swcIndex = pkgKey.indexOf('@next/swc-') | ||
if (swcIndex > -1) { | ||
foundSwcPkgs.add(pkgKey.substring(swcIndex)) | ||
} | ||
}) | ||
|
||
// if swc package keys are missing manually populate them | ||
// so installs on different platforms can succeed | ||
// user will need to run npm i after to ensure it's corrected | ||
if (foundSwcPkgs.size !== expectedSwcPkgs.length) { | ||
Log.warn(`Found lockfile missing swc dependencies, patching..`) | ||
|
||
try { | ||
// populate fields for each missing swc pkg | ||
for (const pkg of expectedSwcPkgs) { | ||
if (!foundSwcPkgs.has(pkg)) { | ||
console.log('fetching', pkg) | ||
const res = await fetch(`https://registry.npmjs.org/${pkg}`) | ||
|
||
if (!res.ok) { | ||
throw new Error( | ||
`Failed to fetch registry info for ${pkg}, got status ${res.status}` | ||
) | ||
} | ||
const data = await res.json() | ||
const version = data.versions[nextVersion] | ||
|
||
if (!version) { | ||
throw new Error( | ||
`Failed to find matching version for ${pkg} at ${nextVersion}` | ||
) | ||
} | ||
lockfileParsed.packages[`node_modules/${pkg}`] = { | ||
version: nextVersion, | ||
resolved: version.dist.tarball, | ||
integrity: version.dist.integrity, | ||
cpu: version.cpu, | ||
optional: true, | ||
os: version.os, | ||
engines: version.engines, | ||
} | ||
} | ||
} | ||
|
||
await promises.writeFile( | ||
lockfilePath, | ||
JSON.stringify(lockfileParsed, null, 2) | ||
) | ||
Log.warn( | ||
'Lockfile was successfully patched, please run "npm install" to ensure @next/swc dependencies are downloaded' | ||
) | ||
} catch (err) { | ||
Log.error( | ||
`Failed to patch lockfile, please try uninstalling and reinstalling next in this workspace` | ||
) | ||
console.error(err) | ||
} | ||
} | ||
} |
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,31 @@ | ||
import { traceGlobals } from '../../trace/shared' | ||
import { Telemetry } from '../storage' | ||
|
||
const EVENT_PLUGIN_PRESENT = 'NEXT_SWC_LOAD_FAILURE' | ||
export type EventSwcLoadFailure = { | ||
eventName: string | ||
payload: { | ||
platform: string | ||
arch: string | ||
nodeVersion: string | ||
nextVersion: string | ||
wasm?: string | ||
glibcVersion?: string | ||
installedSwcPackages?: string | ||
} | ||
} | ||
|
||
export async function eventSwcLoadFailure( | ||
event: EventSwcLoadFailure['payload'] | ||
): Promise<void> { | ||
const telemetry: Telemetry = traceGlobals.get('telemetry') | ||
// can't continue if telemetry isn't set | ||
if (!telemetry) return | ||
|
||
telemetry.record({ | ||
eventName: EVENT_PLUGIN_PRESENT, | ||
payload: event, | ||
}) | ||
// ensure this event is flushed before process exits | ||
await telemetry.flush() | ||
} |
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
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