zv 0.6.0

Ziglang Version Manager and Project Starter
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
use crate::{ArchiveExt, ResolvedZigVersion, Result, Shim, ZvError, app::utils::ProgressHandle};
use color_eyre::eyre::{Context, eyre};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tokio::fs;
const TARGET: &str = "zv::app::toolchain";

/// An entry representing an installed Zig version
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ZigInstall {
    /// The semantic version of this installation
    pub version: semver::Version,
    /// Path to the root directory of this installation
    pub path: PathBuf,
    /// Whether this installation is from the "master" nested directory
    pub is_master: bool,
}

#[derive(Debug, Clone)]
pub struct ToolchainManager {
    versions_path: PathBuf,
    installations: Vec<ZigInstall>,
    active_install: Option<ZigInstall>,
    bin_path: PathBuf,
    active_file: PathBuf,
}

impl ToolchainManager {
    pub async fn new(zv_root: impl AsRef<Path>) -> Result<Self, ZvError> {
        let zv_root = zv_root.as_ref().to_path_buf();
        let versions_path = zv_root.join("versions");
        let bin_path = zv_root.join("bin");
        let active_file = zv_root.join("active.json");

        // discover what is on disk
        let installations =
            Self::scan_installations(&versions_path).map_err(ZvError::ZvAppInitError)?;

        // Helper function to find the best fallback version from installations
        let find_fallback_install = |installations: &[ZigInstall]| -> Option<ZigInstall> {
            if installations.is_empty() {
                return None;
            }

            // Prefer highest stable version over master
            let fallback = installations
                .iter()
                .filter(|i| !i.is_master)
                .max_by(|a, b| a.version.cmp(&b.version))
                .or_else(|| {
                    // If no stable versions, use highest master version
                    installations
                        .iter()
                        .filter(|i| i.is_master)
                        .max_by(|a, b| a.version.cmp(&b.version))
                })
                .cloned();

            if let Some(ref zi) = fallback {
                let json =
                    serde_json::to_vec(zi).expect("ZigInstall serialization should never fail");
                if let Err(e) = std::fs::write(&active_file, json) {
                    tracing::error!(target: TARGET, "Failed to write fallback active install to file: {}", e);
                }
            }

            fallback
        };

        // load last active install
        let active_install = if active_file.is_file() {
            match fs::read(&active_file).await {
                Ok(bytes) => {
                    match serde_json::from_slice::<ZigInstall>(&bytes) {
                        Ok(zig_install) => {
                            // Verify the install exists in our installations list
                            let exists = installations.iter().any(|i| *i == zig_install);

                            if exists {
                                Some(zig_install)
                            } else {
                                tracing::debug!(target: TARGET,
                                    "Active install from file not found in installations, using fallback"
                                );
                                find_fallback_install(&installations)
                            }
                        }
                        Err(err) => {
                            tracing::debug!(target: TARGET,
                                "Failed to deserialize active install file {}: {}, using fallback",
                                active_file.display(),
                                err
                            );
                            find_fallback_install(&installations)
                        }
                    }
                }
                Err(io_err) => {
                    tracing::debug!(target: TARGET,
                        "Failed to read active install file {}: {}, using fallback",
                        active_file.display(),
                        io_err
                    );
                    find_fallback_install(&installations)
                }
            }
        } else {
            find_fallback_install(&installations)
        };

        let toolchain_manager = Self {
            versions_path,
            installations,
            active_install,
            bin_path,
            active_file,
        };

        Ok(toolchain_manager)
    }
    /// Scan installations in `versions_path` and return a sorted list of found [ZigInstall]s
    pub(crate) fn scan_installations(versions_path: &Path) -> Result<Vec<ZigInstall>> {
        use walkdir::WalkDir;

        let mut out = Vec::new();
        if !versions_path.is_dir() {
            return Ok(out);
        }

        let zig_exe = Shim::Zig.executable_name();

        // Walk only 2 levels deep: versions/*  or  versions/master/*
        for entry in WalkDir::new(versions_path)
            .min_depth(1)
            .max_depth(2)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_dir())
        {
            let path = entry.path();
            let depth = entry.depth();

            // case 1: depth 1 bare semver  ->  versions/0.13.0
            if depth == 1
                && let Some(ver) = path
                    .file_name()
                    .and_then(|s| s.to_str())
                    .and_then(|s| s.parse::<semver::Version>().ok())
            {
                let zig_bin = path.join(zig_exe);
                if zig_bin.is_file() {
                    out.push(ZigInstall {
                        version: ver,
                        path: path.to_path_buf(),
                        is_master: false,
                    });
                }
            }

            // case 2: depth 2 inside master  ->  versions/master/0.13.0
            if depth == 2
                && path.parent().unwrap().file_name() == Some(std::ffi::OsStr::new("master"))
                && let Some(ver) = path
                    .file_name()
                    .and_then(|s| s.to_str())
                    .and_then(|s| s.parse::<semver::Version>().ok())
            {
                let zig_bin = path.join(zig_exe);
                if zig_bin.is_file() {
                    out.push(ZigInstall {
                        version: ver,
                        path: path.to_path_buf(),
                        is_master: true,
                    });
                }
            }
        }

        out.sort_by(|a, b| a.version.cmp(&b.version));
        Ok(out)
    }

    /// Check if a specific version is installed
    pub fn is_version_installed(&self, rzv: &ResolvedZigVersion) -> Option<PathBuf> {
        let (is_master, version) = (rzv.is_master(), rzv.version());
        let base = if is_master {
            self.versions_path.join("master").join(version.to_string())
        } else {
            self.versions_path.join(version.to_string())
        };
        if !base.is_dir() {
            return None;
        }
        let zig = base.join(Shim::Zig.executable_name());
        if zig.is_file() { Some(zig) } else { None }
    }

    /// Install a Zig version from a downloaded archive
    pub async fn install_version(
        &mut self,
        archive_path: &Path,
        version: &semver::Version,
        ext: ArchiveExt,
        is_master: bool,
    ) -> Result<PathBuf> {
        const TARGET: &str = "zv::toolchain";

        let install_destination = if is_master {
            self.versions_path.join("master").join(version.to_string())
        } else {
            self.versions_path.join(version.to_string())
        };
        tracing::debug!(target: TARGET, %version, is_master, dest = %install_destination.display(), "Installation destination");

        let archive_tmp = self.versions_path.join("archive_tmp");
        if archive_tmp.exists() {
            fs::remove_dir_all(&archive_tmp).await?;
        }
        fs::create_dir_all(&archive_tmp).await?;
        let progress_handle = ProgressHandle::spawn();
        let bytes = fs::read(archive_path).await?;
        let archive_name = archive_path
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "zig archive".to_string());
        // extract archive
        match ext {
            ArchiveExt::TarXz => {
                let _ = progress_handle
                    .start(format!("Extracting {archive_name}"))
                    .await;
                let xz = xz2::read::XzDecoder::new(std::io::Cursor::new(bytes));
                let mut ar = tar::Archive::new(xz);
                if let Err(e) = ar.unpack(&archive_tmp) {
                    let _ = progress_handle
                        .finish_with_error("Failed to extract tar.xz archive")
                        .await;
                    return Err(e.into());
                }
            }
            ArchiveExt::Zip => {
                let _ = progress_handle
                    .start(format!("Extracting {archive_name}"))
                    .await;
                let mut ar = match zip::ZipArchive::new(std::io::Cursor::new(bytes)) {
                    Ok(ar) => ar,
                    Err(e) => {
                        let _ = progress_handle
                            .finish_with_error("Failed to open zip archive")
                            .await;
                        return Err(e.into());
                    }
                };
                for i in 0..ar.len() {
                    let mut file = match ar.by_index(i) {
                        Ok(file) => file,
                        Err(e) => {
                            let _ = progress_handle
                                .finish_with_error("Failed to read zip entry")
                                .await;
                            return Err(e.into());
                        }
                    };
                    let out = archive_tmp.join(file.name());
                    if file.is_dir() {
                        if let Err(e) = fs::create_dir_all(&out).await {
                            let _ = progress_handle
                                .finish_with_error("Failed to create directory during extraction")
                                .await;
                            return Err(e.into());
                        }
                    } else {
                        if let Some(p) = out.parent()
                            && let Err(e) = fs::create_dir_all(p).await
                        {
                            let _ = progress_handle
                                .finish_with_error(
                                    "Failed to create parent directory during extraction",
                                )
                                .await;
                            return Err(e.into());
                        }
                        let mut w = match std::fs::File::create(&out) {
                            Ok(w) => w,
                            Err(e) => {
                                let _ = progress_handle
                                    .finish_with_error("Failed to create file during extraction")
                                    .await;
                                return Err(e.into());
                            }
                        };
                        if let Err(e) = std::io::copy(&mut file, &mut w) {
                            let _ = progress_handle
                                .finish_with_error("Failed to write file during extraction")
                                .await;
                            return Err(e.into());
                        }
                    }
                }
            }
        }
        let _ = progress_handle.finish("Extraction complete").await;
        // strip wrapper directory
        let mut entries = fs::read_dir(&archive_tmp).await?;
        let mut top_dirs = Vec::new();
        while let Some(entry) = entries.next_entry().await? {
            if entry.file_type().await?.is_dir() {
                top_dirs.push(entry.path());
            }
        }
        let actual_root = match top_dirs.len() {
            1 => top_dirs.into_iter().next().unwrap(), // wrapper dir
            _ => archive_tmp.clone(),                  // already flat
        };

        // --- 5.  sanity check
        let zig_bin = actual_root.join(Shim::Zig.executable_name());
        if !zig_bin.is_file() {
            let _ = fs::remove_dir_all(&archive_tmp).await;
            return Err(eyre!("Zig executable not found after installation"));
        }

        // promote to final location
        if install_destination.exists() {
            fs::remove_dir_all(&install_destination).await?;
        }

        // Move contents of actual_root, not the directory itself
        if actual_root != archive_tmp {
            // We have a wrapper directory - move its contents to the install destination
            fs::create_dir_all(&install_destination).await?;
            let mut entries = fs::read_dir(&actual_root).await?;
            while let Some(entry) = entries.next_entry().await? {
                let src = entry.path();
                let dst = install_destination.join(entry.file_name());
                fs::rename(&src, &dst).await?;
            }
            fs::remove_dir_all(&archive_tmp).await.ok();
        } else {
            // Already flat - move the entire directory
            fs::rename(&archive_tmp, &install_destination).await?;
        }

        // update cache
        let new_install = ZigInstall {
            version: version.clone(),
            path: install_destination.clone(),
            is_master,
        };
        let exe_path = new_install.path.join(Shim::Zig.executable_name());
        match self
            .installations
            .binary_search_by(|i| i.version.cmp(version))
        {
            Ok(pos) => self.installations[pos] = new_install,
            Err(pos) => self.installations.insert(pos, new_install),
        }

        Ok(exe_path)
    }

    /// Sets the active Zig version, updating the shims in bin/ and writing to the active file
    pub async fn set_active_version(&mut self, rzv: &ResolvedZigVersion) -> Result<()> {
        let version = rzv.version();
        tracing::debug!(target: TARGET, %version, "Setting active version");
        let install = self
            .installations
            .iter()
            .find(|i| &i.version == version)
            .ok_or_else(|| eyre!("Version {} is not installed", version))?;

        tracing::debug!(target: TARGET, install_path = %install.path.display(), "Found installation, deploying shims");
        self.deploy_shims(install, false, false).await?;

        let json = serde_json::to_vec(&install)
            .wrap_err("Failed to serialize Zig install for active file")?;
        fs::write(&self.active_file, json).await?;
        self.active_install = Some(install.clone());

        tracing::trace!(target: TARGET, %version, "Set active Zig version");
        Ok(())
    }
    /// Sets the active Zig version, updating the shims in bin/ and writing to the active file
    /// Optionally provide the installed path to skip re-checking installation
    pub async fn set_active_version_with_path(
        &mut self,
        rzv: &ResolvedZigVersion,
        installed_path: PathBuf,
    ) -> Result<()> {
        // installed_path is the full path to zig.exe, we need the directory containing it
        let install_dir = installed_path
            .parent()
            .ok_or_else(|| eyre!("Invalid installed path: {}", installed_path.display()))?
            .to_path_buf();

        tracing::debug!(target: TARGET, version = %rzv.version(), install_dir = %install_dir.display(), "Setting active version with path");
        let zig_install = ZigInstall {
            version: rzv.version().clone(),
            path: install_dir,
            is_master: rzv.is_master(),
        };
        tracing::debug!(target: TARGET, "Deploying shims");
        self.deploy_shims(&zig_install, false, false).await?;
        let json = serde_json::to_vec(&zig_install)
            .wrap_err("Failed to serialize Zig install for active file")?;
        fs::write(&self.active_file, json).await?;
        self.active_install = Some(zig_install.clone());
        tracing::trace!(target: TARGET, version = ?rzv.version().to_string(), "Set active Zig completed");
        Ok(())
    }
    /// Validates that the zv binary exists in the bin directory
    /// Similar to setup logic - checks existence and warns about checksum mismatches but continues
    fn validate_zv_binary(&self) -> Result<PathBuf> {
        use crate::tools::files_have_same_hash;

        let zv_bin_path = self.bin_path.join(Shim::Zv.executable_name());

        // Check if zv binary exists
        if !zv_bin_path.exists() {
            return Err(eyre!(
                "zv binary not found in bin directory: {}",
                self.bin_path.display()
            ))
            .inspect_err(|_| {
                println!(
                    "Run {} or {} to synchronize zv with ZV_DIR/bin/zv",
                    yansi::Paint::cyan("zv setup"),
                    yansi::Paint::cyan("zv sync")
                )
            });
        }

        // Get current executable for comparison
        let current_exe =
            std::env::current_exe().wrap_err("Failed to get current executable path")?;

        // Compare checksums like setup does
        match files_have_same_hash(&current_exe, &zv_bin_path) {
            Ok(true) => {
                tracing::debug!(target: TARGET, zv_path = %zv_bin_path.display(), "Validated zv binary (checksum match)");
            }
            Ok(false) => {
                tracing::warn!(target: TARGET,
                    current_exe = %current_exe.display(),
                    zv_path = %zv_bin_path.display(),
                    "zv versions mismatch (checksum) - created zig/zls installations may not perform correctly. Please run `zv setup`"
                );
            }
            Err(e) => {
                tracing::warn!(target: TARGET,
                    "zv versions mismatch (checksum comparison failed: {}) - created zig/zls installations may not perform correctly. Please run `zv setup`", e
                );
            }
        }

        tracing::debug!(target: TARGET, zv_path = %zv_bin_path.display(), "Using zv binary from bin directory");
        Ok(zv_bin_path)
    }

    /// Deploys or updates the proxy shims (zig, zls) in bin/ that link to zv
    pub async fn deploy_shims(
        &self,
        install: &ZigInstall,
        skip_zv_bin_check: bool,
        quiet: bool,
    ) -> Result<()> {
        let zv_path = if !skip_zv_bin_check {
            // Validate that zv binary exists
            self.validate_zv_binary()?
        } else {
            self.bin_path.join(Shim::Zv.executable_name())
        };

        tracing::debug!(target: TARGET, install_path = %install.path.display(), "Deploying shims for installation");

        self.create_shim(&zv_path, Shim::Zig).await?;
        // TODO: ZLS support is unimplemented
        // self.create_shim(&zv_path, Shim::Zls).await?;
        if !quiet {
            tracing::info!(target: TARGET, "Successfully deployed zig version {}", install.version);
        }
        Ok(())
    }

    /// Creates a single shim (hard link or symlink) to the zv binary
    async fn create_shim(&self, zv_path: &Path, shim: Shim) -> Result<()> {
        let shim_path = self.bin_path.join(shim.executable_name());

        tracing::trace!(target: TARGET,
            shim = shim.executable_name(),
            zv_path = %zv_path.display(),
            shim_path = %shim_path.display(),
            "Creating shim"
        );

        // Check if shim already exists and points to the correct zv binary
        if self.is_valid_shim(&shim_path, zv_path)? {
            tracing::trace!(target: TARGET, "Shim {} already exists and is valid, skipping", shim.executable_name());
            return Ok(());
        }

        // Remove existing file/symlink if it exists
        if shim_path.exists() || shim_path.is_symlink() {
            fs::remove_file(&shim_path).await?;
        }

        tracing::info!(target: TARGET,
            shim = shim.executable_name(),
            "Creating shim {} -> {}",
            shim_path.display(),
            zv_path.display()
        );

        #[cfg(unix)]
        tokio::fs::symlink(zv_path, &shim_path).await?;

        #[cfg(windows)]
        {
            match tokio::fs::symlink_file(zv_path, &shim_path).await {
                Ok(()) => {
                    tracing::debug!(target: TARGET, "Created symlink successfully for {}", shim.executable_name());
                }
                Err(symlink_err) => {
                    tracing::debug!(target: TARGET, "Symlink failed for {}: {}, trying hard link", shim.executable_name(), symlink_err);
                    std::fs::hard_link(zv_path, &shim_path).wrap_err_with(|| {
                        format!(
                            "Failed to create hard link from {} to {}",
                            zv_path.display(),
                            shim_path.display()
                        )
                    })?;
                    tracing::debug!(target: TARGET, "Created hard link successfully for {}", shim.executable_name());
                }
            }
        }

        Ok(())
    }

    /// Checks if a shim file exists and points to the correct zv binary
    fn is_valid_shim(&self, shim_path: &Path, zv_path: &Path) -> Result<bool> {
        use same_file::Handle;

        if !shim_path.exists() {
            return Ok(false);
        }

        let zv_handle =
            Handle::from_path(zv_path).wrap_err("Failed to create handle for zv binary")?;

        // Check for hard links
        if let Ok(shim_handle) = Handle::from_path(shim_path)
            && shim_handle == zv_handle
        {
            return Ok(true);
        }

        // Check for symlinks
        if shim_path.is_symlink()
            && let Ok(target) = std::fs::read_link(shim_path)
        {
            let resolved_target = if target.is_absolute() {
                target
            } else {
                shim_path.parent().unwrap_or(shim_path).join(&target)
            };

            if let Ok(target_handle) = Handle::from_path(&resolved_target)
                && target_handle == zv_handle
            {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get the currently active installation, if any
    pub fn get_active_install(&self) -> Option<&ZigInstall> {
        self.active_install.as_ref()
    }
    /// List all installed versions, returning a tuple of (version, is_active, is_master)
    pub fn list_installations(&self) -> Vec<(semver::Version, bool, bool)> {
        self.installations
            .iter()
            .map(|i| {
                let active = self
                    .active_install
                    .as_ref()
                    .is_some_and(|a| a.version == i.version);
                (i.version.clone(), active, i.is_master)
            })
            .collect()
    }

    /// Check if there are any installed versions
    /// Returns `true`` if no installations are available, `false` otherwise.
    pub fn installations_empty(&self) -> bool {
        self.installations.is_empty()
    }
    /// Clear the active version without setting a new one
    pub fn clear_active_version(&mut self) -> Result<()> {
        if self.active_file.exists() {
            if let Err(e) = std::fs::remove_file(&self.active_file) {
                tracing::warn!(target: TARGET, "Failed to remove active version file: {}", e);
                return Err(eyre!(e).wrap_err("Failed to remove active version file"));
            }
        }
        Ok(())
    }
}