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
60
61
62
63
64
65
66
67
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
93
94
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
use crate::lockfile::package::PackageColumns as _;
use bstr::BStr;
use bun_ast::{Loc, Log};
use bun_core::ZBox;
use bun_core::{Global, Output};
use bun_core::{ZStr, strings};
use bun_paths::{self as path, PathBuffer};
use bun_resolver::fs::FileSystem;
use bun_semver::String as SemverString;
use bun_sys::{self as sys, Fd, FdExt};
use bun_threading::IntrusiveWorkTask as _;
use bun_threading::thread_pool::{Batch, Node as ThreadPoolNode, Task as ThreadPoolTask};
use crate::package_install::PackageInstall;
use crate::package_manager;
use crate::{
DependencyID, PackageID, PackageManager, bun_hash_tag, lockfile::Package,
resolution::Resolution,
};
// Thin re-exports (mirroring Zig `pub const X = @import(...)` lines).
pub use crate::lockfile::PatchedDep;
pub use crate::resolution::Resolution as ResolutionExport;
pub use crate::{
DependencyID as DependencyIDExport, PackageID as PackageIDExport,
PackageInstall as PackageInstallExport, bun_hash_tag as bun_hash_tag_export,
};
// TODO(port): the Zig file re-exports these under the same names; Rust cannot re-export and `use`
// the same identifier twice in one module without aliasing. Collapse the duplicate
// `*Export` aliases above once module layout is settled.
bun_output::declare_scope!(InstallPatch, visible);
/// Length of the hex representation of `u64::MAX` (i.e. 16).
pub(crate) const MAX_HEX_HASH_LEN: usize = const_format::formatcp!("{:x}", u64::MAX).len();
pub(crate) const MAX_BUNTAG_HASH_BUF_LEN: usize = MAX_HEX_HASH_LEN + bun_hash_tag.len() + 1;
pub(crate) type BuntagHashBuf = [u8; MAX_BUNTAG_HASH_BUF_LEN];
// `std.fs.Dir` aliases on `PatchTask`/`ApplyPatch` are *borrowed views* of the
// `PackageManager`-owned cache/temp directory descriptors. Store the raw `Fd`
// so the task's drop never closes a descriptor it doesn't own. Use
// `Dir::borrow(&fd)` where a `&Dir` is needed.
pub struct PatchTask {
/// BACKREF (Zig: `*PackageManager`). Stored as `BackRef` because the task
/// is held via raw pointer through the intrusive thread-pool queue while
/// the manager is concurrently borrowed `&mut` on the main thread; a `&`
/// reference here would alias that exclusive borrow under Stacked Borrows.
/// Constructed via `BackRef::new_mut` so the underlying pointer carries
/// write provenance for `PackageManager::wake_raw(*mut Self)`, which
/// writes the event-loop wake flag.
pub manager: bun_ptr::BackRef<PackageManager>,
/// Borrowed view of the manager's temp directory fd (see comment at top of file).
pub tempdir: Fd,
pub project_dir: &'static [u8],
pub callback: Callback,
pub task: ThreadPoolTask,
pub pre: bool,
pub next: bun_threading::Link<PatchTask>,
}
bun_threading::intrusive_work_task!(PatchTask, task);
// SAFETY: `next` is the sole intrusive link for `UnboundedQueue(PatchTask, .next)`.
unsafe impl bun_threading::Linked for PatchTask {
#[inline]
unsafe fn link(item: *mut Self) -> *const bun_threading::Link<Self> {
// SAFETY: `item` is valid and properly aligned per `UnboundedQueue` contract.
unsafe { core::ptr::addr_of!((*item).next) }
}
}
#[derive(strum::IntoStaticStr)]
pub enum Callback {
#[strum(serialize = "calc_hash")]
CalcHash(CalcPatchHash),
#[strum(serialize = "apply")]
Apply(ApplyPatch),
}
impl Callback {
/// Zig: `@tagName(self.callback)`.
#[inline]
pub(crate) fn tag_name(&self) -> &'static str {
<&'static str>::from(self)
}
#[inline]
pub(crate) fn is_calc_hash(&self) -> bool {
matches!(self, Callback::CalcHash(_))
}
#[inline]
pub(crate) fn is_apply(&self) -> bool {
matches!(self, Callback::Apply(_))
}
/// Zig: `&self.callback.apply`. Panics if the active variant is not `Apply`.
#[inline]
pub(crate) fn apply_mut(&mut self) -> &mut ApplyPatch {
match self {
Callback::Apply(a) => a,
_ => unreachable!("PatchTask.callback is not .apply"),
}
}
}
pub struct CalcPatchHash {
pub patchfile_path: Box<[u8]>,
pub name_and_version_hash: u64,
pub state: Option<EnqueueAfterState>,
pub result: Option<u64>,
pub logger: Log,
}
pub struct EnqueueAfterState {
pub pkg_id: PackageID,
pub dependency_id: DependencyID,
pub url: Box<[u8]>,
}
pub struct ApplyPatch {
pub pkg_id: PackageID,
pub patch_hash: u64,
pub name_and_version_hash: u64,
pub patchfilepath: Box<[u8]>,
pub pkgname: SemverString,
/// Borrowed view of the manager's cache directory fd (see comment at top of file).
pub cache_dir: Fd,
pub cache_dir_subpath: ZBox,
pub cache_dir_subpath_without_patch_hash: ZBox,
/// this is non-null if this was called before a Task, for example extracting
pub task_id: Option<TaskId>,
pub install_context: Option<InstallContext>,
// dependency_id: ?struct = null,
pub logger: Log,
}
pub struct InstallContext {
pub dependency_id: DependencyID,
pub tree_id: crate::lockfile::tree::Id,
pub path: Vec<u8>,
}
impl PatchTask {
/// Destroy a heap-allocated `PatchTask` previously created by
/// `new_calc_patch_hash` / `new_apply_patch_hash`.
///
/// PORT NOTE: Zig `deinit` freed each owned field then `bun.destroy(this)`. In Rust the
/// owned fields (`Box<[u8]>`, `Vec<u8>`, `Log`, `Option<...>`) drop automatically, so no
/// `impl Drop` body is needed. Per PORTING.md, `deinit` is never exposed as the public API;
/// because `PatchTask` is held via raw pointer through the intrusive `next`/thread-pool
/// queue, the named reclaim point is `unsafe fn destroy`. Cross-file callers map
/// `pt.deinit()` → `unsafe { PatchTask::destroy(pt) }`.
///
/// # Safety
/// `this` must have been produced by `heap::alloc` in the `new_*` constructors below and
/// ownership must be returned here exactly once.
pub unsafe fn destroy(this: *mut Self) {
// TODO: how to deinit `this.callback.calc_hash.network_task` (carried over from Zig)
// SAFETY: caller contract — `this` was produced by `heap::into_raw` in
// `new_calc_patch_hash`/`new_apply_patch_hash` and is reclaimed exactly once.
drop(unsafe { bun_core::heap::take(this) });
}
/// # Safety
/// Only invoked by `ThreadPool` via the `callback` fn-pointer registered in
/// `new_calc_patch_hash` / `new_apply_patch_hash`. `task` must be live and
/// point at the `task` field of a heap-allocated `PatchTask`, with the pool
/// granting exclusive access for the duration of the call.
pub unsafe fn run_from_thread_pool(task: *mut ThreadPoolTask) {
// SAFETY: thread-pool callback contract — `task` points to the `task`
// field of a live `PatchTask` (set at construction); the pool runs
// each task at most once with exclusive access for the call.
let patch_task = unsafe { &mut *PatchTask::from_task_ptr(task) };
patch_task.run_from_thread_pool_impl();
}
pub fn run_from_thread_pool_impl(&mut self) {
bun_output::scoped_log!(
InstallPatch,
"runFromThreadPoolImpl {}",
<&'static str>::from(&self.callback)
);
// PORT NOTE: Zig used nested `defer { defer wake(); push(this); }`. There are no early
// returns in the body, so the equivalent ordering (body → push → wake) is inlined below.
match &mut self.callback {
Callback::CalcHash(_) => {
let result = self.calc_hash();
if let Callback::CalcHash(ch) = &mut self.callback {
ch.result = result;
}
// PORT NOTE: reshaped for borrowck — `calc_hash` borrows `&mut self`, so we
// cannot hold a `&mut ch` across the call.
}
Callback::Apply(_) => {
bun_core::handle_oom(self.apply());
}
}
let mgr = self.manager.as_ptr();
// SAFETY: `self.manager` is a long-lived BACKREF (Zig `*PackageManager`);
// the worker thread only touches the lock-free `patch_task_queue` and the
// event-loop wake atomics, neither of which alias data the main thread
// holds an exclusive borrow on.
unsafe {
(*mgr)
.patch_task_queue
.push(core::ptr::NonNull::from(&mut *self));
PackageManager::wake_raw(mgr);
}
}
pub fn run_from_main_thread(
&mut self,
manager: &mut PackageManager,
log_level: LogLevel,
) -> Result<(), bun_core::Error> {
// TODO(port): narrow error set
bun_output::scoped_log!(
InstallPatch,
"runFromThreadMainThread {}",
<&'static str>::from(&self.callback)
);
let pre = self.pre;
match &mut self.callback {
Callback::CalcHash(_) => {
let r = self.run_from_main_thread_calc_hash(manager, log_level);
if pre {
let _ = manager
.pending_pre_calc_hashes
.fetch_sub(1, core::sync::atomic::Ordering::Relaxed);
}
r?;
}
Callback::Apply(_) => {
self.run_from_main_thread_apply(manager);
if pre {
let _ = manager
.pending_pre_calc_hashes
.fetch_sub(1, core::sync::atomic::Ordering::Relaxed);
}
}
}
Ok(())
}
pub fn run_from_main_thread_apply(&mut self, manager: &mut PackageManager) {
let _ = manager; // autofix
let Callback::Apply(apply) = &mut self.callback else {
unreachable!()
};
if apply.logger.errors > 0 {
Output::err_generic(
"failed to apply patchfile ({})",
format_args!("{}", BStr::new(&apply.patchfilepath)),
);
let _ = apply
.logger
.print(std::ptr::from_mut(Output::error_writer()));
// PORT NOTE: Zig called `apply.logger.deinit()` here under `defer`. The `Log` is a
// field and will be dropped with the task; explicit early drop is skipped to avoid
// double-drop. If `Log::deinit` is reset-to-empty (idempotent), an explicit
// `apply.logger.clear()` could be restored here.
// TODO(port): confirm Log drop semantics
}
}
fn run_from_main_thread_calc_hash(
&mut self,
manager: &mut PackageManager,
log_level: LogLevel,
) -> Result<(), bun_core::Error> {
// TODO(port): narrow error set
// TODO only works for npm package
// need to switch on version.tag and handle each case appropriately
let Callback::CalcHash(calc_hash) = &mut self.callback else {
unreachable!()
};
let Some(hash) = calc_hash.result else {
if log_level != LogLevel::Silent {
if calc_hash.logger.has_errors() {
let _ = calc_hash
.logger
.print(std::ptr::from_mut(Output::error_writer()));
} else {
Output::err_generic(
"Failed to calculate hash for patch <b>{}<r>",
format_args!("{}", BStr::new(&calc_hash.patchfile_path)),
);
}
}
Global::crash();
};
let gop = bun_core::handle_oom(
manager
.lockfile
.patched_dependencies
.get_or_put(calc_hash.name_and_version_hash),
);
if gop.found_existing {
gop.value_ptr.set_patchfile_hash(Some(hash));
} else {
panic!("No entry for patched dependency, this is a bug in Bun.");
}
if let Some(state) = &calc_hash.state {
let url = &state.url;
let pkg_id = state.pkg_id;
let dep_id = state.dependency_id;
let pkg: Package = *manager.lockfile.packages.get(pkg_id as usize);
// PORT NOTE: `Package` is not `Copy` in the Rust port; capture the
// scalar fields we need after `determine_preinstall_state` consumes
// it (Zig's `packages.get()` returns by-value-copy).
let pkg_meta_id = pkg.meta.id;
let pkg_name = pkg.name;
let pkg_resolution_tag = pkg.resolution.tag;
let name_and_version_hash = calc_hash.name_and_version_hash;
let mut out_name_and_version_hash: Option<u64> = None;
let mut out_patchfile_hash: Option<u64> = None;
manager.set_preinstall_state(pkg_meta_id, PreinstallState::Unknown);
match manager.determine_preinstall_state(
&pkg,
&mut out_name_and_version_hash,
&mut out_patchfile_hash,
) {
PreinstallState::Done => {
// patched pkg in folder path, should now be handled by PackageInstall.install()
bun_output::scoped_log!(
InstallPatch,
"pkg: {} done",
BStr::new(pkg_name.slice(&manager.lockfile.buffers.string_bytes))
);
}
PreinstallState::Extract => {
bun_output::scoped_log!(
InstallPatch,
"pkg: {} extract",
BStr::new(pkg_name.slice(&manager.lockfile.buffers.string_bytes))
);
// SAFETY: this arm is the `.npm` extract path; the
// resolution union's active variant is `npm` (see the
// `pkg_resolution_tag` switch below — Zig only reads
// `pkg.resolution.value.npm.version` here, line 183).
let pkg_npm_version = unsafe {
manager.lockfile.packages.items_resolution()[pkg_id as usize]
.value
.npm
.version
};
let task_id =
TaskId::for_npm_package(manager.lockfile.str(&pkg_name), pkg_npm_version);
debug_assert!(!manager.network_dedupe_map.contains_key(&task_id));
let is_required = manager.lockfile.buffers.dependencies[dep_id as usize]
.behavior
.is_required();
let pkg_again: Package = *manager.lockfile.packages.get(pkg_id as usize);
let network_task: *mut crate::NetworkTask =
package_manager::generate_network_task_for_tarball(
manager,
// TODO: not just npm package
task_id,
url,
is_required,
dep_id,
&pkg_again,
Some(name_and_version_hash),
match pkg_resolution_tag {
crate::resolution_real::Tag::Npm => {
Authorization::AllowAuthorization
}
_ => Authorization::NoAuthorization,
},
)?
.unwrap_or_else(|| unreachable!());
if manager.get_preinstall_state(pkg_meta_id) == PreinstallState::Extract {
manager.set_preinstall_state(pkg_meta_id, PreinstallState::Extracting);
package_manager::enqueue_network_task(manager, network_task);
}
}
PreinstallState::ApplyPatch => {
bun_output::scoped_log!(
InstallPatch,
"pkg: {} apply patch",
BStr::new(pkg_name.slice(&manager.lockfile.buffers.string_bytes))
);
let patch_task = PatchTask::new_apply_patch_hash(
manager,
pkg_meta_id,
hash,
name_and_version_hash,
);
if manager.get_preinstall_state(pkg_meta_id) == PreinstallState::ApplyPatch {
manager.set_preinstall_state(pkg_meta_id, PreinstallState::ApplyingPatch);
// SAFETY: `patch_task` is a fresh `heap::alloc` from
// `new_apply_patch_hash`; ownership transfers to the fifo.
unsafe { package_manager::enqueue_patch_task(manager, patch_task) };
}
}
_ => {}
}
}
Ok(())
}
// 1. Parse patch file
// 2. Create temp dir to do all the modifications
// 3. Copy un-patched pkg into temp dir
// 4. Apply patches to pkg in temp dir
// 5. Add bun tag for patch hash
// 6. rename() newly patched pkg to cache
pub fn apply(&mut self) -> Result<(), bun_alloc::AllocError> {
let Callback::Apply(patch) = &mut self.callback else {
unreachable!()
};
let log = &mut patch.logger;
bun_output::scoped_log!(InstallPatch, "apply patch task");
// bun.assert(this.callback == .apply) — enforced by the match above.
let dir = self.project_dir;
let patchfile_path = &patch.patchfilepath;
let mut absolute_patchfile_path_buf = PathBuffer::uninit();
// 1. Parse the patch file
let absolute_patchfile_path = path::resolve_path::join_z_buf::<path::platform::Auto>(
&mut absolute_patchfile_path_buf.0,
&[dir, patchfile_path],
);
// TODO: can the patch file be anything other than utf-8?
let patchfile_txt =
match sys::File::read_from(Fd::cwd(), absolute_patchfile_path.as_bytes()) {
sys::Result::Ok(txt) => txt,
sys::Result::Err(e) => {
log.add_sys_error(&e, format_args!("failed to read patchfile"));
return Ok(());
}
};
// PORT NOTE: `defer this.manager.allocator.free(patchfile_txt)` — `patchfile_txt` is owned
// (`Vec<u8>`/`Box<[u8]>`) and drops at end of scope.
let patchfile = match crate::patch_parser::parse_patch_file(&patchfile_txt) {
Ok(p) => p,
Err(e) => {
log.add_error_fmt_opts(
format_args!("failed to parse patchfile: {}", <&'static str>::from(e)),
Default::default(),
);
return Ok(());
}
};
// PORT NOTE: `defer patchfile.deinit(bun.default_allocator)` — handled by Drop.
// 2. Create temp dir to do all the modifications
let mut tmpname_buf = [0u8; 1024];
let tempdir_name =
match FileSystem::tmpname(b"tmp", &mut tmpname_buf, bun_core::fast_random()) {
Ok(name) => name,
// max len is 1+16+1+8+3, well below 1024
Err(_no_space_left) => unreachable!(),
};
let system_tmpdir = self.tempdir;
let pkg_name = patch.pkgname;
let dummy_node_modules = crate::package_installer::NodeModulesFolder {
path: Vec::<u8>::new(),
tree_id: 0,
};
let (resolution_label, resolution_tag) = {
// TODO: fix this threadsafety issue.
// PORT NOTE: not `self.manager()` — `&mut self.callback` is live.
// BACKREF; the lockfile is read-only while apply tasks run
// off-thread (same contract as the Zig pointer dereference here).
let manager = self.manager.get();
let resolution: &Resolution =
&manager.lockfile.packages.items_resolution()[patch.pkg_id as usize];
let mut label = Vec::<u8>::new();
use std::io::Write as _;
write!(
&mut label,
"{}",
resolution.fmt(
manager.lockfile.buffers.string_bytes.as_slice(),
bun_core::fmt::PathSep::Posix,
)
)
.expect("OOM");
(label, resolution.tag)
};
// PORT NOTE: `defer allocator.free(resolution_label)` — Vec drops at scope end.
// 3. copy the unpatched files into temp dir
let cache_dir_subpath_z: &ZStr = patch.cache_dir_subpath_without_patch_hash.as_zstr();
// PORT NOTE: borrowck — `tempdir_name` borrows `tmpname_buf` mutably, but
// `PackageInstall` also wants `&mut tmpname_buf[..]` for
// `destination_dir_subpath_buf`. Zig aliased the two; `PackageInstall`
// assumes `destination_dir_subpath` is a prefix slice *into*
// `destination_dir_subpath_buf` (see `verifyGitResolution` /
// `verifyPackageJSONNameAndVersion`). Rust can't express that aliasing
// with `&ZStr` + `&mut [u8]`, so use a separate buffer but mirror the
// prefix bytes so the invariant holds for any future call that reaches
// those paths.
let mut dest_subpath_buf = [0u8; 1024];
dest_subpath_buf[..tempdir_name.len() + 1]
.copy_from_slice(tempdir_name.as_bytes_with_nul());
// PORT NOTE: not `self.manager()` — `&mut self.callback` is live.
// BACKREF — read-only lockfile access; same contract as the Zig
// pointer dereference here.
let lockfile = &self.manager.get().lockfile;
let mut pkg_install = PackageInstall {
cache_dir: patch.cache_dir,
cache_dir_subpath: cache_dir_subpath_z,
destination_dir_subpath: tempdir_name,
destination_dir_subpath_buf: &mut dest_subpath_buf[..],
patch: None,
progress: None,
package_name: pkg_name,
package_version: &resolution_label,
file_count: 0,
// dummy value
node_modules: &dummy_node_modules,
lockfile,
};
match pkg_install.install(
true,
sys::Dir::borrow(&system_tmpdir),
InstallMethod::Copyfile,
resolution_tag,
) {
InstallResult::Success => {}
InstallResult::Failure(reason) => {
log.add_error_fmt_opts(
format_args!(
"{} while executing step: {}",
reason.err.name(),
BStr::new(reason.step.name())
),
Default::default(),
);
return Ok(());
}
}
{
let patch_pkg_dir = match sys::openat(
system_tmpdir,
tempdir_name,
sys::O::RDONLY | sys::O::DIRECTORY,
0,
) {
sys::Result::Ok(fd) => fd,
sys::Result::Err(e) => {
log.add_sys_error(
&e,
format_args!(
"failed trying to open temporary dir to apply patch to package: {}",
BStr::new(&resolution_label)
),
);
return Ok(());
}
};
let _close_guard = scopeguard::guard(patch_pkg_dir, |fd| fd.close());
// 4. apply patch
if let Some(e) = patchfile.apply(patch_pkg_dir) {
log.add_error_fmt_opts(
format_args!("failed applying patch file: {}", e),
Default::default(),
);
return Ok(());
}
// 5. Add bun tag
let bun_tag_prefix = bun_hash_tag;
let mut buntagbuf: BuntagHashBuf = [0; MAX_BUNTAG_HASH_BUF_LEN];
buntagbuf[..bun_tag_prefix.len()].copy_from_slice(bun_tag_prefix);
let hashlen = {
use std::io::Write as _;
let mut cursor = &mut buntagbuf[bun_tag_prefix.len()..];
let before = cursor.len();
write!(&mut cursor, "{:x}", patch.patch_hash).expect("unreachable");
before - cursor.len()
};
buntagbuf[bun_tag_prefix.len() + hashlen] = 0;
let buntag_zstr = ZStr::from_buf(&buntagbuf, bun_tag_prefix.len() + hashlen);
if let Err(e) = sys::File::write_file(patch_pkg_dir, buntag_zstr, b"") {
log.add_error_fmt_opts(
format_args!(
"failed adding bun tag: {}",
e.with_path(buntag_zstr.as_bytes())
),
Default::default(),
);
return Ok(());
}
}
// 6. rename to cache dir
let mut path_in_tmpdir_buf = PathBuffer::uninit();
let path_in_tmpdir = path::resolve_path::join_z_buf::<path::platform::Auto>(
&mut path_in_tmpdir_buf.0,
&[
tempdir_name.as_bytes(),
// tempdir_name,
],
);
let cache_dir_subpath_z: &ZStr = patch.cache_dir_subpath.as_zstr();
if let Err(e) = sys::renameat_concurrently(
system_tmpdir,
path_in_tmpdir,
patch.cache_dir,
cache_dir_subpath_z,
sys::RenameOptions {
move_fallback: true,
..Default::default()
},
) {
log.add_error_fmt_opts(
format_args!(
"renaming changes to cache dir: {}",
e.with_path(cache_dir_subpath_z.as_bytes())
),
Default::default(),
);
return Ok(());
}
Ok(())
}
pub fn calc_hash(&mut self) -> Option<u64> {
let Callback::CalcHash(calc_hash) = &mut self.callback else {
unreachable!()
};
let log = &mut calc_hash.logger;
let dir = self.project_dir;
let patchfile_path = &calc_hash.patchfile_path;
let mut absolute_patchfile_path_buf = PathBuffer::uninit();
// parse the patch file
let absolute_patchfile_path = path::resolve_path::join_z_buf::<path::platform::Auto>(
&mut absolute_patchfile_path_buf.0,
&[dir, patchfile_path],
);
let stat: sys::Stat = match sys::stat(absolute_patchfile_path) {
sys::Result::Err(e) => {
if e.get_errno() == sys::Errno::ENOENT {
// PORT NOTE: not `self.manager()` — `&mut self.callback` is live.
// BACKREF — read-only lockfile access on the worker thread;
// same contract as the Zig pointer dereference here.
let manager = self.manager.get();
log.add_error_fmt(
None,
Loc::EMPTY,
format_args!(
"Couldn't find patch file: '{}'\n\nTo create a new patch file run:\n\n <cyan>bun patch {}<r>",
BStr::new(&calc_hash.patchfile_path),
BStr::new(
manager
.lockfile
.patched_dependencies
.get(&calc_hash.name_and_version_hash)
.unwrap()
.path
.slice(&manager.lockfile.buffers.string_bytes)
),
),
);
return None;
}
log.add_warning_fmt(
None,
Loc::EMPTY,
format_args!(
"patchfile <b>{}<r> is empty, please restore or delete it.",
BStr::new(absolute_patchfile_path.as_bytes())
),
);
return None;
}
sys::Result::Ok(s) => s,
};
let size: u64 = u64::try_from(stat.st_size).expect("int cast");
if size == 0 {
log.add_error_fmt(
None,
Loc::EMPTY,
format_args!(
"patchfile <b>{}<r> is empty, please restore or delete it.",
BStr::new(absolute_patchfile_path.as_bytes())
),
);
return None;
}
let file = match sys::File::open(absolute_patchfile_path, sys::O::RDONLY, 0) {
sys::Result::Err(e) => {
log.add_error_fmt(
None,
Loc::EMPTY,
format_args!("failed to open patch file: {}", e),
);
return None;
}
sys::Result::Ok(f) => f,
};
let mut hasher = bun_sha_hmac::sha::hashers::SHA1::init();
const CHUNK_SIZE: usize = 64 * 1024;
let mut chunk = vec![0u8; CHUNK_SIZE];
let mut offset: u64 = 0;
while offset < size {
let n = match file.pread_all(&mut chunk[..], offset) {
sys::Result::Ok(n) => n,
sys::Result::Err(e) => {
log.add_error_fmt(
None,
Loc::EMPTY,
format_args!(
"failed to read from patch file: {} ({})",
e,
BStr::new(absolute_patchfile_path.as_bytes())
),
);
return None;
}
};
if n == 0 {
break;
}
hasher.update(&chunk[..n]);
offset += n as u64;
}
let mut digest = [0u8; bun_sha_hmac::sha::hashers::SHA1::DIGEST];
hasher.r#final(&mut digest);
Some(u64::from_le_bytes(digest[0..8].try_into().unwrap()))
}
pub fn notify(&mut self) {
// PORT NOTE: Zig `defer this.manager.wake()` then `push`. No early returns; inline order.
let mgr = self.manager.as_ptr();
// SAFETY: `self.manager` is a long-lived BACKREF (Zig `*PackageManager`);
// only touches the lock-free queue and event-loop wake atomics.
unsafe {
(*mgr)
.patch_task_queue
.push(core::ptr::NonNull::from(&mut *self));
PackageManager::wake_raw(mgr);
}
}
pub fn schedule(&mut self, batch: &mut Batch) {
batch.push(Batch::from(&raw mut self.task));
}
pub fn new_calc_patch_hash(
manager: &mut PackageManager,
name_and_version_hash: u64,
state: Option<EnqueueAfterState>,
) -> *mut PatchTask {
let patchdep = manager
.lockfile
.patched_dependencies
.get(&name_and_version_hash)
.unwrap_or_else(|| panic!("This is a bug"));
let patchfile_path: Box<[u8]> =
Box::from(patchdep.path.slice(&manager.lockfile.buffers.string_bytes));
// TODO(port): Zig used `dupeZ` (NUL-terminated). The field is typed `[]const u8` and only
// used as a byte slice, so `Box<[u8]>` without trailing NUL should be equivalent. Verify.
let tempdir = manager.get_temporary_directory().handle.fd();
let pt = Box::new(PatchTask {
tempdir,
callback: Callback::CalcHash(CalcPatchHash {
state,
patchfile_path,
name_and_version_hash,
result: None,
logger: Log::init(),
}),
manager: bun_ptr::BackRef::new_mut(manager),
project_dir: FileSystem::instance().top_level_dir(),
task: ThreadPoolTask {
node: ThreadPoolNode::default(),
callback: Self::run_from_thread_pool,
},
pre: false,
next: bun_threading::Link::new(),
});
bun_core::heap::into_raw(pt)
}
pub fn new_apply_patch_hash(
pkg_manager: &mut PackageManager,
pkg_id: PackageID,
patch_hash: u64,
name_and_version_hash: u64,
) -> *mut PatchTask {
let pkg_name = pkg_manager.lockfile.packages.items_name()[pkg_id as usize];
// PORT NOTE: borrowck — `compute_cache_dir_and_subpath` borrows `&mut PackageManager`
// while `pkg_name.slice(..)` and `resolution` borrow `pkg_manager.lockfile` immutably.
// Clone the slice/resolution out first.
let pkg_name_slice = pkg_name
.slice(&pkg_manager.lockfile.buffers.string_bytes)
.to_vec();
// PORT NOTE: `Resolution` is `Copy`; copy out so the lockfile borrow ends
// before `compute_cache_dir_and_subpath` reborrows `pkg_manager` mutably.
let resolution_clone: Resolution =
pkg_manager.lockfile.packages.items_resolution()[pkg_id as usize];
let mut folder_path_buf = PathBuffer::uninit();
let stuff = package_manager::compute_cache_dir_and_subpath(
pkg_manager,
&pkg_name_slice,
&resolution_clone,
&mut folder_path_buf,
Some(patch_hash),
);
let patchfilepath: Box<[u8]> = Box::from(
pkg_manager
.lockfile
.patched_dependencies
.get(&name_and_version_hash)
.unwrap()
.path
.slice(&pkg_manager.lockfile.buffers.string_bytes),
);
let cache_dir_subpath_bytes = stuff.cache_dir_subpath.as_bytes();
let patch_hash_idx = strings::index_of(cache_dir_subpath_bytes, b"_patch_hash=")
.unwrap_or_else(|| panic!("This is a bug in Bun."));
// need to dupe this as it's calculated using
// `PackageManager.cached_package_folder_name_buf` which may be modified
let cache_dir_subpath = ZBox::from_bytes(cache_dir_subpath_bytes);
let cache_dir_subpath_without_patch_hash =
ZBox::from_bytes(&cache_dir_subpath_bytes[..patch_hash_idx]);
let cache_dir = stuff.cache_dir;
let tempdir = pkg_manager.get_temporary_directory().handle.fd();
let pt = Box::new(PatchTask {
tempdir,
callback: Callback::Apply(ApplyPatch {
pkg_id,
patch_hash,
name_and_version_hash,
cache_dir,
patchfilepath,
pkgname: pkg_name,
logger: Log::init(),
cache_dir_subpath,
cache_dir_subpath_without_patch_hash,
task_id: None,
install_context: None,
}),
manager: bun_ptr::BackRef::new_mut(pkg_manager),
project_dir: FileSystem::instance().top_level_dir(),
task: ThreadPoolTask {
node: ThreadPoolNode::default(),
callback: Self::run_from_thread_pool,
},
pre: false,
next: bun_threading::Link::new(),
});
bun_core::heap::into_raw(pt)
}
}
// TODO(port): these enum/type references are placeholders for cross-file types that live in
// `bun_install`. Replace with the real paths once those modules are ported.
use crate::PreinstallState;
use crate::network_task::Authorization;
use crate::package_install::{InstallResult, Method as InstallMethod};
use crate::package_manager::Options::LogLevel;
use crate::package_manager_task::Id as TaskId;
// ported from: src/install/patch_install.zig