Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
1992 lines (1850 loc) · 75.4 KB
/
Copy pathmod.rs
File metadata and controls
1992 lines (1850 loc) · 75.4 KB
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 VKrishna04
// SPDX-License-Identifier: Apache-2.0
// Package manager adapter trait and registry.
//
// This module defines the [`PackageManager`] trait that all ecosystem adapters
// must implement. It also provides helper functions for adapter detection and
// directory size calculation.
//
// ## Adding a New Adapter
//
// 1. Create a new file in `src/adapters/` (e.g., `maven.rs`)
// 2. Implement the [`PackageManager`] trait
// 3. Register it in [`get_all_adapters()`]
// 4. Add tests
//
// See [../../docs/ADDING_ADAPTERS.md] for a detailed guide.
pub mod bun;
pub mod bundler;
pub mod cabal;
pub mod cargo_adapter;
pub mod cmake_build;
pub mod cocoapods;
pub mod cocos;
pub mod composer;
pub mod dart;
pub mod defold;
pub mod deno;
pub mod dotnet_build;
pub mod go;
pub mod godot;
pub mod gradle;
pub mod maven;
pub mod mix;
pub mod mix_build;
pub mod npm;
pub mod pdm;
pub mod pipenv;
pub mod pixi;
pub mod pnpm;
pub mod poetry;
pub mod pycache;
pub mod sbt;
pub mod stack;
pub mod swift;
pub mod terraform;
pub mod unity;
pub mod unreal;
pub mod uv;
pub mod vcpkg;
pub mod venv;
pub mod yarn;
pub mod zig;
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use anyhow::{Context as _, Result};
use walkdir::WalkDir;
/// Information about a bloat directory that can be pruned.
#[derive(Debug, Clone)]
pub struct BloatDir {
/// Human-readable name (e.g., "node_modules").
pub name: String,
/// Full path to the bloat directory.
pub path: PathBuf,
/// Bytes that deleting this directory actually gives back to the disk.
pub size_bytes: u64,
/// Bytes reachable through hardlinks from outside this directory — pnpm's and
/// bun's store links. Deleting the directory does not free these; the store
/// keeps them. Zero for managers that copy instead of link.
pub shared_bytes: u64,
}
impl fmt::Display for BloatDir {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.path.display())
}
}
/// Packages sitting in a manager's environment directory that its lockfile does not
/// record — installs a post-prune restore would not bring back.
#[derive(Debug, Clone)]
pub struct DriftReport {
/// The environment directory that drifted (e.g. `.venv`, `node_modules`).
pub directory: String,
/// The unrecorded package names, sorted.
pub unrecorded: Vec<String>,
/// The command that writes them into the lockfile.
pub record_command: &'static str,
}
/// The core trait that every package manager adapter must implement.
///
/// Each adapter is responsible for:
/// - **Detecting** whether it applies to a given project directory
/// - **Listing** the bloat directories it manages
/// - **Enforcing** lockfile consistency before deletion
/// - **Restoring** dependencies from lockfiles
pub trait PackageManager: Send + Sync {
/// Human-readable name for this adapter (e.g., "npm", "pnpm", "uv").
fn name(&self) -> &'static str;
/// Check if this adapter applies to the given project directory.
///
/// Typically checks for the presence of a specific lockfile or config file.
fn detect(&self, project_path: &Path) -> bool;
/// List all bloat directories this adapter manages in the given project.
///
/// Only returns directories that actually exist on disk.
fn bloat_dirs(&self, project_path: &Path) -> Vec<BloatDir>;
/// Prove the lockfile can rebuild what is about to be deleted.
///
/// This is a **safety-critical** method. It MUST succeed before any bloat
/// directory is deleted. If this fails, deletion for this adapter is aborted.
///
/// See [`EnforcePolicy`] for the one rule every adapter follows.
fn enforce_lockfile(&self, project_path: &Path, policy: EnforcePolicy) -> Result<()>;
/// Restore dependencies from the lockfile (for `dev-prune restore`).
///
/// `timeout` is threaded explicitly for the same reason [`EnforcePolicy`] is: the
/// restore path used to burn the compiled-in default regardless of
/// `command_timeout_secs`, and a full `npm ci` on a large tree needs the raised
/// timeout far more often than a verify does.
fn restore(&self, project_path: &Path, timeout: std::time::Duration) -> Result<()>;
/// [`PackageManager::restore`], told the name the pruned directory had.
///
/// Most managers have exactly one possible directory name and ignore this. venv does
/// not: it prunes any folder carrying a `pyvenv.cfg` — `venv`, `env`, `my_env` — and
/// without the recorded name it would rebuild the environment as `.venv`, leaving
/// every activate script, IDE interpreter path and Makefile pointing at nothing.
/// `runtime` is the interpreter tag recorded when the directory was deleted (see
/// [`crate::config::PrunedDir::runtime`]). `None` means nothing was recorded, or the
/// caller has decided this machine cannot honour it; either way the manager should
/// fall back to whatever it would have used before.
fn restore_named(
&self,
project_path: &Path,
dir_name: &str,
runtime: Option<&str>,
timeout: std::time::Duration,
) -> Result<()> {
let _ = (dir_name, runtime);
self.restore(project_path, timeout)
}
/// The language runtime a bloat directory is built against, recorded at prune time.
///
/// Only the Python managers answer this. A `node_modules` is rebuilt by the same
/// `npm ci` whichever Node is installed, and cargo and go pin their toolchains in
/// files that are already in the repository — but a virtual environment is a
/// *copy* of one specific interpreter, and rebuilding it on a different one silently
/// changes which wheels resolve.
///
/// `dir_name` is the directory about to be deleted, relative to `project_path`.
fn runtime_tag(&self, project_path: &Path, dir_name: &str) -> Option<String> {
let _ = (project_path, dir_name);
None
}
/// The file this manager rebuilds its bloat directory from.
///
/// Two callers. Conflict resolution breaks ties between managers that share a bloat
/// directory — npm, pnpm, yarn and bun all own the same `node_modules` — by comparing
/// these files' timestamps. `devp doctor` names them, because a missing one is the
/// most common reason a project is not pruneable.
///
/// More than one entry means the manager accepts any of them (bun's binary and text
/// lockfiles). An empty slice means the manager has no single file to point at.
fn lockfiles(&self) -> &'static [&'static str] {
&[]
}
/// Installed-but-unrecorded packages, as data instead of a refusal.
///
/// The same comparison [`PackageManager::enforce_lockfile`] refuses a prune on,
/// surfaced early so `devp status --drift` can point at the problem before a prune
/// is ever attempted. Runs nothing and writes nothing. An empty answer means
/// "nothing detected", not "proven clean" — most managers have no cheap way to
/// compare and say nothing here.
fn drift(&self, project_path: &Path) -> Vec<DriftReport> {
let _ = project_path;
Vec::new()
}
/// Whether this adapter is inert until the user enables it in settings.
///
/// Adapters whose directory is compiler output answer `true` — cargo, gradle,
/// maven, swift, dart, mix_build, vcpkg, cmake_build, dotnet_build, godot,
/// unity, unreal, defold, cocos, zig, stack, cabal and sbt.
/// Theirs come back by
/// recompiling the project, which costs far
/// more than a dependency reinstall, so nobody should find them deleted without
/// having asked. The engine also holds them to the longer `build_idle_days` idle
/// window.
///
/// The test is what it costs to get the directory back, not whether a lockfile
/// exists: cargo has as good a lockfile as npm does, and `target/` still has to be
/// rebuilt from source.
fn opt_in(&self) -> bool {
false
}
}
/// Adapters that all manage `node_modules` and therefore cannot coexist.
///
/// Deno is deliberately not one of them. The four here are interchangeable — a
/// `node_modules` built by one is a `node_modules` the others would have built
/// differently, so exactly one of them owns the directory. Deno is not an alternative
/// to them: it detects on `deno.lock`, which a project either has or does not, and a
/// repository holding both a `deno.lock` and a `package-lock.json` genuinely uses both
/// tools. The prune pass deduplicates by path, so the shared `node_modules` is still
/// counted and deleted once.
const JS_MANAGERS: [&str; 4] = ["npm", "pnpm", "yarn", "bun"];
/// Bookkeeping files that each JavaScript manager writes into `node_modules` when it
/// installs. Finding one identifies the manager that actually produced the tree on
/// disk, which is stronger evidence than a lockfile's timestamp.
///
/// pnpm and yarn are checked before npm: a project migrated away from npm can still
/// carry npm's `.package-lock.json` inside a tree the new manager rebuilt around it.
/// Bun has no marker we rely on, so a bun conflict falls through to the later rules.
const JS_INSTALL_MARKERS: [(&str, &[&str]); 3] = [
("pnpm", &[".pnpm", ".modules.yaml"]),
("yarn", &[".yarn-state.yml", ".yarn-integrity"]),
("npm", &[".package-lock.json"]),
];
/// Returns all registered package manager adapters.
///
/// To add a new adapter, create your struct and add it to this list.
pub fn get_all_adapters() -> Vec<Box<dyn PackageManager>> {
vec![
Box::new(npm::Npm),
Box::new(pnpm::Pnpm),
Box::new(yarn::Yarn),
Box::new(bun::Bun),
Box::new(deno::Deno),
Box::new(uv::Uv),
Box::new(poetry::Poetry),
Box::new(pdm::Pdm),
Box::new(pipenv::Pipenv),
Box::new(venv::Venv),
Box::new(pycache::Pycache),
Box::new(pixi::Pixi),
Box::new(cargo_adapter::Cargo),
Box::new(go::Go),
Box::new(composer::Composer),
Box::new(bundler::Bundler),
Box::new(cocoapods::CocoaPods),
Box::new(mix::Mix),
Box::new(mix_build::MixBuild),
Box::new(gradle::Gradle),
Box::new(maven::Maven),
Box::new(swift::Swift),
Box::new(terraform::Terraform),
Box::new(dart::Dart),
Box::new(vcpkg::Vcpkg),
Box::new(cmake_build::CmakeBuild),
Box::new(dotnet_build::DotnetBuild),
Box::new(godot::Godot),
Box::new(unity::Unity),
Box::new(unreal::Unreal),
Box::new(defold::Defold),
Box::new(cocos::Cocos),
Box::new(zig::Zig),
Box::new(stack::Stack),
Box::new(cabal::Cabal),
Box::new(sbt::Sbt),
]
}
/// The names of the opt-in adapters the user has switched on, resolved once per
/// process from the registry settings.
///
/// Resolved here rather than threaded through every caller because `detect_adapters`
/// is the single funnel every command discovers projects through — gating detection
/// makes a disabled adapter invisible everywhere at once (status, stats, run, doctor),
/// instead of visible in one view and inert in another.
fn opt_in_enabled() -> &'static [String] {
static ENABLED: OnceLock<Vec<String>> = OnceLock::new();
ENABLED.get_or_init(|| {
crate::config::Registry::load()
.map(|r| {
let mut names = Vec::new();
if r.settings.enable_cargo {
names.push("cargo".to_string());
}
if r.settings.enable_gradle {
names.push("gradle".to_string());
}
if r.settings.enable_maven {
names.push("maven".to_string());
}
if r.settings.enable_swift {
names.push("swift".to_string());
}
if r.settings.enable_dart {
names.push("dart".to_string());
}
if r.settings.enable_mix_build {
names.push("mix_build".to_string());
}
if r.settings.enable_vcpkg {
names.push("vcpkg".to_string());
}
if r.settings.enable_cmake_build {
names.push("cmake_build".to_string());
}
if r.settings.enable_dotnet_build {
names.push("dotnet_build".to_string());
}
if r.settings.enable_godot {
names.push("godot".to_string());
}
if r.settings.enable_unity {
names.push("unity".to_string());
}
if r.settings.enable_unreal {
names.push("unreal".to_string());
}
if r.settings.enable_defold {
names.push("defold".to_string());
}
if r.settings.enable_cocos {
names.push("cocos".to_string());
}
if r.settings.enable_zig {
names.push("zig".to_string());
}
if r.settings.enable_stack {
names.push("stack".to_string());
}
if r.settings.enable_cabal {
names.push("cabal".to_string());
}
if r.settings.enable_sbt {
names.push("sbt".to_string());
}
names
})
.unwrap_or_default()
})
}
/// The adapters switched off by name in `disabled_adapters`, resolved once per process.
///
/// The mirror image of [`opt_in_enabled`], and read at the same single funnel for the
/// same reason: an adapter someone has turned off should not appear in `status`, be
/// counted by `stats`, or be probed for by `doctor` — "off" that still shows up
/// everywhere is not off.
fn user_disabled() -> &'static [String] {
static DISABLED: OnceLock<Vec<String>> = OnceLock::new();
DISABLED.get_or_init(|| {
crate::config::Registry::load()
.map(|r| {
r.settings
.disabled_adapters
.iter()
.map(|n| n.trim().to_ascii_lowercase())
.filter(|n| !n.is_empty())
.collect()
})
.unwrap_or_default()
})
}
/// Whether `name` is a real adapter name, for validating what the user typed.
pub fn is_adapter_name(name: &str) -> bool {
get_all_adapters().iter().any(|a| a.name() == name)
}
/// The adapters, grouped by the language they belong to.
///
/// A flat list of twenty names is a wall: the question a user actually has is "leave
/// Python alone" or "only Rust waits longer", and neither is expressible one checkbox
/// at a time. Order is the order the groups are shown in, which is roughly how common
/// they are rather than alphabetical — the four JavaScript managers are what most
/// people came for.
///
/// The one invariant, enforced by [`every_adapter_is_grouped_exactly_once`]: every
/// registered adapter appears here exactly once, and nothing appears here that is not
/// registered. A new adapter that is not added to a group would silently vanish from
/// the picker, which is the one place a user goes to find it.
pub const ADAPTER_GROUPS: &[(&str, &[&str])] = &[
("JavaScript", &["npm", "pnpm", "yarn", "bun", "deno"]),
(
"Python",
&["uv", "poetry", "pdm", "pipenv", "venv", "pycache", "pixi"],
),
("Rust", &["cargo"]),
("Go", &["go"]),
("JVM", &["gradle", "maven", "sbt"]),
("PHP", &["composer"]),
("Ruby", &["bundler"]),
("Swift & Objective-C", &["swift", "cocoapods"]),
("Elixir", &["mix", "mix_build"]),
("Infrastructure", &["terraform"]),
("Dart & Flutter", &["dart"]),
("C & C++", &["vcpkg", "cmake_build"]),
(".NET", &["dotnet_build"]),
("Zig", &["zig"]),
("Haskell", &["stack", "cabal"]),
(
"Game engines",
&["godot", "unity", "unreal", "defold", "cocos"],
),
];
/// The language group `name` belongs to, or `"Other"` if it somehow belongs to none.
///
/// The fallback exists so a missing entry degrades to a visible oddity in the picker
/// rather than an adapter that cannot be reached at all; the test is what actually
/// keeps [`ADAPTER_GROUPS`] complete.
pub fn adapter_group(name: &str) -> &'static str {
ADAPTER_GROUPS
.iter()
.find(|(_, names)| names.contains(&name))
.map(|(group, _)| *group)
.unwrap_or("Other")
}
/// Every adapter name, in registry order, for error messages and pickers.
pub fn all_adapter_names() -> Vec<&'static str> {
get_all_adapters().iter().map(|a| a.name()).collect()
}
/// The adapters that need their own `enable_*` switch as well as not being disabled.
///
/// Two switches govern these, and a picker that ticks one without saying so leaves the
/// user watching nothing happen.
pub fn opt_in_adapter_names() -> Vec<&'static str> {
get_all_adapters()
.iter()
.filter(|a| a.opt_in())
.map(|a| a.name())
.collect()
}
/// Detect which adapters apply to a given project directory.
///
/// Several adapters detecting at once is normal and supported — a directory holding
/// `package-lock.json`, `uv.lock` and `Cargo.toml` legitimately has three managers,
/// each owning a different bloat directory. Adapters that would fight over the *same*
/// directory are reduced to one first; see [`resolve_conflicts`].
pub fn detect_adapters(project_path: &Path) -> Vec<Box<dyn PackageManager>> {
detect_adapters_with(project_path, opt_in_enabled(), user_disabled())
}
/// Every package manager that claims this directory, whatever the user has switched off.
///
/// [`detect_adapters`] answers "what would a prune pass touch here", which is the right
/// question everywhere a prune pass is involved and the wrong one for `devp caches`: an
/// opt-in adapter that is off, or one named in `disabled_adapters`, still means the
/// project uses that manager and still means its download cache is what puts the project
/// back. Counting with the filtered detector would report a cargo cache as used by no
/// repository on a machine full of Rust, because `enable_cargo` happens to be off — and
/// that is the one answer that would get a cache cleared.
pub fn detect_all_adapters(project_path: &Path) -> Vec<Box<dyn PackageManager>> {
let opt_in: Vec<String> = opt_in_adapter_names()
.into_iter()
.map(str::to_string)
.collect();
detect_adapters_with(project_path, &opt_in, &[])
}
/// The opt-in adapters this directory uses that no switch has turned on.
///
/// A disabled opt-in adapter is invisible to the whole pass: it produces no bloat row,
/// so nothing downstream can say "cargo would have claimed a `target/` here". The dry-run
/// report calls this to say it once, at the moment the user is already deciding what the
/// next pass should do. Adapters named in `disabled_adapters` are excluded: that list is
/// an explicit "no", and recommending against it is nagging.
pub fn detect_dormant_opt_in(project_path: &Path) -> Vec<Box<dyn PackageManager>> {
let enabled = opt_in_enabled();
let disabled = user_disabled();
get_all_adapters()
.into_iter()
.filter(|a| a.opt_in())
.filter(|a| !enabled.iter().any(|n| n == a.name()))
.filter(|a| !disabled.iter().any(|n| n == a.name()))
.filter(|a| a.detect(project_path))
.collect()
}
/// The body of [`detect_adapters`], with the two user-configured lists passed in.
///
/// Split out so the tests can state which opt-in adapters are on instead of inheriting
/// whatever the machine running them has configured. `opt_in_enabled` and
/// `user_disabled` read the real registry through a process-wide `OnceLock`, so a test
/// calling `detect_adapters` directly asserted against the developer's own settings and
/// passed or failed depending on whether they had ever run the config wizard.
fn detect_adapters_with(
project_path: &Path,
opt_in: &[String],
disabled: &[String],
) -> Vec<Box<dyn PackageManager>> {
let mut detected: Vec<Box<dyn PackageManager>> = get_all_adapters()
.into_iter()
.filter(|adapter| !adapter.opt_in() || opt_in.iter().any(|n| n == adapter.name()))
.filter(|adapter| !disabled.iter().any(|n| n == adapter.name()))
.filter(|adapter| adapter.detect(project_path))
.collect();
resolve_conflicts(project_path, &mut detected);
detected
}
/// Reduce every set of adapters that shares a bloat directory down to a single owner.
fn resolve_conflicts(project_path: &Path, detected: &mut Vec<Box<dyn PackageManager>>) {
resolve_js_conflict(project_path, detected);
resolve_python_conflict(project_path, detected);
}
/// Reduce several JavaScript managers claiming the same `node_modules` down to one.
///
/// A directory carrying more than one JS lockfile is usually a half-finished migration
/// or a stray file nobody deleted. Running the wrong manager's `enforce_lockfile` would
/// rewrite a lockfile the project does not use, so pick deliberately, strongest signal
/// first:
///
/// 1. The `packageManager` field of `package.json` — the maintainers said so outright.
/// 2. The bookkeeping files inside `node_modules` — whoever built the tree we are about
/// to delete is the manager whose lockfile has to be able to rebuild it.
/// 3. The most recently written lockfile — a last resort when nothing else distinguishes
/// them.
fn resolve_js_conflict(project_path: &Path, detected: &mut Vec<Box<dyn PackageManager>>) {
if detected
.iter()
.filter(|a| JS_MANAGERS.contains(&a.name()))
.count()
< 2
{
return;
}
let winner = declared_package_manager(project_path)
.filter(|name| detected.iter().any(|a| a.name() == name))
.or_else(|| installed_manager(project_path, detected))
.or_else(|| newest_lockfile_owner(project_path, detected));
let Some(winner) = winner else { return };
detected.retain(|a| !JS_MANAGERS.contains(&a.name()) || a.name() == winner);
}
/// Give one lockfile-backed Python manager sole ownership of the environment directory.
///
/// uv, poetry, pdm and pipenv all point at the same in-project `.venv`, and so does the
/// plain-venv adapter. Running the wrong one's `enforce_lockfile` would sync a lockfile
/// the project does not use, so pick deliberately:
///
/// 1. Any of the four displaces `venv`. They have real lockfiles and rebuild the
/// environment exactly; `requirements.txt` cannot promise that, so it is the
/// fallback for projects none of them recognises.
/// 2. Between themselves — usually a half-finished migration — the one whose lockfile
/// is actually on disk built the tree we are about to delete. With several or none
/// present, the tie goes to whichever comes first in [`get_all_adapters()`].
const PYTHON_ENV_MANAGERS: [(&str, &str); 4] = [
("uv", "uv.lock"),
("poetry", "poetry.lock"),
("pdm", "pdm.lock"),
("pipenv", "Pipfile.lock"),
];
fn resolve_python_conflict(project_path: &Path, detected: &mut Vec<Box<dyn PackageManager>>) {
let claimants: Vec<(&str, &str)> = PYTHON_ENV_MANAGERS
.iter()
.copied()
.filter(|(name, _)| detected.iter().any(|a| a.name() == *name))
.collect();
let Some(&(first, _)) = claimants.first() else {
return;
};
detected.retain(|a| a.name() != "venv");
if claimants.len() < 2 {
return;
}
let winner = claimants
.iter()
.find(|(_, lockfile)| project_path.join(lockfile).exists())
.map_or(first, |(name, _)| *name);
detected.retain(|a| {
a.name() == winner
|| !PYTHON_ENV_MANAGERS
.iter()
.any(|(name, _)| *name == a.name())
});
}
/// The manager that actually installed the `node_modules` tree currently on disk.
fn installed_manager(project_path: &Path, detected: &[Box<dyn PackageManager>]) -> Option<String> {
let node_modules = project_path.join("node_modules");
if !node_modules.is_dir() {
return None;
}
JS_INSTALL_MARKERS
.iter()
.find(|(name, markers)| {
detected.iter().any(|a| a.name() == *name)
&& markers.iter().any(|m| node_modules.join(m).exists())
})
.map(|(name, _)| (*name).to_string())
}
/// Read the Corepack `packageManager` field (e.g. `"pnpm@9.1.0"`) from `package.json`.
fn declared_package_manager(project_path: &Path) -> Option<String> {
let raw = std::fs::read_to_string(project_path.join("package.json")).ok()?;
let json: serde_json::Value = serde_json::from_str(&raw).ok()?;
let declared = json.get("packageManager")?.as_str()?;
let name = declared.split('@').next().unwrap_or_default();
JS_MANAGERS
.iter()
.find(|m| **m == name)
.map(|m| (*m).to_string())
}
/// The detected JS manager whose lockfile has the most recent modification time.
fn newest_lockfile_owner(
project_path: &Path,
detected: &[Box<dyn PackageManager>],
) -> Option<String> {
detected
.iter()
.filter(|a| JS_MANAGERS.contains(&a.name()))
.filter_map(|a| {
let newest = a
.lockfiles()
.iter()
.filter_map(|f| std::fs::metadata(project_path.join(f)).ok()?.modified().ok())
.max()?;
Some((newest, a.name().to_string()))
})
// Ties keep the earlier adapter in `get_all_adapters()` order, so the choice is
// deterministic when two lockfiles share a timestamp.
.fold(None::<(std::time::SystemTime, String)>, |best, cur| {
match best {
Some(b) if b.0 >= cur.0 => Some(b),
_ => Some(cur),
}
})
.map(|(_, name)| name)
}
/// Calculate the total size of a directory recursively (in bytes).
pub fn dir_size(path: &Path) -> u64 {
if !path.exists() {
return 0;
}
WalkDir::new(path)
.follow_links(false)
.into_iter()
.flatten()
.filter_map(|entry| entry.metadata().ok())
.filter(|meta| meta.is_file())
.map(|meta| meta.len())
.sum()
}
/// A directory's size split by what deleting it would actually free.
#[derive(Debug, Clone, Copy, Default)]
pub struct DirSizeBreakdown {
/// Bytes `remove_dir_all` gives back to the disk.
pub freed_bytes: u64,
/// Bytes that survive the deletion because a hardlink outside the directory —
/// for pnpm and bun, the global store — still points at them.
pub shared_bytes: u64,
}
/// [`dir_size`], but hardlink-aware.
///
/// pnpm and bun do not copy packages into `node_modules`; they hardlink them from a
/// machine-wide store, so summing file sizes counts bytes the store keeps after the
/// delete and promises space a prune cannot deliver. Here a physical file is counted
/// once no matter how many names it has inside the tree, and counts as freed only
/// when every one of its links is inside the tree. A store that fell back to copying
/// — a different volume, a filesystem without hardlinks — leaves the link count at
/// one, so copied installs still count in full. A file whose link count cannot be
/// read is counted as freed, which errs toward the plain [`dir_size`] figure.
pub fn dir_size_with_hardlinks(path: &Path) -> DirSizeBreakdown {
let mut out = DirSizeBreakdown::default();
if !path.exists() {
return out;
}
// (volume, file id) → (bytes, links on disk, links seen inside this walk)
let mut linked: HashMap<(u64, u64), (u64, u64, u64)> = HashMap::new();
for entry in WalkDir::new(path).follow_links(false).into_iter().flatten() {
let Ok(meta) = entry.metadata() else { continue };
if !meta.is_file() {
continue;
}
match file_link_identity(entry.path(), &meta) {
Some((dev, ino, nlink)) if nlink > 1 => {
linked.entry((dev, ino)).or_insert((meta.len(), nlink, 0)).2 += 1;
}
_ => out.freed_bytes += meta.len(),
}
}
for (bytes, nlink, seen) in linked.into_values() {
if seen >= nlink {
out.freed_bytes += bytes;
} else {
out.shared_bytes += bytes;
}
}
out
}
/// (volume, file id, hardlink count) for one file, where the platform can say.
#[cfg(unix)]
fn file_link_identity(_path: &Path, meta: &std::fs::Metadata) -> Option<(u64, u64, u64)> {
use std::os::unix::fs::MetadataExt as _;
Some((meta.dev(), meta.ino(), meta.nlink()))
}
/// Windows keeps the link count behind an opened handle, not in the directory entry
/// (std exposes it only on an unstable feature), so this costs one metadata-only open
/// per file. Only the adapters that actually hardlink — pnpm and bun — pay it.
#[cfg(windows)]
fn file_link_identity(path: &Path, _meta: &std::fs::Metadata) -> Option<(u64, u64, u64)> {
use std::os::windows::fs::OpenOptionsExt as _;
use std::os::windows::io::AsRawHandle as _;
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
};
// access_mode(0) asks for attribute access only, so a file another process holds
// open without read sharing — an antivirus scan, an editor — does not fail here.
let file = std::fs::OpenOptions::new().access_mode(0).open(path).ok()?;
let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
// SAFETY: `file` keeps the handle open for the whole call, and `info` is a
// plain-data out-parameter the API fills before returning.
if unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut info) } == 0 {
return None;
}
Some((
u64::from(info.dwVolumeSerialNumber),
(u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow),
u64::from(info.nNumberOfLinks),
))
}
#[cfg(not(any(unix, windows)))]
fn file_link_identity(_path: &Path, _meta: &std::fs::Metadata) -> Option<(u64, u64, u64)> {
None
}
/// Resolve a program name into something `Command::new` can actually spawn.
///
/// On Windows the JS package managers (`npm`, `pnpm`, `yarn`, `bun`) are shipped as
/// `.cmd` shims. `CreateProcess` only ever appends `.exe`, so `Command::new("npm")`
/// fails with `NotFound` even when npm is installed and on `PATH`. Search `PATH`
/// ourselves for the shim extensions and hand back the full path.
///
/// Names that already contain a path separator (e.g. `.venv\Scripts\python.exe`)
/// are returned unchanged, as are all names on non-Windows platforms.
pub fn resolve_program(program: &str) -> String {
#[cfg(windows)]
{
if Path::new(program).components().count() > 1 {
return program.to_string();
}
let Some(path_var) = std::env::var_os("PATH") else {
return program.to_string();
};
for dir in std::env::split_paths(&path_var) {
for ext in ["exe", "cmd", "bat"] {
let candidate = dir.join(format!("{program}.{ext}"));
if candidate.is_file() {
return candidate.to_string_lossy().into_owned();
}
}
}
}
program.to_string()
}
/// Check whether a package manager binary is present and runnable.
///
/// Answers are cached for the life of the process. Every adapter asks this before it
/// enforces a lockfile, so a monorepo with ten projects on the same manager otherwise
/// pays for ten `npm --version` process spawns — around half a second each on Windows —
/// to learn the same fact ten times. A run is short-lived, so nothing installed or
/// removed mid-run can be missed for long.
pub fn binary_available(program: &str) -> bool {
static CACHE: OnceLock<Mutex<HashMap<String, bool>>> = OnceLock::new();
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
// Held across the probe on purpose: two threads asking about the same missing
// binary should spawn one process, not two. Nothing else takes this lock.
let mut guard = match cache.lock() {
Ok(g) => g,
// A poisoned lock only means some other thread panicked mid-probe; the answer
// is still worth having, so fall back to probing without the cache.
Err(_) => return probe_binary(program),
};
if let Some(known) = guard.get(program) {
return *known;
}
let available = probe_binary(program);
guard.insert(program.to_string(), available);
available
}
/// Programs that do not answer `--version`, and what to ask them instead.
///
/// `go --version` is not a typo for `go version`: the Go toolchain parses everything
/// after `go` as a subcommand, rejects the flag with `flag provided but not defined:
/// -version` and exits 2. A probe reading that as "not installed" is wrong on every
/// machine with Go on it, and wrong in a way that silently weakens things — the Go
/// adapter skips `go mod download` verification when it believes `go` is absent.
const VERSION_PROBE_ARGS: [(&str, &[&str]); 1] = [("go", &["version"])];
/// The arguments that make `program` print its version and exit `0`.
fn version_probe_args(program: &str) -> &'static [&'static str] {
VERSION_PROBE_ARGS
.iter()
.find(|(name, _)| *name == program)
.map_or(&["--version"], |(_, args)| *args)
}
/// The actual version-probe spawn behind [`binary_available`].
fn probe_binary(program: &str) -> bool {
crate::spawn::command(resolve_program(program))
.args(version_probe_args(program))
.stdin(std::process::Stdio::null())
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
/// The exit status and drained pipes of a finished command.
struct CommandOutput {
status: std::process::ExitStatus,
stdout: String,
stderr: String,
}
/// Spawn a command, drain both of its pipes and wait for it, bounded by `timeout`.
///
/// Shared by the two public wrappers below. `devp caches` needs a command's *output* —
/// `npm config get cache` answers a question rather than performing an action — and a
/// second copy of the draining and polling below would be a second place for the
/// deadlock it exists to prevent to come back.
fn spawn_capture(
program: &str,
args: &[&str],
cwd: &Path,
timeout: std::time::Duration,
) -> Result<CommandOutput> {
use std::io::Read;
use std::process::Stdio;
use std::thread;
use std::time::Instant;
let resolved = resolve_program(program);
let mut child = crate::spawn::command(&resolved)
.args(args)
.current_dir(cwd)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.with_context(|| format!("Failed to execute: {program} {}", args.join(" ")))?;
// Drain both pipes on their own threads. A package manager easily emits more
// than the ~64 KiB OS pipe buffer; if nobody reads it the child blocks on write
// and never exits, which would turn every large install into a timeout kill.
let mut stdout_pipe = child.stdout.take();
let mut stderr_pipe = child.stderr.take();
let stdout_reader = thread::spawn(move || {
let mut buf = Vec::new();
if let Some(pipe) = stdout_pipe.as_mut() {
let _ = pipe.read_to_end(&mut buf);
}
buf
});
let stderr_reader = thread::spawn(move || {
let mut buf = Vec::new();
if let Some(pipe) = stderr_pipe.as_mut() {
let _ = pipe.read_to_end(&mut buf);
}
buf
});
let start = Instant::now();
let status = loop {
match child.try_wait()? {
Some(status) => break status,
None => {
if start.elapsed() >= timeout {
let _ = child.kill();
let _ = child.wait();
anyhow::bail!(
"Command timed out after {}s: {} {}\n\
To increase the timeout, run: `devp config set command_timeout_secs <seconds>`",
timeout.as_secs(),
program,
args.join(" ")
);
}
thread::sleep(std::time::Duration::from_millis(100));
}
}
};
let stderr = stderr_reader
.join()
.map(|b| String::from_utf8_lossy(&b).into_owned())
.unwrap_or_default();
let stdout = stdout_reader
.join()
.map(|b| String::from_utf8_lossy(&b).into_owned())
.unwrap_or_default();
Ok(CommandOutput {
status,
stdout,
stderr,
})
}
/// Helper: run a command with a configurable timeout.
pub fn run_command_with_timeout(
program: &str,
args: &[&str],
cwd: &Path,
timeout: std::time::Duration,
) -> Result<()> {
let out = spawn_capture(program, args, cwd, timeout)?;
if out.status.success() {
Ok(())
} else {
anyhow::bail!(
"{} {} failed (exit code {:?}):\n{}",
program,
args.join(" "),
out.status.code(),
crate::output::condense_tool_output(
&out.stderr,
crate::constants::TOOL_OUTPUT_MAX_LINES
)
)
}
}
/// A command's outcome and both of its streams, for a caller that needs the failure
/// text rather than an error built from it.
pub struct CapturedCommand {
/// Whether it exited zero.
pub ok: bool,
/// Everything it wrote to stdout.
pub stdout: String,
/// Everything it wrote to stderr.
pub stderr: String,
}
/// Run a command and hand back what it printed, whether or not it worked.
///
/// `devp caches containers` needs the difference between "docker is not installed" and
/// "docker is installed and its daemon is not running", and the second only exists in
/// what the failed command wrote to stderr. Every other caller wants
/// [`capture_command_with_timeout`], which turns a non-zero exit into an error.
///
/// `Err` here is narrower than it looks: the process could not be spawned at all, or it
/// outlived `timeout`. A command that ran and failed is `Ok` with `ok: false`.
pub fn capture_allowing_failure(
program: &str,
args: &[&str],
cwd: &Path,
timeout: std::time::Duration,
) -> Result<CapturedCommand> {
let out = spawn_capture(program, args, cwd, timeout)?;
Ok(CapturedCommand {
ok: out.status.success(),
stdout: out.stdout,
stderr: out.stderr,
})
}
/// Run a command and hand back what it printed on stdout, bounded by `timeout`.
///
/// For commands that answer a question instead of doing work. A non-zero exit is an
/// error like anywhere else, so a caller never mistakes an error message on stderr for
/// the answer it asked for.
pub fn capture_command_with_timeout(
program: &str,
args: &[&str],
cwd: &Path,
timeout: std::time::Duration,
) -> Result<String> {
let out = spawn_capture(program, args, cwd, timeout)?;
if out.status.success() {