fatou 0.6.0

A language server, formatter, and linter for Julia
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
//! Julia environment resolution: locate the active project, read the pinned
//! package set from its manifest, and resolve each package to its on-disk
//! source directory in a depot.
//!
//! Fatou has no Julia runtime, so this mirrors what Julia's loader does using
//! only the filesystem. Discovery follows Julia's precedence: `JULIA_PROJECT`
//! first, then a walk-up from the workspace root, then the newest default
//! environment under `~/.julia/environments/`. Package sources live at
//! `<depot>/packages/<Name>/<slug>/`, where `<slug>` is derived from the
//! package UUID and its `git-tree-sha1` (see [`version_slug`]); we compute the
//! slug rather than scan because a package may have several versions installed.
//!
//! This module is intentionally standalone: it is not yet wired into the salsa
//! layer, the LSP, or the CLI. Later Phase 3/5 work consumes [`Environment`].

use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};

/// A parsed 16-byte package UUID, stored in textual (big-endian) byte order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Uuid([u8; 16]);

impl Uuid {
    /// The 16 bytes in textual (big-endian) order.
    pub fn bytes(&self) -> [u8; 16] {
        self.0
    }
}

impl std::str::FromStr for Uuid {
    type Err = ();

    /// Parse the canonical `8-4-4-4-12` hyphenated form (hyphens optional).
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut bytes = [0u8; 16];
        let mut nibbles = s.bytes().filter(|b| *b != b'-');
        for byte in bytes.iter_mut() {
            let hi = nibbles.next().and_then(hex_val).ok_or(())?;
            let lo = nibbles.next().and_then(hex_val).ok_or(())?;
            *byte = (hi << 4) | lo;
        }
        if nibbles.next().is_some() {
            return Err(()); // too many hex digits
        }
        Ok(Uuid(bytes))
    }
}

fn hex_val(b: u8) -> Option<u8> {
    match b {
        b'0'..=b'9' => Some(b - b'0'),
        b'a'..=b'f' => Some(b - b'a' + 10),
        b'A'..=b'F' => Some(b - b'A' + 10),
        _ => None,
    }
}

/// How a package's source was (or was not) located.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageKind {
    /// A registered package installed in a depot (`git-tree-sha1` present).
    Registered,
    /// A `dev`'d package referenced by `path`.
    Dev,
    /// A standard-library package (no `git-tree-sha1`, no `path`). Its source
    /// lives in the Julia installation, resolved by the later Base/stdlib work.
    Stdlib,
}

/// A single pinned package from the manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Package {
    pub name: String,
    pub uuid: Uuid,
    pub version: Option<String>,
    pub tree_sha1: Option<String>,
    pub deps: Vec<String>,
    pub kind: PackageKind,
    /// The resolved package root (the directory that contains `src/`), if
    /// determinable. `None` for stdlib packages and for registered packages not
    /// found in any depot.
    pub source: Option<PathBuf>,
}

/// Which discovery strategy located the environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnvSource {
    JuliaProject,
    WorkspaceWalkUp,
    DefaultEnv,
}

/// A located Julia installation whose plain Base/stdlib sources fatou can
/// harvest. Found from the filesystem alone, without running Julia.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JuliaInstall {
    /// The installation prefix (`<prefix>/bin/julia`, `<prefix>/share/julia`).
    pub prefix: PathBuf,
    /// `<prefix>/share/julia`.
    pub share: PathBuf,
    /// `<share>/base`, holding `Base.jl`, `boot.jl`, `exports.jl`, etc.
    pub base_dir: PathBuf,
    /// `<share>/stdlib/vX.Y`, holding one directory per standard-library package.
    pub stdlib_dir: PathBuf,
    /// The stdlib version, e.g. `1.11`, taken from the `stdlib/vX.Y` directory.
    pub version: String,
}

/// A resolved Julia environment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Environment {
    pub project_file: PathBuf,
    pub project_dir: PathBuf,
    pub manifest_file: Option<PathBuf>,
    pub name: Option<String>,
    pub uuid: Option<Uuid>,
    pub direct_deps: BTreeMap<String, Uuid>,
    pub packages: Vec<Package>,
    pub depots: Vec<PathBuf>,
    pub source: EnvSource,
    /// The Julia installation whose Base/stdlib sources back this environment,
    /// if one could be located. `None` falls back to the baked-in export list.
    pub install: Option<JuliaInstall>,
}

/// The package under development: the workspace's own `Project.toml` package,
/// whose `src/` tree fatou indexes like a depot package so its top-level symbols
/// resolve across the package's files. `root` is the directory containing `src/`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DevPackage {
    pub name: String,
    pub root: PathBuf,
}

impl Environment {
    /// The package under development, if this environment *is* a package project
    /// (a named `Project.toml` with a matching `src/<Name>.jl` entry file). A
    /// bare shared environment (`DefaultEnv`) or a nameless project has none.
    ///
    /// The entry-file check is what distinguishes a package project from a plain
    /// environment that merely carries a `name`; only the former has a module
    /// tree to harvest.
    pub fn dev_package(&self) -> Option<DevPackage> {
        if self.source == EnvSource::DefaultEnv {
            return None;
        }
        let name = self.name.as_ref()?;
        let entry = self.project_dir.join("src").join(format!("{name}.jl"));
        if entry.is_file() {
            Some(DevPackage {
                name: name.clone(),
                root: self.project_dir.clone(),
            })
        } else {
            None
        }
    }
}

/// Everything environment-dependent, injected so resolution stays testable
/// (no direct `std::env`/`$HOME` reads in the logic).
#[derive(Debug, Clone)]
pub struct EnvContext {
    pub workspace_root: PathBuf,
    pub julia_project: Option<String>,
    pub julia_depot_path: Option<String>,
    pub home: Option<PathBuf>,
    /// `JULIA_BINDIR`: an explicit `<prefix>/bin` override for locating the
    /// installation, taking precedence over juliaup and `PATH`.
    pub julia_bindir: Option<String>,
    /// The process `PATH`, searched for the `julia` executable as a last resort.
    pub path: Option<String>,
}

impl EnvContext {
    /// Build a context from the process environment for the given workspace.
    pub fn from_process(workspace_root: PathBuf) -> Self {
        Self {
            workspace_root,
            julia_project: std::env::var("JULIA_PROJECT").ok(),
            julia_depot_path: std::env::var("JULIA_DEPOT_PATH").ok(),
            home: std::env::var_os("HOME").map(PathBuf::from),
            julia_bindir: std::env::var("JULIA_BINDIR").ok(),
            path: std::env::var("PATH").ok(),
        }
    }
}

#[derive(Debug)]
pub enum EnvironmentError {
    Read { path: PathBuf, message: String },
    Parse { path: PathBuf, message: String },
}

impl std::fmt::Display for EnvironmentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EnvironmentError::Read { path, message } => {
                write!(f, "failed to read {}: {message}", path.display())
            }
            EnvironmentError::Parse { path, message } => {
                write!(f, "failed to parse {}: {message}", path.display())
            }
        }
    }
}

impl std::error::Error for EnvironmentError {}

const PROJECT_NAMES: [&str; 2] = ["JuliaProject.toml", "Project.toml"];
const MANIFEST_NAMES: [&str; 2] = ["JuliaManifest.toml", "Manifest.toml"];

/// Whether `path` names a file that steers environment resolution: a project
/// file (`Project.toml`/`JuliaProject.toml`) or a manifest (`Manifest.toml`/
/// `JuliaManifest.toml`, or a version-specific `Manifest-vX.Y.toml`). The LSP
/// uses this to escalate a watched-file change to a full environment
/// re-resolve instead of a workspace re-harvest.
pub fn is_environment_file(path: &Path) -> bool {
    let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
        return false;
    };
    PROJECT_NAMES.contains(&name)
        || MANIFEST_NAMES.contains(&name)
        || name
            .strip_prefix("Manifest-v")
            .and_then(|rest| rest.strip_suffix(".toml"))
            .and_then(parse_version)
            .is_some()
}

/// Resolve the active Julia environment for `ctx`. Returns `Ok(None)` when no
/// project can be located by any strategy.
pub fn resolve(ctx: &EnvContext) -> Result<Option<Environment>, EnvironmentError> {
    let depots = depot_roots(ctx);
    let Some((project_file, source)) = locate_project(ctx, &depots) else {
        return Ok(None);
    };
    let project_dir = project_file
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or_default();

    let (name, uuid, direct_deps) = parse_project(&project_file)?;
    let manifest_file = find_manifest(&project_dir);
    let mut packages = match &manifest_file {
        Some(path) => parse_manifest(path, &project_dir, &depots)?,
        None => Vec::new(),
    };

    let install = locate_install(ctx, &depots);
    if let Some(install) = &install {
        resolve_stdlib_sources(&mut packages, install);
    }

    Ok(Some(Environment {
        project_file,
        project_dir,
        manifest_file,
        name,
        uuid,
        direct_deps,
        packages,
        depots,
        source,
        install,
    }))
}

// --- Discovery -------------------------------------------------------------

/// Find the project file, following Julia's precedence.
fn locate_project(ctx: &EnvContext, depots: &[PathBuf]) -> Option<(PathBuf, EnvSource)> {
    if let Some(raw) = ctx.julia_project.as_deref() {
        let trimmed = raw.trim();
        if !trimmed.is_empty()
            && let Some(path) = from_julia_project(trimmed, ctx, depots)
        {
            return Some((path, EnvSource::JuliaProject));
        }
    }

    if let Some(path) = walk_up_for_project(&ctx.workspace_root) {
        return Some((path, EnvSource::WorkspaceWalkUp));
    }

    newest_default_env(ctx).map(|path| (path, EnvSource::DefaultEnv))
}

/// Interpret a `JULIA_PROJECT` value: `@.` (walk up), `@name` (shared env), or
/// a directory/file path.
fn from_julia_project(value: &str, ctx: &EnvContext, depots: &[PathBuf]) -> Option<PathBuf> {
    if value == "@." {
        return walk_up_for_project(&ctx.workspace_root);
    }
    if let Some(name) = value.strip_prefix('@') {
        return depots
            .iter()
            .find_map(|depot| project_file_in(&depot.join("environments").join(name)));
    }
    let path = PathBuf::from(value);
    if path.is_file() {
        return Some(path);
    }
    project_file_in(&path)
}

/// Walk up from `anchor` looking for a project file, à la `config::discover`.
fn walk_up_for_project(anchor: &Path) -> Option<PathBuf> {
    anchor.ancestors().find_map(project_file_in)
}

/// The project file within `dir`, honoring `JuliaProject.toml` precedence.
fn project_file_in(dir: &Path) -> Option<PathBuf> {
    PROJECT_NAMES
        .iter()
        .map(|name| dir.join(name))
        .find(|candidate| candidate.is_file())
}

/// The sibling manifest for a project directory, honoring name precedence and
/// falling back to the highest version-specific `Manifest-vX.Y.toml`.
fn find_manifest(project_dir: &Path) -> Option<PathBuf> {
    if let Some(path) = MANIFEST_NAMES
        .iter()
        .map(|name| project_dir.join(name))
        .find(|candidate| candidate.is_file())
    {
        return Some(path);
    }
    // Version-specific manifests (Julia 1.10.8+): pick the highest version.
    std::fs::read_dir(project_dir)
        .ok()?
        .filter_map(Result::ok)
        .filter_map(|entry| {
            let name = entry.file_name();
            let name = name.to_str()?;
            let version = name
                .strip_prefix("Manifest-v")?
                .strip_suffix(".toml")
                .and_then(parse_version)?;
            Some((version, entry.path()))
        })
        .max_by_key(|(version, _)| *version)
        .map(|(_, path)| path)
}

/// The newest `~/.julia/environments/vX.Y` project, by `(major, minor)`.
fn newest_default_env(ctx: &EnvContext) -> Option<PathBuf> {
    let envs = ctx.home.as_ref()?.join(".julia").join("environments");
    let dir = std::fs::read_dir(&envs)
        .ok()?
        .filter_map(Result::ok)
        .filter_map(|entry| {
            let name = entry.file_name();
            let version = parse_version(name.to_str()?.strip_prefix('v')?)?;
            Some((version, entry.path()))
        })
        .max_by_key(|(version, _)| *version)
        .map(|(_, path)| path)?;
    project_file_in(&dir)
}

/// Parse a `major.minor` (or longer) version prefix into a comparable tuple.
fn parse_version(s: &str) -> Option<(u32, u32)> {
    let mut parts = s.split('.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    Some((major, minor))
}

// --- Depots ----------------------------------------------------------------

/// The ordered depot roots: `JULIA_DEPOT_PATH` (empty entries expand to the
/// default), falling back to `~/.julia`.
fn depot_roots(ctx: &EnvContext) -> Vec<PathBuf> {
    let default = ctx.home.as_ref().map(|home| home.join(".julia"));
    match ctx.julia_depot_path.as_deref() {
        Some(raw) if !raw.trim().is_empty() => raw
            .split(depot_separator())
            .flat_map(|entry| {
                if entry.is_empty() {
                    default.clone()
                } else {
                    Some(PathBuf::from(entry))
                }
            })
            .collect(),
        _ => default.into_iter().collect(),
    }
}

const fn depot_separator() -> char {
    if cfg!(windows) { ';' } else { ':' }
}

// --- Julia installation ----------------------------------------------------

/// Locate the active Julia installation without running Julia, trying (in
/// order): the `JULIA_BINDIR` override, the juliaup default channel, then the
/// `julia` executable on `PATH`. Returns `None` when none resolves to a tree
/// with a readable `base/Base.jl`.
pub fn locate_install(ctx: &EnvContext, depots: &[PathBuf]) -> Option<JuliaInstall> {
    install_from_bindir(ctx)
        .or_else(|| install_from_juliaup(depots))
        .or_else(|| install_from_path(ctx))
}

/// `<JULIA_BINDIR>/../share/julia`.
fn install_from_bindir(ctx: &EnvContext) -> Option<JuliaInstall> {
    let bindir = ctx.julia_bindir.as_deref().map(str::trim)?;
    if bindir.is_empty() {
        return None;
    }
    let prefix = Path::new(bindir).parent()?;
    install_from_share(prefix.join("share").join("julia"))
}

/// Read `<depot>/juliaup/juliaup.json`, follow the default channel's version to
/// its install directory, and take its bundled `share/julia`.
fn install_from_juliaup(depots: &[PathBuf]) -> Option<JuliaInstall> {
    for depot in depots {
        let juliaup = depot.join("juliaup");
        let Ok(text) = std::fs::read_to_string(juliaup.join("juliaup.json")) else {
            continue;
        };
        let Ok(json) = serde_json::from_str::<serde_json::Value>(&text) else {
            continue;
        };
        let Some(install) = juliaup_install_dir(&json, &juliaup) else {
            continue;
        };
        if let Some(found) = install_from_share(install.join("share").join("julia")) {
            return Some(found);
        }
    }
    None
}

/// The default channel's install directory from a parsed `juliaup.json`, joined
/// under `juliaup/` (where the `Path` values are relative).
fn juliaup_install_dir(json: &serde_json::Value, juliaup: &Path) -> Option<PathBuf> {
    let default = json.get("Default")?.as_str()?;
    let version = json
        .get("InstalledChannels")?
        .get(default)?
        .get("Version")?
        .as_str()?;
    let rel = json
        .get("InstalledVersions")?
        .get(version)?
        .get("Path")?
        .as_str()?;
    Some(juliaup.join(rel))
}

/// Find `julia` on `PATH`, resolve symlinks and shell wrappers, and take
/// `<prefix>/share/julia` from the `<prefix>/bin/julia` layout.
fn install_from_path(ctx: &EnvContext) -> Option<JuliaInstall> {
    let path = ctx.path.as_deref()?;
    let exe = if cfg!(windows) { "julia.exe" } else { "julia" };
    let julia = path
        .split(depot_separator())
        .filter(|dir| !dir.is_empty())
        .map(|dir| Path::new(dir).join(exe))
        .find(|candidate| candidate.is_file())?;
    let real = std::fs::canonicalize(&julia).ok()?;
    // On NixOS (and some distros) `julia` is a shell wrapper that `exec`s the
    // real binary in a different prefix; follow the chain to the ELF.
    let real = std::fs::canonicalize(follow_wrapper(&real, 8)).unwrap_or(real);
    let prefix = real.parent()?.parent()?; // <prefix>/bin/julia -> <prefix>
    install_from_share(prefix.join("share").join("julia"))
}

/// Follow a chain of shell wrappers (each ending in `exec "<path>"`) to the real
/// executable, bounded by `depth`. Non-scripts and unparseable wrappers stop.
fn follow_wrapper(path: &Path, depth: u8) -> PathBuf {
    if depth == 0 || !is_shebang(path) {
        return path.to_path_buf();
    }
    match std::fs::read_to_string(path)
        .ok()
        .as_deref()
        .and_then(exec_target)
    {
        Some(target) if target != path => follow_wrapper(&target, depth - 1),
        _ => path.to_path_buf(),
    }
}

/// Whether `path`'s first two bytes are `#!` (a shell wrapper, not an ELF).
fn is_shebang(path: &Path) -> bool {
    let mut buf = [0u8; 2];
    std::fs::File::open(path)
        .and_then(|mut file| file.read_exact(&mut buf))
        .is_ok()
        && &buf == b"#!"
}

/// The target of the last `exec <path>` line in a wrapper script (quoted or a
/// bare first token).
fn exec_target(script: &str) -> Option<PathBuf> {
    let line = script
        .lines()
        .rev()
        .find(|line| line.trim_start().starts_with("exec "))?;
    let after = line.trim_start().strip_prefix("exec ")?.trim_start();
    let target = match after.strip_prefix('"') {
        Some(rest) => rest.split('"').next()?,
        None => after.split_whitespace().next()?,
    };
    (!target.is_empty()).then(|| PathBuf::from(target))
}

/// Build and validate a [`JuliaInstall`] from a candidate `share/julia`
/// directory: it must hold `base/Base.jl` and a `stdlib/vX.Y` directory.
fn install_from_share(share: PathBuf) -> Option<JuliaInstall> {
    let base_dir = share.join("base");
    if !base_dir.join("Base.jl").is_file() {
        return None;
    }
    let (version, stdlib_dir) = newest_stdlib(&share.join("stdlib"))?;
    let prefix = share.parent()?.parent()?.to_path_buf(); // <prefix>/share/julia
    Some(JuliaInstall {
        prefix,
        base_dir,
        stdlib_dir,
        version,
        share,
    })
}

/// The highest `vMAJOR.MINOR` directory under `stdlib/`, with its `X.Y` string.
fn newest_stdlib(stdlib: &Path) -> Option<(String, PathBuf)> {
    std::fs::read_dir(stdlib)
        .ok()?
        .filter_map(Result::ok)
        .filter_map(|entry| {
            let name = entry.file_name();
            let raw = name.to_str()?.strip_prefix('v')?;
            let version = parse_version(raw)?;
            Some((version, raw.to_string(), entry.path()))
        })
        .max_by_key(|(version, _, _)| *version)
        .map(|(_, raw, path)| (raw, path))
}

/// Point each stdlib package at its source under the installation's `stdlib`.
fn resolve_stdlib_sources(packages: &mut [Package], install: &JuliaInstall) {
    for pkg in packages
        .iter_mut()
        .filter(|p| p.kind == PackageKind::Stdlib)
    {
        let dir = install.stdlib_dir.join(&pkg.name);
        if dir.is_dir() {
            pkg.source = Some(dir);
        }
    }
}

// --- Project.toml ----------------------------------------------------------

type ProjectMeta = (Option<String>, Option<Uuid>, BTreeMap<String, Uuid>);

fn parse_project(path: &Path) -> Result<ProjectMeta, EnvironmentError> {
    let table = read_toml(path)?;
    let name = table
        .get("name")
        .and_then(|v| v.as_str())
        .map(str::to_string);
    let uuid = table
        .get("uuid")
        .and_then(|v| v.as_str())
        .and_then(|s| s.parse().ok());
    let mut direct_deps = BTreeMap::new();
    if let Some(deps) = table.get("deps").and_then(|v| v.as_table()) {
        for (dep_name, value) in deps {
            if let Some(uuid) = value.as_str().and_then(|s| s.parse().ok()) {
                direct_deps.insert(dep_name.clone(), uuid);
            }
        }
    }
    Ok((name, uuid, direct_deps))
}

// --- Manifest.toml ---------------------------------------------------------

fn parse_manifest(
    path: &Path,
    project_dir: &Path,
    depots: &[PathBuf],
) -> Result<Vec<Package>, EnvironmentError> {
    let table = read_toml(path)?;
    let mut packages = Vec::new();

    // Format 2.0 nests entries under a top-level `deps` table; format 1.0 puts
    // each package array at the top level.
    if let Some(deps) = table.get("deps").and_then(|v| v.as_table()) {
        for (name, value) in deps {
            collect_entries(name, value, project_dir, depots, &mut packages);
        }
    } else {
        for (name, value) in &table {
            collect_entries(name, value, project_dir, depots, &mut packages);
        }
    }

    packages.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(packages)
}

/// Push every package entry in `value` (an array of tables) into `packages`.
fn collect_entries(
    name: &str,
    value: &toml::Value,
    project_dir: &Path,
    depots: &[PathBuf],
    packages: &mut Vec<Package>,
) {
    let Some(entries) = value.as_array() else {
        return;
    };
    for entry in entries {
        if let Some(table) = entry.as_table()
            && let Some(package) = parse_entry(name, table, project_dir, depots)
        {
            packages.push(package);
        }
    }
}

fn parse_entry(
    name: &str,
    table: &toml::Table,
    project_dir: &Path,
    depots: &[PathBuf],
) -> Option<Package> {
    let uuid: Uuid = table.get("uuid").and_then(|v| v.as_str())?.parse().ok()?;
    let version = table
        .get("version")
        .and_then(|v| v.as_str())
        .map(str::to_string);
    let tree_sha1 = table
        .get("git-tree-sha1")
        .and_then(|v| v.as_str())
        .map(str::to_string);
    let path = table.get("path").and_then(|v| v.as_str());
    let deps = extract_deps(table.get("deps"));

    let kind = if path.is_some() {
        PackageKind::Dev
    } else if tree_sha1.is_some() {
        PackageKind::Registered
    } else {
        PackageKind::Stdlib
    };

    let source = match kind {
        PackageKind::Dev => Some(resolve_dev_path(project_dir, path?)),
        PackageKind::Registered => resolve_registered(name, uuid, tree_sha1.as_deref()?, depots),
        PackageKind::Stdlib => None,
    };

    Some(Package {
        name: name.to_string(),
        uuid,
        version,
        tree_sha1,
        deps,
        kind,
        source,
    })
}

/// A package's `deps` may be an array of names or a table (name -> uuid).
fn extract_deps(value: Option<&toml::Value>) -> Vec<String> {
    match value {
        Some(toml::Value::Array(arr)) => arr
            .iter()
            .filter_map(|v| v.as_str().map(str::to_string))
            .collect(),
        Some(toml::Value::Table(table)) => table.keys().cloned().collect(),
        _ => Vec::new(),
    }
}

/// Resolve a `dev`'d package's root relative to the project directory.
fn resolve_dev_path(project_dir: &Path, path: &str) -> PathBuf {
    let path = Path::new(path);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        project_dir.join(path)
    }
}

/// Locate a registered package's root by computing its version slug and probing
/// each depot in order.
fn resolve_registered(
    name: &str,
    uuid: Uuid,
    tree_sha1: &str,
    depots: &[PathBuf],
) -> Option<PathBuf> {
    let sha1 = parse_sha1(tree_sha1)?;
    let slug = version_slug(uuid, &sha1);
    depots
        .iter()
        .map(|depot| depot.join("packages").join(name).join(&slug))
        .find(|candidate| candidate.is_dir())
}

// --- Slug computation ------------------------------------------------------

const SLUG_CHARS: &[u8; 62] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

/// Julia's `version_slug(uuid, sha1)`: CRC-32C over the UUID's little-endian
/// bytes, continued over the tree hash, then base-62 encoded to 5 characters.
fn version_slug(uuid: Uuid, sha1: &[u8]) -> String {
    // Julia hashes the UUID's native (little-endian) in-memory representation,
    // which is the textual byte order reversed.
    let mut uuid_le = uuid.bytes();
    uuid_le.reverse();
    let crc = crc32c(&uuid_le, 0);
    let crc = crc32c(sha1, crc);
    slug(crc, 5)
}

fn slug(mut value: u32, len: usize) -> String {
    let base = SLUG_CHARS.len() as u32;
    let mut out = String::with_capacity(len);
    for _ in 0..len {
        let digit = (value % base) as usize;
        value /= base;
        out.push(SLUG_CHARS[digit] as char);
    }
    out
}

/// CRC-32C (Castagnoli), reflected, chainable via `crc`.
fn crc32c(bytes: &[u8], crc: u32) -> u32 {
    const POLY: u32 = 0x82F6_3B78;
    let mut c = !crc;
    for &byte in bytes {
        c ^= byte as u32;
        for _ in 0..8 {
            c = if c & 1 != 0 { (c >> 1) ^ POLY } else { c >> 1 };
        }
    }
    !c
}

/// Parse a 40-hex-character SHA1 into 20 bytes (textual order).
fn parse_sha1(s: &str) -> Option<[u8; 20]> {
    let mut bytes = [0u8; 20];
    let mut nibbles = s.bytes();
    for byte in bytes.iter_mut() {
        let hi = nibbles.next().and_then(hex_val)?;
        let lo = nibbles.next().and_then(hex_val)?;
        *byte = (hi << 4) | lo;
    }
    if nibbles.next().is_some() {
        return None;
    }
    Some(bytes)
}

// --- Shared helpers --------------------------------------------------------

fn read_toml(path: &Path) -> Result<toml::Table, EnvironmentError> {
    let text = std::fs::read_to_string(path).map_err(|err| EnvironmentError::Read {
        path: path.to_path_buf(),
        message: err.to_string(),
    })?;
    text.parse::<toml::Table>()
        .map_err(|err| EnvironmentError::Parse {
            path: path.to_path_buf(),
            message: err.to_string(),
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_uuid_roundtrip() {
        let uuid: Uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c".parse().unwrap();
        assert_eq!(uuid.bytes()[0], 0x15);
        assert_eq!(uuid.bytes()[15], 0x5c);
    }

    #[test]
    fn rejects_malformed_uuid() {
        assert!("not-a-uuid".parse::<Uuid>().is_err());
        assert!("1520ce14".parse::<Uuid>().is_err());
    }

    #[test]
    fn crc32c_empty_is_zero() {
        assert_eq!(crc32c(b"", 0), 0);
    }

    #[test]
    fn crc32c_chains() {
        assert_eq!(
            crc32c(b"world", crc32c(b"hello ", 0)),
            crc32c(b"hello world", 0)
        );
    }

    /// Golden vector against a real depot entry:
    /// `AbstractTrees` -> on-disk slug `Ftf8W`.
    #[test]
    fn version_slug_golden() {
        let uuid: Uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c".parse().unwrap();
        let sha1 = parse_sha1("2d9c9a55f9c93e8887ad391fbae72f8ef55e1177").unwrap();
        assert_eq!(version_slug(uuid, &sha1), "Ftf8W");
    }

    #[test]
    fn extract_deps_array_and_table() {
        let value: toml::Value = toml::from_str("deps = [\"A\", \"B\"]").unwrap();
        assert_eq!(extract_deps(value.get("deps")), vec!["A", "B"]);

        let value: toml::Value = toml::from_str("[deps]\nA = \"x\"\nB = \"y\"").unwrap();
        let mut got = extract_deps(value.get("deps"));
        got.sort();
        assert_eq!(got, vec!["A", "B"]);
    }

    #[test]
    fn classifies_manifest_entries() {
        let text = r#"
            julia_version = "1.11.7"
            manifest_format = "2.0"

            [[deps.AbstractTrees]]
            git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177"
            uuid = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
            version = "0.4.5"

            [[deps.Dates]]
            uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

            [[deps.Local]]
            deps = ["Dates"]
            path = "vendor/Local"
            uuid = "00000000-0000-0000-0000-000000000001"
        "#;
        let table: toml::Table = text.parse().unwrap();
        let project_dir = Path::new("/proj");
        let deps = table.get("deps").and_then(|v| v.as_table()).unwrap();
        let mut packages = Vec::new();
        for (name, value) in deps {
            collect_entries(name, value, project_dir, &[], &mut packages);
        }
        packages.sort_by(|a, b| a.name.cmp(&b.name));

        assert_eq!(packages.len(), 3);
        let by_name = |n: &str| packages.iter().find(|p| p.name == n).unwrap();

        assert_eq!(by_name("AbstractTrees").kind, PackageKind::Registered);
        assert_eq!(by_name("Dates").kind, PackageKind::Stdlib);
        assert_eq!(by_name("Dates").source, None);

        let local = by_name("Local");
        assert_eq!(local.kind, PackageKind::Dev);
        assert_eq!(local.deps, vec!["Dates"]);
        assert_eq!(local.source, Some(PathBuf::from("/proj/vendor/Local")));
    }

    #[test]
    fn depot_roots_fall_back_to_home() {
        let ctx = EnvContext {
            workspace_root: PathBuf::from("/ws"),
            julia_project: None,
            julia_depot_path: None,
            home: Some(PathBuf::from("/home/u")),
            julia_bindir: None,
            path: None,
        };
        assert_eq!(depot_roots(&ctx), vec![PathBuf::from("/home/u/.julia")]);
    }

    #[test]
    fn depot_roots_expand_empty_entry_to_default() {
        let sep = depot_separator();
        let ctx = EnvContext {
            workspace_root: PathBuf::from("/ws"),
            julia_project: None,
            julia_depot_path: Some(format!("/custom{sep}")),
            home: Some(PathBuf::from("/home/u")),
            julia_bindir: None,
            path: None,
        };
        assert_eq!(
            depot_roots(&ctx),
            vec![PathBuf::from("/custom"), PathBuf::from("/home/u/.julia")]
        );
    }

    #[test]
    fn classifies_environment_files() {
        for name in [
            "Project.toml",
            "JuliaProject.toml",
            "Manifest.toml",
            "JuliaManifest.toml",
            "Manifest-v1.11.toml",
        ] {
            assert!(
                is_environment_file(&PathBuf::from("/ws").join(name)),
                "{name} steers resolution"
            );
        }
        for name in ["a.jl", "Cargo.toml", "Manifest-vX.toml", "Manifest-v1.11"] {
            assert!(
                !is_environment_file(&PathBuf::from("/ws").join(name)),
                "{name} does not steer resolution"
            );
        }
    }

    #[test]
    fn parse_version_orders_correctly() {
        assert!(parse_version("1.11") > parse_version("1.7"));
        assert!(parse_version("2.0") > parse_version("1.99"));
        assert_eq!(parse_version("nope"), None);
    }
}