Skip to main content

lux_lib/operations/
pin.rs

1use crate::lockfile::locked_package;
2use crate::package;
3use std::io;
4
5use crate::{
6    fs,
7    lockfile::{FlushLockfileError, PinnedState},
8    tree::{self, InstallTree, Tree},
9};
10use fs_extra::dir::CopyOptions;
11use itertools::Itertools;
12use miette::Diagnostic;
13use thiserror::Error;
14
15// TODO(vhyrro): Differentiate pinned LockedPackages at the type level?
16
17#[derive(Error, Debug, Diagnostic)]
18pub enum Error {
19    #[error("package with ID '{0}' not found in the lockfile")]
20    #[diagnostic(help("this is probably a bug"))]
21    PackageNotFound(locked_package::ID),
22    #[error("rock {rock} is already {}pinned!", if *.pin_state == PinnedState::Unpinned { "un" } else { "" })]
23    PinStateUnchanged {
24        pin_state: PinnedState,
25        rock: package::Spec,
26    },
27    #[error("cannot change the pin state of '{rock}', since a second version of '{rock}' is already installed with 'pin: {}'", .pin_state.as_bool())]
28    PinStateConflict {
29        pin_state: PinnedState,
30        rock: package::Spec,
31    },
32    #[error(transparent)]
33    #[diagnostic(transparent)]
34    FlushLockfile(#[from] FlushLockfileError),
35    #[error(transparent)]
36    #[diagnostic(transparent)]
37    Tree(#[from] tree::Error),
38    #[error("failed to move the old package")]
39    #[diagnostic(help("make sure Lux has write access to the install directory"))]
40    MoveItemsFailure(#[from] fs_extra::error::Error),
41    #[error("cannot change pin state of {rock}, because it is not an entrypoint")]
42    #[diagnostic(help(
43        "Lux does not allow pinning dependencies, as doing so could break the version requirement in a future update."
44    ))]
45    NotAnEntrypoint { rock: package::Spec },
46    #[error(transparent)]
47    #[diagnostic(transparent)]
48    Fs(#[from] fs::Error),
49}
50
51pub fn set_state(
52    package_id: &locked_package::ID,
53    tree: &Tree,
54    pin: PinnedState,
55) -> Result<(), Error> {
56    let lockfile = tree.lockfile()?;
57    let package = lockfile
58        .get(package_id)
59        .ok_or_else(|| Error::PackageNotFound(package_id.clone()))?
60        .clone();
61
62    if !lockfile.is_entrypoint(&package.id()) {
63        return Err(Error::NotAnEntrypoint {
64            rock: package.to_package(),
65        });
66    }
67
68    if pin == package.pinned() {
69        return Err(Error::PinStateUnchanged {
70            pin_state: package.pinned(),
71            rock: package.to_package(),
72        });
73    }
74
75    let old_package = package.clone();
76    let layout = tree.layout_for(&package.spec);
77    let items = fs::sync::read_dir(&layout.root)?
78        .filter_map(Result::ok)
79        .map(|dir| dir.path())
80        .collect_vec();
81
82    let package = package.repin(pin);
83
84    if lockfile.get(&package.id()).is_some() {
85        return Err(Error::PinStateConflict {
86            pin_state: package.pinned(),
87            rock: package.to_package(),
88        });
89    }
90
91    fs::sync::create_dir_all(&layout.root)?;
92
93    fs_extra::move_items(&items, layout.root, &CopyOptions::new())?;
94
95    lockfile.map_then_flush(|lockfile| {
96        let binaries = lockfile.binaries(&old_package.id()).cloned();
97        lockfile.remove(&old_package);
98        lockfile.add_entrypoint(&package);
99        if let Some(binaries) = binaries {
100            lockfile.set_binaries(&package, binaries);
101        }
102
103        Ok::<_, io::Error>(())
104    })?;
105
106    Ok(())
107}