Commit 2ab74b0
authored
feat: strip patchedDependencies from the packed package.json (#497)
Part of native dependency patching
([npm/rfcs#862](npm/rfcs#862)). When packing a
`directory` spec (the `npm publish` / `npm pack` path), this strips a
top-level `patchedDependencies` field from the `package.json` written
**into the tarball**.
## Why
`patchedDependencies` declares project-local patches against installed
dependencies. It is honored only in a root manifest, so it is
meaningless to consumers of a published package and should never travel
through the registry. The published *packument* manifest is already
stripped in `libnpmpublish`; this closes the other half — the
`package.json` inside the tarball itself — so `npm pack --dry-run` and
the published tarball no longer carry the field. It pairs with the
npm-packlist change that excludes the patch files themselves; together
they guarantee a patched project publishes clean.
## How
`DirFetcher` packs the raw on-disk files via `tar.c`, so the tarball's
`package.json` is the literal file on disk — there is no manifest seam
to edit. The new `#tarOptions()`:
1. Reads the on-disk `package.json` (after `prepare`) via
`@npmcli/package-json`. If it has no `patchedDependencies`, returns the
existing options unchanged — **non-patched packs are byte-for-byte
identical to before**.
2. Otherwise deletes the field and re-serializes preserving the original
indent, newline, and key order (the indent/newline symbols
`@npmcli/package-json` attaches; `JSON.stringify` ignores them), writes
the stripped copy to a temp dir, and removes the temp dir if the write
fails.
3. Sets node-tar's `onWriteEntry` to redirect **only** the top-level
`package.json` entry's `absolute` at the stripped copy and fix its
`stat.size`/`nlink`. `onWriteEntry` runs before the header and the
file's hardlink check, so the override is honored; every other file is
untouched.
4. The temp dir is removed once the tar source stream emits
`end`/`error`, so it outlives content consumption.
No behavior change for any package without `patchedDependencies`.
## References
Part of
- npm/rfcs#862
Related to
- npm/cli#9439
- npm/npm-packlist#2911 parent 1f5f131 commit 2ab74b0
2 files changed
Lines changed: 117 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
| |||
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
81 | | - | |
82 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
83 | 92 | | |
84 | 93 | | |
85 | 94 | | |
86 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
87 | 142 | | |
88 | 143 | | |
89 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 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 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
0 commit comments