Commit e335251
feat: patch-package for SES-compat patches; root patches/ as single source of truth (#874)
## Summary
`@MetaMask/ocap-kernel` and `@metamask/kernel-utils` depend on
`@chainsafe/libp2p-yamux@7.0.4`, which requires a patch for SES/lockdown
compatibility. Previously the patch was applied via Yarn's `patch:`
protocol from a root `.yarn/patches/` file — invisible to and
unreproducible by consumers.
This PR switches to `patch-package` and exposes the patch in published
npm tarballs so consumers can apply it. Root `patches/` is the single
source of truth; per-package copies are generated at publish time by a
script that uses dependency graph analysis to determine which packages
need to ship the patch.
## Changes
**patch-package setup**
- Replace the Yarn `patch:` URL for `@chainsafe/libp2p-yamux` with a
plain `7.0.4` version specifier across all dependent packages
- Add `patch-package` as a root `devDependency`; add it to the root
`postinstall` so the patch is applied after every `yarn install`
- Add `patches/@ChainSafe+libp2p-yamux+7.0.4.patch` at the repo root and
remove the old `.yarn/patches/` file
**Graph-based sink analysis**
- Add `scripts/copy-patches.cjs`: for each root patch file, computes
which published packages are "sinks" — packages whose consumers may not
install any sibling that already handles the patch — and copies patch
files to their `patches/` dirs before packaging
- A sink is any package that directly depends on the patched dep and has
no other package in that set as a transitive dependency. Currently
`@metamask/kernel-utils` is the only sink (`@MetaMask/ocap-kernel`
depends on it, so its consumers always get `kernel-utils`'
`postinstall`)
- Add `node scripts/copy-patches.cjs` to `scripts/prepack.sh`; add
`packages/*/patches/` to `.gitignore`
**Sink package (`@metamask/kernel-utils`)**
- Add `"patches/"` to `files` and `"postinstall": "patch-package
--patch-dir patches"` so the patch is applied for consumers
- Add `"patch-package": "*"` to `peerDependencies` to signal the
requirement
**Non-sink package (`@MetaMask/ocap-kernel`)**
- Remove `"patches/"` from `files`; no `postinstall` needed
(kernel-utils handles it)
**`yarn constraints` enforcement**
- Replace `expectPatchedDependenciesAreDeclaredAndShipped` with
graph-aware rules: `expectPatchShippingIsCorrect` (sinks must have the
required fields; non-sinks must not — auto-fixable where possible) and
`expectNoPatchesForRemovedDependencies` (errors if a root patch targets
a package no longer depended on by any workspace)
- Keep `expectNoPatchProtocolProductionDependencies`
## Testing
- `yarn constraints` passes; `kernel-utils` is identified as a sink and
has all required fields; `ocap-kernel` is identified as a non-sink and
has none
- `node scripts/copy-patches.cjs` copies the patch to
`packages/kernel-utils/patches/` only; the generated dir does not appear
in `git status` (gitignored)
- The patch is applied on `yarn install` (verified via `grep
Object.defineProperty
node_modules/@chainsafe/libp2p-yamux/dist/src/decode.js`)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes install and publish-time behavior by introducing
`patch-package`, root `patches/`, and new Yarn constraint enforcement,
which could break dependency installation or package publishing if
misconfigured. Runtime code changes are limited to applying an existing
third-party patch.
>
> **Overview**
> Moves SES/lockdown dependency patching from Yarn’s `patch:` protocol
to `patch-package`, making the repo root `patches/` directory the single
source of truth and applying patches automatically on root
`postinstall`.
>
> Adds publish-time patch shipping: `scripts/copy-patches.cjs` computes
“sink” packages from the workspace dependency graph and copies root
patches into each sink’s `patches/` directory during `prepack`
(generated dirs are gitignored). Yarn constraints are extended to forbid
`patch:` in production deps, enforce correct sink/non-sink patch
shipping config (e.g., `kernel-utils` ships `patches/`, declares
`patch-package` peer dep, and runs a guarded `postinstall`), and error
on orphaned root patches.
>
> Also updates a few package dependencies (e.g.,
`@metamask/snaps-utils`, `@metamask/design-system-react`) and
regenerates `yarn.lock` accordingly.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
0de6bb7. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 68dd2dc commit e335251
17 files changed
Lines changed: 770 additions & 114 deletions
File tree
- .yarn/patches
- packages
- brow-2-brow
- kernel-browser-runtime
- kernel-ui
- kernel-utils
- ocap-kernel
- patches
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| |||
Lines changed: 0 additions & 41 deletions
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
69 | 93 | | |
70 | 94 | | |
71 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
129 | | - | |
| 130 | + | |
| 131 | + | |
130 | 132 | | |
131 | 133 | | |
132 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
13 | 29 | | |
14 | 30 | | |
15 | 31 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| |||
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| |||
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
101 | | - | |
| 103 | + | |
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
| |||
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| 158 | + | |
156 | 159 | | |
157 | 160 | | |
158 | 161 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
13 | 41 | | |
14 | 42 | | |
15 | 43 | | |
0 commit comments