Skip to content

Commit d11382c

Browse files
authored
fix: exclude musl binaries from function bundle when building on Netlify with pnpm (#3145)
1 parent 22ea91b commit d11382c

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/build/content/server.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,13 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
299299
}
300300
const src = join(ctx.standaloneDir, entry)
301301
const dest = join(ctx.serverHandlerDir, entry)
302-
await cp(src, dest, { recursive: true, verbatimSymlinks: true, force: true })
302+
const filter = ctx.constants.IS_LOCAL ? undefined : nodeModulesFilter
303+
await cp(src, dest, {
304+
recursive: true,
305+
verbatimSymlinks: true,
306+
force: true,
307+
filter,
308+
})
303309

304310
if (entry === 'node_modules') {
305311
await recreateNodeModuleSymlinks(ctx.resolveFromSiteDir('node_modules'), dest)
@@ -438,3 +444,24 @@ export const verifyHandlerDirStructure = async (ctx: PluginContext) => {
438444
)
439445
}
440446
}
447+
448+
// This is a workaround for Next.js installations in a pnpm+glibc context
449+
// Patch required due to an intermittent upstream issue in the npm/pnpm ecosystem
450+
// https://github.com/pnpm/pnpm/issues/9654
451+
// https://github.com/pnpm/pnpm/issues/5928
452+
// https://github.com/pnpm/pnpm/issues/7362 (persisting even though ticket is closed)
453+
const nodeModulesFilter = async (sourcePath: string) => {
454+
// Filtering rule for the following packages:
455+
// - @rspack+binding-linux-x64-musl
456+
// - @swc+core-linux-x64-musl
457+
// - @img+sharp-linuxmusl-x64
458+
// - @img+sharp-libvips-linuxmusl-x64
459+
if (
460+
sourcePath.includes('.pnpm') &&
461+
(sourcePath.includes('linuxmusl-x64') || sourcePath.includes('linux-x64-musl'))
462+
) {
463+
return false
464+
}
465+
466+
return true
467+
}

src/build/functions/edge.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,11 @@ const buildHandlerDefinition = (
313313
ctx: PluginContext,
314314
def: EdgeOrNodeMiddlewareDefinition,
315315
): Array<ManifestFunction> => {
316-
const functionHandlerName = getHandlerName({ name: def.name })
317-
const functionName = 'Next.js Middleware Handler'
318-
const cache = def.name.endsWith('middleware') ? undefined : ('manual' as const)
319-
const generator = `${ctx.pluginName}@${ctx.pluginVersion}`
320-
321316
return augmentMatchers(def.matchers, ctx).map((matcher) => ({
322-
function: functionHandlerName,
323-
name: functionName,
317+
function: getHandlerName({ name: def.name }),
318+
name: 'Next.js Middleware Handler',
324319
pattern: matcher.regexp,
325-
cache,
326-
generator,
320+
generator: `${ctx.pluginName}@${ctx.pluginVersion}`,
327321
}))
328322
}
329323

src/build/plugin-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ export interface ExportDetail {
4444
}
4545

4646
export class PluginContext {
47+
constants: NetlifyPluginConstants
4748
featureFlags: NetlifyPluginOptions['featureFlags']
4849
netlifyConfig: NetlifyPluginOptions['netlifyConfig']
4950
pluginName: string
5051
pluginVersion: string
5152
utils: NetlifyPluginUtils
5253

53-
private constants: NetlifyPluginConstants
5454
private packageJSON: { name: string; version: string } & Record<string, unknown>
5555

5656
/** Absolute path of the next runtime plugin directory */

0 commit comments

Comments
 (0)