Skip to content

Commit 16ff159

Browse files
mikolalysenkoclaude
andcommitted
Parse each npm lock once in the hosted rewrite (#711, #993)
The #711 user-patched gate fully parsed every present npm lock whose text contains `"patched"` (every lockfileVersion 4 lock), and `rewrite_one_npm_lock` then parsed the same text again, so each large lock was parsed twice per hosted run. `rewrite_npm_lock` now parses each present lock once, builds the gate from that parse, and hands the parsed value to `rewrite_one_npm_lock` (an unparseable lock still warns `redirect_npm_lock_unparseable`). No behavior change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 171945d commit 16ff159

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

‎crates/socket-patch-core/src/patch/redirect/lock_index_equivalence_tests.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ fn indexed_npm_lock_rewrite_matches_golden() {
163163
let refs: Vec<&DepOverride> = deps.iter().collect();
164164
for lockfile in ["package-lock.json", "npm-shrinkwrap.json"] {
165165
let mut got = RewriteResult::default();
166-
rewrite_one_npm_lock(&text, lockfile, &refs, &NpmOverrides::default(), &mut got);
166+
rewrite_one_npm_lock(
167+
&text,
168+
parse_json_text(&text).ok(),
169+
lockfile,
170+
&refs,
171+
&NpmOverrides::default(),
172+
&mut got,
173+
);
167174
record(&(&text, lockfile, &deps), &got);
168175
edits += got.edits.len();
169176
codes.extend(got.warnings.iter().map(|w| w.code.clone()));

‎crates/socket-patch-core/src/patch/redirect/mod.rs‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,19 +1109,23 @@ fn rewrite_npm_lock(
11091109
// `patchedDependencies` key, or the `"patched": {integrity, path}`
11101110
// record npm writes on the lock entry (lockfileVersion 4). npm applies
11111111
// that diff to every install of the entry, so rewriting a sibling lock
1112-
// would still stack the user's diff on the hosted bytes. Only a lock
1113-
// that spells `"patched"` at all is parsed here, so the common case
1114-
// costs one substring scan.
1112+
// would still stack the user's diff on the hosted bytes. Each present
1113+
// lock is parsed ONCE, here, and the parse is handed to its rewrite;
1114+
// only a lock that spells `"patched"` at all is walked for the gate.
11151115
let user_patched = crate::vendor::bun_lock_text::patched_dependency_keys(
11161116
files.get("package.json").map(String::as_str),
11171117
None,
11181118
);
1119-
let lock_patched: Vec<(String, String, &str, String)> = present
1119+
let parsed: Vec<(&str, Option<Value>)> = present
11201120
.iter()
1121-
.filter(|lockfile| files[**lockfile].contains("\"patched\""))
1122-
.filter_map(|lockfile| Some((*lockfile, parse_json_text(&files[*lockfile]).ok()?)))
1121+
.map(|lockfile| (*lockfile, parse_json_text(&files[*lockfile]).ok()))
1122+
.collect();
1123+
let lock_patched: Vec<(String, String, &str, String)> = parsed
1124+
.iter()
1125+
.filter(|(lockfile, _)| files[*lockfile].contains("\"patched\""))
1126+
.filter_map(|(lockfile, lock)| Some((*lockfile, lock.as_ref()?)))
11231127
.flat_map(|(lockfile, lock)| {
1124-
npm_lock_entries(&lock)
1128+
npm_lock_entries(lock)
11251129
.into_iter()
11261130
.filter(|e| e.section == NpmLockSection::Packages && e.is_dependency())
11271131
.filter(|e| e.value.get("patched").is_some_and(|p| !p.is_null()))
@@ -1167,9 +1171,10 @@ fn rewrite_npm_lock(
11671171
false
11681172
})
11691173
.collect();
1170-
for lockfile in &present {
1174+
for (lockfile, lock) in parsed {
11711175
rewrite_one_npm_lock(
1172-
&files[*lockfile],
1176+
&files[lockfile],
1177+
lock,
11731178
lockfile,
11741179
&npm,
11751180
&manifest_overrides,
@@ -1302,13 +1307,15 @@ impl From<NpmLockEntry<'_>> for NpmLockTarget {
13021307
/// the identical override rewrite (see the dual-lock note there).
13031308
fn rewrite_one_npm_lock(
13041309
content: &str,
1310+
lock: Option<Value>,
13051311
lockfile: &str,
13061312
npm: &[&DepOverride],
13071313
manifest_overrides: &NpmOverrides,
13081314
result: &mut RewriteResult,
13091315
) {
1310-
// npm reads past a leading UTF-8 BOM; so do we.
1311-
let Ok(mut lock) = parse_json_text(content) else {
1316+
// `lock` is `content` parsed (npm reads past a leading UTF-8 BOM; so
1317+
// does that parse), `None` when it is not JSON.
1318+
let Some(mut lock) = lock else {
13121319
// A corrupt lockfile is strictly worse than a missing one (which
13131320
// warns in the caller) — never skip the whole npm redirect silently.
13141321
result.warnings.push(RewriteWarning {

0 commit comments

Comments
 (0)