Skip to content

fix: use directory-level unpack patterns to avoid minimatch length limit#178

Open
akkay-fp wants to merge 3 commits intoelectron:mainfrom
akkay-fp:fix/pattern-too-long-unpack
Open

fix: use directory-level unpack patterns to avoid minimatch length limit#178
akkay-fp wants to merge 3 commits intoelectron:mainfrom
akkay-fp:fix/pattern-too-long-unpack

Conversation

@akkay-fp
Copy link

@akkay-fp akkay-fp commented Mar 10, 2026

Summary

When merging ASARs for universal macOS builds, the unpack pattern passed to asar.createPackageWithOptions is constructed by listing every individual unpacked file path in a brace expansion: {/tmp/x64-.../file1,/tmp/x64-.../file2,...}.

For projects with many unpacked files (e.g. apps using native modules like sharp, better-sqlite3, ag-psd that get auto-unpacked by electron-builder's detectUnpackedDirs), this pattern can easily exceed minimatch's 64 KiB pattern length limit, causing:

TypeError: pattern is too long
    at assertValidPattern (minimatch.js:279)
    at shouldUnpackPath (@electron/asar/src/asar.ts:158)
    at mergeASARs (@electron/universal/src/asar-utils.ts)

Fix

Instead of listing every individual file path, group unpacked files by their top-level directory (e.g. node_modules/sharp, node_modules/@img/sharp-darwin-arm64, dist/main) and construct ** glob patterns. This reduces thousands of entries to a handful of compact patterns while preserving identical unpack behavior.

Before (3000+ files → ~400KB pattern):

{/tmp/x64-abc/node_modules/ag-psd/dist/psdReader.js,/tmp/x64-abc/node_modules/ag-psd/dist/abr.js,...3000 more}

After (6 directories → ~500 byte pattern):

{/tmp/x64-abc/node_modules/ag-psd/**,/tmp/x64-abc/node_modules/sharp/**,/tmp/x64-abc/dist/main/**,...}

Related Issues

Test Plan

  • Tested with a production Electron app (FilterPixel) that has 3233 unpacked files across native modules (sharp, better-sqlite3, ag-psd, fsevents, exiftool)
  • Universal build (--mac --universal) completes successfully with the fix
  • All build artifacts (DMG, PKG, ZIP) produced correctly

Disclaimer: I took claude help for clean code comments and PR creation.
🤖 Generated with Claude Code

When merging ASARs for universal builds, the unpack pattern was
constructed by listing every individual unpacked file path in a brace
expansion like `{file1,file2,...}`. For projects with many unpacked
files (e.g. auto-unpacked native modules), this pattern can exceed
minimatch's 64 KiB limit, causing a "pattern is too long" TypeError.

This change groups unpacked files by their top-level directory (e.g.
`node_modules/pkg` or `dist/main`) and uses `**` glob patterns instead.
This reduces thousands of entries to a handful of directory patterns
while preserving the same unpack behavior.

Fixes electron-userland/electron-builder#7044
@akkay-fp akkay-fp requested a review from a team as a code owner March 10, 2026 20:41
// listing every file individually. When there are many unpacked files
// (e.g. auto-unpacked native modules) the old brace-expansion approach
// can exceed minimatch's 64 KiB pattern-length limit.
// See: https://github.com/electron/universal/issues/XXX
Copy link
Member

@erickzhao erickzhao Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an open issue for this in the repo? Or was this supposed to link to the PR?

Copy link
Author

@akkay-fp akkay-fp Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was an oversight on my end. I haven't opened a separate issue because the PR description covers the problem comprehensively. I have updated the link to point here.

@akkay-fp
Copy link
Author

@erickzhao I ran Prettier and pushed the latest changes to resolve the CI lint test case failures.

Also, just a heads-up that the recent commit from @Akshay-akkay is from my personal GitHub account, as I was working on this from my own machine. Let me know if you need anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants