Unverified Commit 60f74230 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Tweak lockfile patching to be eager (#36549)

This is a follow-up to https://github.com/vercel/next.js/pull/36527 to make sure we eagerly patch the lockfile as the correct swc dependency will be present if the platform matches so the load failure isn't hit which is where we were previously patching the lockfile. This is done in the background so does not block compilation. This also removes some extra logs that aren't necessary. 

<details>

<summary>before lockfile patch</summary>

![before](https://user-images.githubusercontent.com/22380829/165780507-09e5aee6-3253-483f-9e3f-e24bea6df377.png)

</details>


<details>

<summary>after lockfile patch</summary>

![after](https://user-images.githubusercontent.com/22380829/165780545-829665aa-05ec-431d-aa2f-42c4ce3badd7.png)

</details>

This also fixes a case I noticed where we weren't flushing events before exiting when builds fail
parent 4392b6a9
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -5,3 +5,4 @@ export function minify(src: string, options: any): Promise<string> ...@@ -5,3 +5,4 @@ export function minify(src: string, options: any): Promise<string>
export function minifySync(src: string, options: any): string export function minifySync(src: string, options: any): string
export function bundle(options: any): Promise<any> export function bundle(options: any): Promise<any>
export function parse(src: string, options: any): any export function parse(src: string, options: any): any
export const lockfilePatchPromise: { cur?: Promise<void> }
...@@ -12,8 +12,17 @@ const triples = platformArchTriples[PlatformName][ArchName] || [] ...@@ -12,8 +12,17 @@ const triples = platformArchTriples[PlatformName][ArchName] || []
let nativeBindings let nativeBindings
let wasmBindings let wasmBindings
export const lockfilePatchPromise = {}
async function loadBindings() { async function loadBindings() {
if (!lockfilePatchPromise.cur) {
// always run lockfile check once so that it gets patched
// even if it doesn't fail to load locally
lockfilePatchPromise.cur = patchIncorrectLockfile(process.cwd()).catch(
console.error
)
}
let attempts = [] let attempts = []
try { try {
return loadNative() return loadNative()
...@@ -45,7 +54,13 @@ function loadBindingsSync() { ...@@ -45,7 +54,13 @@ function loadBindingsSync() {
logLoadFailure(attempts) logLoadFailure(attempts)
} }
let loggingLoadFailure = false
function logLoadFailure(attempts) { function logLoadFailure(attempts) {
// make sure we only emit the event and log the failure once
if (loggingLoadFailure) return
loggingLoadFailure = true
for (let attempt of attempts) { for (let attempt of attempts) {
Log.warn(attempt) Log.warn(attempt)
} }
...@@ -74,17 +89,15 @@ function logLoadFailure(attempts) { ...@@ -74,17 +89,15 @@ function logLoadFailure(attempts) {
} }
} catch (_) {} } catch (_) {}
patchIncorrectLockfile(process.cwd()) eventSwcLoadFailure({
.then(() => { nextVersion,
return eventSwcLoadFailure({ glibcVersion,
nextVersion, installedSwcPackages,
glibcVersion, arch: process.arch,
installedSwcPackages, platform: process.platform,
arch: process.arch, nodeVersion: process.versions.node,
platform: process.platform, })
nodeVersion: process.versions.node, .then(() => lockfilePatchPromise.cur || Promise.resolve())
})
})
.finally(() => { .finally(() => {
Log.error( Log.error(
`Failed to load SWC binary for ${PlatformName}/${ArchName}, see more info here: https://nextjs.org/docs/messages/failed-loading-swc` `Failed to load SWC binary for ${PlatformName}/${ArchName}, see more info here: https://nextjs.org/docs/messages/failed-loading-swc`
......
...@@ -23,8 +23,9 @@ export async function patchIncorrectLockfile(dir: string) { ...@@ -23,8 +23,9 @@ export async function patchIncorrectLockfile(dir: string) {
const foundSwcPkgs = new Set() const foundSwcPkgs = new Set()
const nextPkg = lockfileParsed.packages['node_modules/next'] const nextPkg = lockfileParsed.packages['node_modules/next']
// if we don't find next in the package-lock we can't continue
if (!nextPkg) { if (!nextPkg) {
return console.error('Failed to locate next in', lockfilePath) return
} }
const nextVersion = nextPkg.version const nextVersion = nextPkg.version
...@@ -49,7 +50,6 @@ export async function patchIncorrectLockfile(dir: string) { ...@@ -49,7 +50,6 @@ export async function patchIncorrectLockfile(dir: string) {
// populate fields for each missing swc pkg // populate fields for each missing swc pkg
for (const pkg of expectedSwcPkgs) { for (const pkg of expectedSwcPkgs) {
if (!foundSwcPkgs.has(pkg)) { if (!foundSwcPkgs.has(pkg)) {
console.log('fetching', pkg)
const res = await fetch(`https://registry.npmjs.org/${pkg}`) const res = await fetch(`https://registry.npmjs.org/${pkg}`)
if (!res.ok) { if (!res.ok) {
...@@ -65,6 +65,14 @@ export async function patchIncorrectLockfile(dir: string) { ...@@ -65,6 +65,14 @@ export async function patchIncorrectLockfile(dir: string) {
`Failed to find matching version for ${pkg} at ${nextVersion}` `Failed to find matching version for ${pkg} at ${nextVersion}`
) )
} }
if (lockfileParsed.dependencies) {
lockfileParsed.dependencies[pkg] = {
version: nextVersion,
resolved: version.dist.tarball,
integrity: version.dist.integrity,
optional: true,
}
}
lockfileParsed.packages[`node_modules/${pkg}`] = { lockfileParsed.packages[`node_modules/${pkg}`] = {
version: nextVersion, version: nextVersion,
resolved: version.dist.tarball, resolved: version.dist.tarball,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment