vivacity-resolver 0.19.1

Port of Composer's dependency resolver — semver, Packagist metadata, pool, solver, lock file — for vivacity
Documentation
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
//! The root package as `RootPackageLoader::load` builds it
//! (docs/reference/RootPackageLoader.php): version (composer.json,
//! COMPOSER_ROOT_VERSION, git, otherwise `1.0.0+no-version-set`), links,
//! `minimum-stability`, `prefer-stable`, and the three extractions from the
//! require constraints: aliases (`X as Y`), stability flags (`@dev`,
//! branches), references (`#sha`).

use crate::constraint::parse_constraints;
use crate::loader;
use crate::package::{Links, Origin, Package};
use crate::version::{
    self, group, normalize, regex, stability_rank, VersionError, STABILITIES_REGEX,
};
use pcre2::bytes::Regex;
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::OnceLock;

pub const DEFAULT_PRETTY_VERSION: &str = "1.0.0+no-version-set";

/// `RootPackage::getAliases()`: an alias declared in a require.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RootAlias {
    pub package: String,
    pub version: String,
    pub alias: String,
    pub alias_normalized: String,
}

#[derive(Debug, Clone)]
pub struct RootPackage {
    pub package: Package,
    /// `RootAliasPackage`: (normalized alias, pretty alias) if `extra.branch-alias` applies.
    pub branch_alias: Option<(String, String)>,
    pub minimum_stability: String,
    pub prefer_stable: bool,
    /// name -> stability rank (`BasePackage::STABILITIES`).
    pub stability_flags: BTreeMap<String, i32>,
    pub aliases: Vec<RootAlias>,
    /// name -> git reference.
    pub references: BTreeMap<String, String>,
    /// `config.platform`.
    pub platform_overrides: Map<String, Value>,
    pub manifest: Value,
}

#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct RootError(pub String);

/// What a dry run changes on the root package in memory instead of in
/// composer.json.
#[derive(Debug, Clone, Default)]
pub struct RootPatch {
    /// `(name, constraint)` to add (`require` or, with `dev`, `require-dev`).
    pub requirements: Vec<(String, String)>,
    pub dev: bool,
    /// `(from require-dev?, name as typed)` to remove.
    pub removals: Vec<(bool, String)>,
}

impl RootPackage {
    /// Loads composer.json (already parsed) with the root version guessed by
    /// vivacity-core (same rules as RootPackageLoader + VersionGuesser).
    pub fn load(manifest: &Value, project_dir: &Path) -> Result<RootPackage, RootError> {
        let mut config = manifest
            .as_object()
            .cloned()
            .ok_or_else(|| RootError("composer.json is not an object".into()))?;
        if !config.contains_key("name") {
            config.insert("name".into(), Value::String("__root__".into()));
        }
        let mut auto_versioned = false;
        if !config.contains_key("version") {
            let rv = vivacity_core::root_version::detect(manifest, project_dir);
            if rv.pretty_version == vivacity_core::root_version::DEFAULT_PRETTY_VERSION {
                config.insert("version".into(), Value::String("1.0.0".into()));
                auto_versioned = true;
            } else {
                config.insert("version".into(), Value::String(rv.pretty_version.clone()));
                config.insert(
                    "version_normalized".into(),
                    Value::String(rv.version.clone()),
                );
                if let Some(commit) = rv.reference {
                    let r = serde_json::json!({"type": "", "url": "", "reference": commit});
                    config.insert("source".into(), r.clone());
                    config.insert("dist".into(), r);
                }
            }
        }
        let value = Value::Object(config.clone());
        let (mut package, alias) =
            loader::load(&value, Origin::Root, false).map_err(|e| RootError(e.0))?;
        if auto_versioned {
            package.pretty_version = DEFAULT_PRETTY_VERSION.to_owned();
        }
        let minimum_stability = match config.get("minimum-stability").and_then(Value::as_str) {
            Some(s) => normalize_stability(s)?,
            None => "stable".to_owned(),
        };
        let mut aliases = Vec::new();
        let mut stability_flags = BTreeMap::new();
        let mut references = BTreeMap::new();
        for links in [&package.requires, &package.dev_requires] {
            let map: Vec<(String, String)> = links
                .iter()
                // `$link->getConstraint()->getPrettyString()`: for
                // `self.version`, the root version.
                .map(|l| {
                    let pretty = if l.pretty_constraint == "self.version" {
                        // Pretty version at parseLinks time (before
                        // `setPrettyVersion('1.0.0+no-version-set')`).
                        if auto_versioned {
                            "1.0.0".to_owned()
                        } else {
                            package.pretty_version.clone()
                        }
                    } else {
                        l.pretty_constraint.clone()
                    };
                    (l.target.clone(), pretty)
                })
                .collect();
            extract_aliases(&map, &mut aliases)?;
            extract_stability_flags(&map, &minimum_stability, &mut stability_flags);
            extract_references(&map, &mut references);
            if map.iter().any(|(n, _)| *n == package.name) {
                return Err(RootError(format!(
                    "Root package '{}' cannot require itself in its composer.json\nDid you accidentally name your root package after an external package?",
                    package.pretty_name
                )));
            }
        }
        let prefer_stable = config
            .get("prefer-stable")
            .map(|v| match v {
                Value::Bool(b) => *b,
                Value::Number(n) => n.as_f64() != Some(0.0),
                Value::String(s) => !(s.is_empty() || s == "0"),
                Value::Null => false,
                _ => true,
            })
            .unwrap_or(false);
        let platform_overrides = config
            .get("config")
            .and_then(|c| c.get("platform"))
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        Ok(RootPackage {
            package,
            branch_alias: alias,
            minimum_stability,
            prefer_stable,
            stability_flags,
            aliases,
            references,
            platform_overrides,
            manifest: manifest.clone(),
        })
    }

    /// `require` + `require-dev` links (`array_merge`: dev-requires
    /// overwrite a duplicate target, at its position).
    /// The in-memory root patch of a dry run (`RequireCommand`,
    /// `RemoveCommand`): the new links are `array_merge`d onto the key's
    /// links (a duplicate key keeps its position), removed from the other
    /// key by their name, and the references / stability flags are
    /// extended; aliases are not re-extracted (the reference does not).
    pub fn apply_patch(&mut self, patch: &RootPatch) -> Result<(), RootError> {
        if !patch.requirements.is_empty() {
            let map: Map<String, Value> = patch
                .requirements
                .iter()
                .map(|(n, c)| (n.clone(), Value::String(c.clone())))
                .collect();
            let kind = if patch.dev {
                crate::package::LinkType::DevRequire
            } else {
                crate::package::LinkType::Require
            };
            let new_links = loader::parse_links(
                &self.package.name,
                &self.package.pretty_version,
                kind,
                Some(&Value::Object(map)),
                false,
            )
            .map_err(|e| RootError(e.0))?;
            let (own_links, other_links) = if kind == crate::package::LinkType::DevRequire {
                (&mut self.package.dev_requires, &mut self.package.requires)
            } else {
                (&mut self.package.requires, &mut self.package.dev_requires)
            };
            for l in new_links.iter() {
                own_links.insert(l.clone());
            }
            for (name, _) in &patch.requirements {
                other_links.remove(name);
            }
            let pairs: Vec<(String, String)> = patch.requirements.clone();
            extract_references(&pairs, &mut self.references);
            extract_stability_flags(&pairs, &self.minimum_stability, &mut self.stability_flags);
        }
        // `RemoveCommand`: `unset($links[$type][$name])` with the name as
        // typed against lowercase keys.
        for (dev, name) in &patch.removals {
            if *dev {
                self.package.dev_requires.remove(name);
            } else {
                self.package.requires.remove(name);
            }
        }
        Ok(())
    }

    pub fn all_requires(&self) -> Links {
        let mut out = self.package.requires.clone();
        for l in self.package.dev_requires.iter() {
            out.insert(l.clone());
        }
        out
    }
}

/// `VersionParser::normalizeStability`.
/// The refusals `RootPackageLoader::load` raises from the manifest alone, in
/// its order: the root package's own name, then — after the links are parsed,
/// which changes nothing for a raw manifest — a package that requires itself,
/// then the name of every link of the five `BasePackage::$supportedLinkTypes`.
///
/// Composer refuses these for EVERY command, because `Factory::createComposer`
/// loads the root package; vivacity only built a `RootPackage` for a
/// resolution, so `install` and `dump-autoload` used to accept them. They call
/// this directly — the checks that need a parsed manifest (a malformed alias,
/// an unknown `minimum-stability`, an unparsable constraint) stay inside
/// `load`, since reaching them means guessing the root version, i.e. running
/// git, on the install hot path.
///
/// The message is returned as Composer words it, prefixes included. vivacity
/// prints it without Symfony Console's box, as it already does for path
/// repositories: the box is wrapped to the terminal width, followed by the
/// command synopsis, and sometimes chained.
///
/// These rules are reported even when Composer would have answered with
/// ANOTHER message — its JSON schema runs first and catches an uppercase or
/// malformed `name`, and `parent::load` catches an unparsable constraint before
/// the link-name loop. Reporting ours then differs in words; staying silent
/// would differ in OUTCOME, letting `dump-autoload` write an autoloader where
/// Composer writes nothing. Words are the cheaper divergence, and the project
/// already takes one for the box.
pub fn manifest_error(manifest: &Value) -> Option<String> {
    use crate::lockfile::package_naming_error;
    let config = manifest.as_object()?;
    // `if (!isset($config['name'])) { '__root__' } elseif ($err = …)`.
    //
    // The schema runs first, and it holds the same shape pattern for `name`
    // but CASE SENSITIVELY (`^[a-z0-9]([_.-]?[a-z0-9]+)*/…$`, measured in
    // `res/composer-schema.json`). So a root name with an uppercase letter or a
    // bad shape is refused with the schema's message, which vivacity does not
    // reproduce — only what the schema lets through is ours to report, i.e. the
    // `.json` suffix and the reserved names.
    let name = config.get("name").and_then(Value::as_str);
    if let Some(name) = name {
        if let Some(err) = package_naming_error(name, false) {
            return Some(format!("Your package name {err}"));
        }
    }
    // `if (isset($links[$config['name']]))`, for require then require-dev. The
    // targets are lowercased by `parseLinks`; the name passed the check above,
    // so it is already lowercase ASCII here.
    if let Some(name) = name {
        for link_type in ["require", "require-dev"] {
            let Some(links) = config.get(link_type).and_then(Value::as_object) else {
                continue;
            };
            if links.keys().any(|k| k.to_ascii_lowercase() == name) {
                return Some(format!(
                    "Root package '{name}' cannot require itself in its composer.json\nDid you accidentally name your root package after an external package?"
                ));
            }
        }
    }
    // `foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType)`,
    // in that order, first error wins.
    for link_type in LINK_TYPES {
        let Some(links) = config.get(*link_type).and_then(Value::as_object) else {
            continue;
        };
        for target in links.keys() {
            if let Some(err) = package_naming_error(target, true) {
                return Some(format!("{link_type}.{err}"));
            }
        }
    }
    None
}

/// `array_keys(BasePackage::$supportedLinkTypes)`, in its order.
const LINK_TYPES: &[&str] = &["require", "conflict", "provide", "replace", "require-dev"];

pub fn normalize_stability(s: &str) -> Result<String, RootError> {
    let lower = s.to_lowercase();
    match lower.as_str() {
        "stable" | "beta" | "alpha" | "dev" => Ok(lower),
        "rc" => Ok("RC".to_owned()),
        _ => Err(RootError(format!(
            "Invalid stability string \"{s}\", expected one of stable, RC, beta, alpha or dev"
        ))),
    }
}

/// `RootPackageLoader::extractAliases`.
fn extract_aliases(
    requires: &[(String, String)],
    aliases: &mut Vec<RootAlias>,
) -> Result<(), RootError> {
    static RE: OnceLock<Regex> = OnceLock::new();
    let re = regex(
        &RE,
        r"(?:^|\| *|, *)([^,\s#|]+)(?:#[^ ]+)? +as +([^,\s|]+)(?:$| *\|| *,)",
        false,
    );
    for (name, req) in requires {
        if let Ok(Some(caps)) = re.captures(req.as_bytes()) {
            let v = group(&caps, 1);
            let a = group(&caps, 2);
            aliases.push(RootAlias {
                package: name.to_lowercase(),
                version: normalize(v, Some(req)).map_err(|e: VersionError| RootError(e.0))?,
                alias: a.to_owned(),
                alias_normalized: normalize(a, Some(req))
                    .map_err(|e: VersionError| RootError(e.0))?,
            });
        } else if req.contains(" as ") {
            return Err(RootError(format!(
                "Invalid alias definition in \"{name}\": \"{req}\". Aliases should be in the form \"exact-version as other-exact-version\"."
            )));
        }
    }
    Ok(())
}

/// `preg_split` of the constraints into "and" pieces across the "or"s.
fn split_constraints(req: &str) -> Vec<String> {
    static OR: OnceLock<Regex> = OnceLock::new();
    static AND: OnceLock<Regex> = OnceLock::new();
    let or = regex(&OR, r"\s*\|\|?\s*", false);
    let and = regex(
        &AND,
        r"(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)",
        false,
    );
    let mut out = Vec::new();
    for part in split(or, req.trim()) {
        out.extend(split(and, &part));
    }
    out
}

fn split(re: &Regex, subject: &str) -> Vec<String> {
    let bytes = subject.as_bytes();
    let mut out = Vec::new();
    let mut last = 0;
    for m in re.find_iter(bytes).flatten() {
        if m.start() == m.end() && m.start() == last && last == bytes.len() {
            break;
        }
        out.push(subject[last..m.start()].to_owned());
        last = m.end();
    }
    out.push(subject[last..].to_owned());
    out
}

/// composer-merge-plugin's `ExtraPackage::mergeStabilityFlags` for one
/// merged file: `StabilityFlags::extractAll` over the file's own
/// requirements (an explicit `@flag`, the most unstable across the
/// constraint's parts, else the parsed stability when it is dev and not
/// more stable than the minimum), each raised to at least the root's
/// current flag, then `array_merge`d over the root's flags.
pub fn merge_plugin_stability_flags(
    flags: &mut BTreeMap<String, i32>,
    minimum_stability: &str,
    requires: &[(String, String)],
) {
    static AT: OnceLock<Regex> = OnceLock::new();
    static AS: OnceLock<Regex> = OnceLock::new();
    let at = regex(&AT, &format!("^[^@]*?@({STABILITIES_REGEX})$"), true);
    let as_re = regex(&AS, r"^([^,\s@]+) as .+$", false);
    let minimum = stability_rank(minimum_stability);
    for (req_name, pretty) in requires {
        let name = req_name.to_lowercase();
        let mut explicit: Option<i32> = None;
        for c in split_constraints(pretty) {
            if let Ok(Some(caps)) = at.captures(c.as_bytes()) {
                let Ok(stab) = normalize_stability(group(&caps, 1)) else {
                    continue;
                };
                let rank = stability_rank(&stab);
                explicit = Some(explicit.map_or(rank, |e| e.max(rank)));
            }
        }
        let stability = match explicit {
            Some(e) => Some(e),
            None => {
                // Drop aliasing if used.
                let v = match as_re.captures(pretty.as_bytes()) {
                    Ok(Some(caps)) => group(&caps, 1).to_owned(),
                    _ => pretty.clone(),
                };
                let rank = stability_rank(version::parse_stability(&v));
                if rank == stability_rank("stable") || minimum > rank {
                    None
                } else {
                    Some(rank)
                }
            }
        };
        if let Some(st) = stability {
            let current = flags.get(&name).copied();
            flags.insert(name, current.map_or(st, |c| c.max(st)));
        }
    }
}

/// `RootPackageLoader::extractStabilityFlags`.
pub fn extract_stability_flags(
    requires: &[(String, String)],
    minimum_stability: &str,
    flags: &mut BTreeMap<String, i32>,
) {
    static AT: OnceLock<Regex> = OnceLock::new();
    static AS: OnceLock<Regex> = OnceLock::new();
    static PLAIN: OnceLock<Regex> = OnceLock::new();
    let at = regex(&AT, &format!("^[^@]*?@({STABILITIES_REGEX})$"), true);
    let as_re = regex(&AS, r"^([^,\s@]+) as .+$", false);
    let plain = regex(&PLAIN, r"^[^,\s@]+$", false);
    let minimum = stability_rank(minimum_stability);
    for (req_name, req) in requires {
        let constraints = split_constraints(req);
        let mut matched = false;
        for c in &constraints {
            if let Ok(Some(caps)) = at.captures(c.as_bytes()) {
                let name = req_name.to_lowercase();
                let Ok(stab) = normalize_stability(group(&caps, 1)) else {
                    continue;
                };
                let rank = stability_rank(&stab);
                if flags.get(&name).is_some_and(|f| *f > rank) {
                    continue;
                }
                flags.insert(name, rank);
                matched = true;
            }
        }
        if matched {
            continue;
        }
        for c in &constraints {
            let stripped = match as_re.captures(c.as_bytes()) {
                Ok(Some(caps)) => group(&caps, 1).to_owned(),
                _ => c.clone(),
            };
            if plain.is_match(stripped.as_bytes()).unwrap_or(false) {
                let stability = version::parse_stability(&stripped);
                if stability != "stable" {
                    let name = req_name.to_lowercase();
                    let rank = stability_rank(stability);
                    if flags.get(&name).is_some_and(|f| *f > rank) || minimum > rank {
                        continue;
                    }
                    flags.insert(name, rank);
                }
            }
        }
    }
}

/// `RootPackageLoader::extractReferences`.
pub fn extract_references(
    requires: &[(String, String)],
    references: &mut BTreeMap<String, String>,
) {
    static AS: OnceLock<Regex> = OnceLock::new();
    static REF: OnceLock<Regex> = OnceLock::new();
    let as_re = regex(&AS, r"^([^,\s@]+) as .+$", false);
    let re = regex(&REF, r"^[^,\s@]+?#([a-f0-9]+)$", false);
    for (name, req) in requires {
        let stripped = match as_re.captures(req.as_bytes()) {
            Ok(Some(caps)) => group(&caps, 1).to_owned(),
            _ => req.clone(),
        };
        if let Ok(Some(caps)) = re.captures(stripped.as_bytes()) {
            if version::parse_stability(&stripped) == "dev" {
                references.insert(name.to_lowercase(), group(&caps, 1).to_owned());
            }
        }
    }
}

/// Constraint of a root require (with `as`: the source part).
pub fn root_constraint(pretty: &str) -> Result<crate::constraint::Constraint, VersionError> {
    Ok(parse_constraints(pretty)?.constraint)
}

#[cfg(test)]
mod tests {
    #[test]
    fn merge_plugin_flags_follow_the_plugin() {
        use super::merge_plugin_stability_flags as flags_of;
        use std::collections::BTreeMap;
        let req = |pairs: &[(&str, &str)]| -> Vec<(String, String)> {
            pairs
                .iter()
                .map(|(n, c)| ((*n).to_owned(), (*c).to_owned()))
                .collect()
        };
        // Explicit flag: the most unstable part of the constraint wins,
        // whatever the minimum stability; the name is lowercased.
        let mut f = BTreeMap::new();
        flags_of(
            &mut f,
            "stable",
            &req(&[("Acme/Lib", "^1.0@beta || ^2.0@RC")]),
        );
        assert_eq!(f.get("acme/lib"), Some(&10));
        // Parsed dev stability counts unless the minimum is more unstable
        // than it (`$this->minimumStability > $stability`), and never a
        // stable one; unlike the loader, no "single plain token" check.
        let mut f = BTreeMap::new();
        flags_of(
            &mut f,
            "stable",
            &req(&[("a/b", "dev-main || ^1.0"), ("c/d", "^1.0")]),
        );
        assert_eq!(f.get("a/b"), Some(&20));
        assert!(!f.contains_key("c/d"));
        let mut f = BTreeMap::new();
        flags_of(&mut f, "dev", &req(&[("a/b", "1.0.0-beta")]));
        assert!(f.is_empty(), "beta is more stable than the dev minimum");
        // `max($stability, current)`: an existing flag is never lowered,
        // and a requirement without a flag leaves it untouched.
        let mut f: BTreeMap<String, i32> = [("a/b".to_owned(), 20)].into();
        flags_of(
            &mut f,
            "stable",
            &req(&[("a/b", "^1.0@beta"), ("a/b", "^1.0")]),
        );
        assert_eq!(f.get("a/b"), Some(&20));
        // The alias is dropped before parsing.
        let mut f = BTreeMap::new();
        flags_of(&mut f, "stable", &req(&[("a/b", "dev-main as 1.0.0")]));
        assert_eq!(f.get("a/b"), Some(&20));
    }

    use super::*;
    use serde_json::json;

    #[test]
    fn extracts_flags_aliases_references() {
        let reqs = vec![
            ("a/b".to_owned(), "^1.0@beta".to_owned()),
            ("c/d".to_owned(), "dev-main as 1.0.x-dev".to_owned()),
            ("e/f".to_owned(), "dev-main#abcdef".to_owned()),
            ("g/h".to_owned(), "1.x-dev || ^2.0".to_owned()),
            ("i/j".to_owned(), "^1.0".to_owned()),
        ];
        let mut flags = BTreeMap::new();
        extract_stability_flags(&reqs, "stable", &mut flags);
        assert_eq!(flags.get("a/b"), Some(&10));
        assert_eq!(flags.get("c/d"), Some(&20));
        assert_eq!(flags.get("e/f"), Some(&20));
        assert_eq!(flags.get("g/h"), Some(&20));
        assert_eq!(flags.get("i/j"), None);
        let mut aliases = Vec::new();
        extract_aliases(&reqs, &mut aliases).unwrap();
        assert_eq!(aliases.len(), 1);
        assert_eq!(aliases[0].alias_normalized, "1.0.9999999.9999999-dev");
        let mut refs = BTreeMap::new();
        extract_references(&reqs, &mut refs);
        assert_eq!(refs.get("e/f").map(String::as_str), Some("abcdef"));
    }

    #[test]
    fn loads_root_without_git() {
        let m = json!({"name": "acme/app", "require": {"php": "^8.1", "monolog/monolog": "^3"}, "minimum-stability": "RC", "prefer-stable": true});
        let r = RootPackage::load(&m, Path::new("/nonexistent-vivacity")).unwrap();
        assert_eq!(r.package.pretty_version, DEFAULT_PRETTY_VERSION);
        assert_eq!(r.package.version, "1.0.0.0");
        assert_eq!(r.minimum_stability, "RC");
        assert!(r.prefer_stable);
        assert_eq!(r.package.requires.len(), 2);
    }

    #[test]
    fn manifest_error_follows_the_loader() {
        use serde_json::json;
        // A real manifest is accepted, platform packages included.
        assert_eq!(
            manifest_error(&json!({
                "name": "acme/app",
                "require": {"php": ">=8.1", "ext-json": "*", "psr/log": "^3", "a-b/c--d": "*"},
                "require-dev": {"phpunit/phpunit": "^11"},
            })),
            None
        );
        // The root name: only what the schema lets through is ours to report.
        assert_eq!(
            manifest_error(&json!({"name": "acme/thing.json"})).as_deref(),
            Some("Your package name acme/thing.json is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.")
        );
        assert!(manifest_error(&json!({"name": "con/thing"})).is_some());
        // An uppercase or malformed root name is refused by Composer's JSON
        // schema first, so its message is the schema's and ours is the
        // loader's. Both refuse and both write nothing, which is the part that
        // matters; staying silent here would let `dump-autoload` write.
        assert_eq!(
            manifest_error(&json!({"name": "acme/ThingBaz"})).as_deref(),
            Some("Your package name acme/ThingBaz is invalid, it should not contain uppercase characters. We suggest using acme/thing-baz instead.")
        );
        assert!(manifest_error(&json!({"name": "acme"}))
            .is_some_and(|e| e
                .starts_with("Your package name acme is invalid, it should have a vendor name")));
        // A trailing newline: the schema's `$` tolerates it, the loader's `D`
        // does not — so the loader is what answers, and so do we.
        assert!(manifest_error(&json!({"name": "acme/app\n"})).is_some());
        // No name at all is `__root__`, never an error.
        assert_eq!(manifest_error(&json!({"require": {"a/b": "*"}})), None);
        // A package that requires itself, from either section.
        for section in ["require", "require-dev"] {
            let m = json!({"name": "acme/app", section: {"acme/app": "*"}});
            assert!(
                manifest_error(&m).is_some_and(|e| e
                    .starts_with("Root package 'acme/app' cannot require itself")
                    && e.contains("\nDid you accidentally")),
                "{section}"
            );
        }
        // The five link types, each prefixed with its own name.
        for section in ["require", "conflict", "provide", "replace", "require-dev"] {
            let m = json!({"name": "acme/app", section: {"Acme/Thing": "^1"}});
            assert_eq!(
                manifest_error(&m).as_deref(),
                Some(&*format!("{section}.Acme/Thing is invalid, it should not contain uppercase characters. Please use acme/thing instead.")),
                "{section}"
            );
        }
        // Composer's order: the root name first, then self-require, then the
        // link names in the order of `BasePackage::$supportedLinkTypes`.
        let all = json!({
            "name": "acme/thing.json",
            "require": {"acme/thing.json": "*", "Bad/Name": "*"},
            "conflict": {"Other/Name": "*"},
        });
        assert!(manifest_error(&all).is_some_and(|e| e.starts_with("Your package name")));
        let links = json!({"require": {"Bad/Name": "*"}, "conflict": {"Other/Name": "*"}});
        assert!(manifest_error(&links).is_some_and(|e| e.starts_with("require.Bad/Name")));
    }
}