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
//! The install transaction: diff (lock vs installed state), parallel fetch
//! into the store, store-to-vendor clone, bin proxies, state files, runtime
//! stub. Idempotent (rerun after an interruption, it converges): the reference
//! state is `installed.json` + the presence of the directories, and each
//! package is laid out by cloning into a previously removed vendor/<name>.
use crate::error::{Error, Result};
use crate::fetch::{Fetcher, Provenance};
use crate::layout::Layout;
use crate::lock::{DistKind, Lock, LockPackage};
use crate::state::RootPackage;
use crate::store::Store;
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;
pub struct InstallOptions {
pub with_dev: bool,
pub offline: bool,
/// Download/extraction parallelism.
pub jobs: usize,
}
impl Default for InstallOptions {
fn default() -> Self {
InstallOptions {
with_dev: true,
offline: false,
jobs: 16,
}
}
}
#[derive(Debug, Default)]
pub struct InstallReport {
pub installed: usize,
pub removed: usize,
pub unchanged: usize,
pub from_cache: usize,
pub from_network: usize,
pub store_hits: usize,
/// Unchanged packages extracted into the store (pre-existing vendor).
pub store_warmed: usize,
/// The local repository after the transaction: the lock's entries for
/// the packages installed or updated, the previous installed.json
/// entries for the unchanged ones (Composer keeps the loaded objects and
/// dumps them back; the autoloader is generated from them too).
pub local_repository: Option<Lock>,
/// Lines Composer prints on stderr during the operations and vivacity
/// reproduces (`Skipped installation of bin …`).
pub messages: Vec<String>,
}
/// Installed identity of a package: version + dist reference.
fn identity(p: &LockPackage) -> (String, String) {
(
p.version().to_owned(),
p.dist_reference().unwrap_or("").to_owned(),
)
}
/// What installed.json says of a package: its identity and, for a `path`
/// package, the source it was laid out from.
struct Installed {
identity: (String, String),
path_source: Option<String>,
/// The entry as written, for a package that stays.
raw: serde_json::Map<String, Value>,
}
fn installed_packages(composer_dir: &Path) -> BTreeMap<String, Installed> {
let mut out = BTreeMap::new();
let path = composer_dir.join("installed.json");
let Ok(text) = std::fs::read_to_string(&path) else {
return out;
};
let Ok(v) = serde_json::from_str::<Value>(&text) else {
return out;
};
for p in v["packages"].as_array().into_iter().flatten() {
let name = p["name"].as_str().unwrap_or_default();
let version = p["version"].as_str().unwrap_or_default();
let reference = p["dist"]["reference"].as_str().unwrap_or_default();
let path_source = (p["dist"]["type"].as_str() == Some("path"))
.then(|| p["dist"]["url"].as_str().map(str::to_owned))
.flatten();
out.insert(
name.to_owned(),
Installed {
identity: (version.to_owned(), reference.to_owned()),
path_source,
raw: p.as_object().cloned().unwrap_or_default(),
},
);
}
out
}
pub async fn install(
_project_dir: &Path,
lock: &Lock,
root_manifest: &Value,
layout: &Layout,
store: Arc<Store>,
fetcher: Arc<Fetcher>,
opts: &InstallOptions,
) -> Result<InstallReport> {
// Absolute root (the layout's): the relative paths of the proxies and of
// the state files must not depend on a relative --working-dir.
let project_dir = layout.root();
let vendor = layout.vendor_dir();
let bin_dir = layout.bin_dir();
std::fs::create_dir_all(&vendor).map_err(Error::io(&vendor))?;
let mut report = InstallReport::default();
let wanted: Vec<&LockPackage> = lock.wanted_packages(opts.with_dev).collect();
let wanted_names: std::collections::BTreeSet<&str> = wanted.iter().map(|p| p.name()).collect();
// `Factory::purgePackages`: a package of installed.json whose install
// path is gone is not installed at all (a fresh install, not an
// update — no `removeBinaries`, no removal of the old path).
let mut previous = installed_packages(&layout.composer_dir());
previous.retain(|name, _| {
layout
.abs(name)
.or_else(|| layout.removals().find(|(n, _)| n == name).map(|(_, d)| d))
.is_some_and(|d| d.exists())
});
// To lay out: changed identity, or missing directory. Unchanged packages
// whose store entry is missing (vendor/ laid out by Composer before vivacity)
// are extracted into the store without being re-cloned: the classmap
// cache applies from the next run on.
let mut to_install: Vec<&LockPackage> = Vec::new();
let mut to_warm: Vec<&LockPackage> = Vec::new();
let mut unchanged_names: std::collections::BTreeSet<&str> = std::collections::BTreeSet::new();
for p in &wanted {
// Installed nowhere (a metapackage, a Flex pack): the layout has no
// path for it.
if layout.install_path(p.name()).is_none() {
if previous.get(p.name()).map(|i| &i.identity) == Some(&identity(p)) {
unchanged_names.insert(p.name());
}
continue;
}
// `LibraryInstaller::isInstalled`: a dangling link is not installed
// (`is_dir` follows links).
let unchanged = previous.get(p.name()).map(|i| &i.identity) == Some(&identity(p))
&& layout.abs(p.name()).is_some_and(|d| d.is_dir());
if unchanged {
unchanged_names.insert(p.name());
report.unchanged += 1;
if p.dist_kind() == DistKind::Zip
&& !store.contains(p.name(), p.version(), p.dist_reference())
{
to_warm.push(p);
}
} else {
to_install.push(p);
}
}
// `PathDownloader::download`: a package cannot be laid out inside its
// own source; checked before anything is written.
for p in &to_install {
if p.dist_kind() == DistKind::Path {
if let (Some(dest), Some(url)) = (layout.abs(p.name()), p.dist_url()) {
crate::path_install::check_not_inside_source(project_dir, &dest, url, p.name())?;
}
}
}
// Fetch + extraction into the store, with bounded parallelism. Packages to
// "warm" only use the local cache (never the network) and their failure
// is silent: it is an optimisation, not an obligation.
let sem = Arc::new(tokio::sync::Semaphore::new(opts.jobs.max(1)));
let mut tasks = tokio::task::JoinSet::new();
let warm_names: std::collections::BTreeSet<&str> = to_warm.iter().map(|p| p.name()).collect();
for p in to_install.iter().chain(to_warm.iter()) {
if p.dist_kind() == DistKind::Path {
continue;
}
if store.contains(p.name(), p.version(), p.dist_reference()) {
report.store_hits += 1;
continue;
}
let warm_only =
warm_names.contains(p.name()) && !to_install.iter().any(|q| q.name() == p.name());
let (name, version) = (p.name().to_owned(), p.version().to_owned());
let dist_ref = p.dist_reference().map(str::to_owned);
let url = p.dist_url_expanded().ok_or_else(|| Error::Http {
url: name.clone(),
message:
"package without a dist url (the scope detector should have routed to the fallback)"
.to_owned(),
})?;
let shasum = p.dist_shasum().map(str::to_owned);
let (store, fetcher, sem) = (store.clone(), fetcher.clone(), sem.clone());
let offline = opts.offline || warm_only;
tasks.spawn(async move {
let _permit = sem.acquire().await.map_err(|_| Error::Http {
url: url.clone(),
message: "semaphore closed".to_owned(),
})?;
let fetched = fetcher
.dist_bytes(&name, &url, shasum.as_deref(), offline)
.await;
let (bytes, provenance) = match fetched {
Ok(v) => v,
// Warming: zip missing from the cache, do not insist.
Err(_) if warm_only => return Ok::<Option<Provenance>, Error>(None),
Err(e) => return Err(e),
};
let store_name = name.clone();
let version2 = version.clone();
let dist_ref2 = dist_ref.clone();
tokio::task::spawn_blocking(move || {
store.ensure(&store_name, &version2, dist_ref2.as_deref(), &bytes)
})
.await
.map_err(|e| Error::Http {
url: name.clone(),
message: format!("extraction task interrupted: {e}"),
})??;
Ok::<Option<Provenance>, Error>(Some(provenance))
});
}
while let Some(joined) = tasks.join_next().await {
let provenance = joined.map_err(|e| Error::Http {
url: "join".to_owned(),
message: e.to_string(),
})??;
match provenance {
Some(Provenance::Cache) => report.from_cache += 1,
Some(Provenance::Network) => report.from_network += 1,
None => {}
}
}
report.store_warmed = to_warm.len();
// Removals: present before, no longer wanted, at the path validated by the
// layout (old install-path = recomputed path, like LibraryInstaller).
for name in previous.keys() {
if !wanted_names.contains(name.as_str()) {
report.removed += 1;
}
}
for (name, dir) in layout.removals() {
// `PathDownloader::remove`: the install path that *is* the source
// stays (", source is still present").
let own_source = previous
.get(name)
.and_then(|i| i.path_source.as_deref())
.is_some_and(|url| {
crate::path_install::is_own_source(project_dir, &dir.to_string_lossy(), url)
});
if own_source {
continue;
}
if std::fs::symlink_metadata(&dir).is_ok() {
crate::path_install::remove_path(&dir)?;
prune_empty_parent(project_dir, &dir);
}
}
// Layout: remove the old version, then clone from the store.
// Packages land in disjoint directories → fan-out where parallel I/O
// pays (Linux: sylius vendor/ wiped 3.75 s -> 1.68 s on ext4/WSL2);
// sequential where it does not (APFS clonefile is metadata-bound and
// contends: 607 ms -> 635 ms on an M4 Max). Each package stays atomic
// (remove-before-clone); only the inter-package order changes, which
// affects nothing but mtimes. `VIVACITY_PARALLEL_IO=0|1` overrides.
let place = |p: &&LockPackage| -> Result<bool> {
let (Some(pkg_root), Some(dest)) = (layout.package_root(p.name()), layout.abs(p.name()))
else {
return Ok(false);
};
if p.dist_kind() == DistKind::Path {
let url = p.dist_url().unwrap_or_default();
// `FileDownloader::update` removes the previous layout before
// `install` (a link becomes a mirror when the options changed);
// a fresh install keeps a path that already resolves to the
// source.
if previous.contains_key(p.name()) {
crate::path_install::remove_path(&pkg_root)?;
}
crate::path_install::install(project_dir, &dest, url, p.raw.get("transport-options"))?;
return Ok(true);
}
// Always start again from an empty package root (target-dir included).
if std::fs::symlink_metadata(&pkg_root).is_ok() {
crate::path_install::remove_path(&pkg_root)?;
}
let src = store.entry_path(p.name(), p.version(), p.dist_reference());
crate::clone::clone_tree(&src, &dest)?;
Ok(true)
};
let placed: Vec<bool> = if crate::platform::parallel_io() {
use rayon::prelude::*;
to_install.par_iter().map(place).collect::<Result<_>>()?
} else {
to_install.iter().map(place).collect::<Result<_>>()?
};
let installed: usize = placed.into_iter().filter(|placed| *placed).count();
report.installed += installed;
// `BinaryInstaller::removeBinaries` runs `initializeBinDir` before
// looking at the package's binaries: an update, a removal, or the
// reinstall of a package still listed in installed.json creates
// vendor/bin even when nothing has a `bin`.
let touches_installed = to_install.iter().any(|p| previous.contains_key(p.name()))
|| previous.keys().any(|n| !wanted_names.contains(n.as_str()));
if touches_installed {
std::fs::create_dir_all(&bin_dir).map_err(Error::io(&bin_dir))?;
}
// Bin proxies: rebuilt for the packages actually (re)placed
// (`BinaryInstaller::installBinaries` on install/update), and — like
// `Installer::run`'s `ensureBinariesPresence` over every installed
// package — written for an unchanged package only where the proxy is
// MISSING (`installBinaries(..., warnOnOverwrite: false)` skips an
// existing one): a wiped `vendor/bin` comes back on a no-op install,
// and a no-op install otherwise touches nothing. The purge of orphaned
// proxies (removed packages) runs on `wanted`. The `.bat` follows the
// resolved bin-compat (`full`, or `auto` on Windows/WSL), like
// Composer's BinaryInstaller — a plain Linux/macOS install writes no
// `.bat`.
let bin_compat = crate::binproxy::resolve_bin_compat(root_manifest)?;
let placed: std::collections::HashSet<&str> = to_install.iter().map(|p| p.name()).collect();
// `LibraryInstaller::update` removes the old version's binaries before
// installing the new ones (`removeBinaries` then `installBinaries`); a
// fresh install only adds. The bin directory is removed when a removal
// leaves it empty — and recreated by the next `initializeBinDir`.
let previous_bins = |name: &str| -> Vec<String> {
previous
.get(name)
.and_then(|i| i.raw.get("bin"))
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect()
})
.unwrap_or_default()
};
for p in &wanted {
let bins = p.bins();
let Some(dir) = layout.abs(p.name()) else {
continue;
};
if placed.contains(p.name()) && previous.contains_key(p.name()) {
let old = previous_bins(p.name());
let old: Vec<&str> = old.iter().map(String::as_str).collect();
crate::binproxy::remove_binaries(&bin_dir, &old)?;
}
if bins.is_empty() {
continue;
}
let missing = || {
bins.iter().any(|b| {
let b = b.trim_start_matches("./");
let link_name = b.rsplit_once('/').map(|(_, f)| f).unwrap_or(b);
dir.join(b).exists() && !bin_dir.join(link_name).exists()
})
};
if placed.contains(p.name()) {
report.messages.extend(crate::binproxy::install_binaries(
&vendor,
&bin_dir,
p.name(),
&dir,
&bins,
bin_compat,
true,
)?);
} else if missing() {
crate::binproxy::install_binaries(
&vendor,
&bin_dir,
p.name(),
&dir,
&bins,
bin_compat,
false,
)?;
}
}
// Removed packages: `removeBinaries` on each (their proxies and `.bat`).
for name in previous.keys() {
if !wanted_names.contains(name.as_str()) {
let old = previous_bins(name);
let old: Vec<&str> = old.iter().map(String::as_str).collect();
crate::binproxy::remove_binaries(&bin_dir, &old)?;
}
}
prune_stale_bat_proxies(&bin_dir, &wanted, bin_compat)?;
// State files + runtime stub, from the local repository: an unchanged
// package keeps the entry installed.json already had (its own
// `version_normalized`/`installation-source`/`install-path` are
// recomputed), so a lock that changed a package's metadata without
// changing its identity — routine with `path` packages whose reference
// is a git HEAD or none — leaves installed.json and the autoloader as
// Composer leaves them.
let mut local = lock.clone();
for p in local
.packages
.iter_mut()
.chain(local.packages_dev.iter_mut())
{
if !unchanged_names.contains(p.name()) {
continue;
}
if let Some(prev) = previous.get(p.name()) {
let mut raw = prev.raw.clone();
for key in ["version_normalized", "installation-source", "install-path"] {
raw.remove(key);
}
p.raw = raw;
}
}
let root = RootPackage::detect(root_manifest, project_dir, opts.with_dev);
crate::state::write_state_files(
&layout.composer_dir(),
&local,
&root,
root_manifest,
opts.with_dev,
layout,
)?;
report.local_repository = Some(local);
Ok(report)
}
/// `LibraryInstaller::uninstall`: the parent directory of the removed package
/// (vendor/<ns>, web/app/plugins...) is removed if empty, never the project
/// root.
fn prune_empty_parent(project_dir: &Path, removed: &Path) {
let Some(parent) = removed.parent() else {
return;
};
if parent == project_dir {
return;
}
if std::fs::read_dir(parent)
.map(|mut d| d.next().is_none())
.unwrap_or(false)
{
let _ = std::fs::remove_dir(parent);
}
}
/// A `<bin>.bat` next to an expected proxy is the Windows proxy of that
/// bin — kept only when the resolved bin-compat writes `.bat` proxies at
/// all; otherwise a leftover from a previous full-mode install, purged so
/// the tree converges on what Composer produces on a bare checkout.
/// Nothing else is touched: the bin directory may be the project's own.
fn prune_stale_bat_proxies(
bin_dir: &Path,
wanted: &[&LockPackage],
bin_compat: crate::binproxy::BinCompat,
) -> Result<()> {
if bin_compat == crate::binproxy::BinCompat::Full {
return Ok(());
}
let Ok(entries) = std::fs::read_dir(bin_dir) else {
return Ok(());
};
let mut expected: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for p in wanted {
for bin in p.bins() {
let bin = bin.trim_start_matches("./");
let link = bin.rsplit_once('/').map(|(_, f)| f).unwrap_or(bin);
expected.insert(link.to_owned());
}
}
for entry in entries.flatten() {
let file_name = entry.file_name().to_string_lossy().into_owned();
if file_name
.strip_suffix(".bat")
.is_some_and(|stem| expected.contains(stem))
{
let p = entry.path();
std::fs::remove_file(&p).map_err(Error::io(&p))?;
}
}
Ok(())
}