Skip to main content

lux_lib/operations/install/
mod.rs

1use std::{collections::HashMap, io, sync::Arc};
2
3use crate::{
4    build::{Build, BuildBehaviour, BuildError, RemotePackageSourceSpec, SrcRockSource},
5    config::Config,
6    lockfile::{
7        FlushLockfileError, LocalPackage, LocalPackageId, LockConstraint, Lockfile, OptState,
8        PinnedState, ReadWrite,
9    },
10    lua_installation::{LuaInstallation, LuaInstallationError},
11    lua_rockspec::BuildBackendSpec,
12    lua_version::LuaVersionUnset,
13    luarocks::{
14        install_binary_rock::{BinaryRockInstall, InstallBinaryRockError},
15        luarocks_installation::{LuaRocksError, LuaRocksInstallError, LuaRocksInstallation},
16    },
17    operations::resolve::{Resolve, ResolveDependenciesError},
18    package::{PackageName, PackageNameList},
19    progress::{MultiProgress, Progress, ProgressBar},
20    remote_package_db::{RemotePackageDB, RemotePackageDBError, RemotePackageDbIntegrityError},
21    rockspec::Rockspec,
22    tree::{self, InstallTree, Tree, TreeError},
23    workspace::{Workspace, WorkspaceTreeError},
24};
25
26pub use crate::operations::install::spec::PackageInstallSpec;
27
28use bon::Builder;
29use bytes::Bytes;
30use futures::StreamExt;
31use itertools::Itertools;
32use thiserror::Error;
33
34use super::{DownloadedRockspec, RemoteRockDownload};
35
36pub mod spec;
37
38/// A rocks package installer, providing fine-grained control
39/// over how packages should be installed.
40/// Can install multiple packages in parallel.
41#[derive(Builder)]
42#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
43pub struct Install<'a, T>
44where
45    T: InstallTree + Clone + Send + Sync,
46{
47    #[builder(start_fn)]
48    config: &'a Config,
49    #[builder(field)]
50    packages: Vec<PackageInstallSpec>,
51    #[builder(setters(name = "_tree", vis = ""))]
52    tree: T,
53    package_db: Option<RemotePackageDB>,
54    progress: Option<Arc<Progress<MultiProgress>>>,
55}
56
57impl<'a, State> InstallBuilder<'a, Tree, State>
58where
59    State: install_builder::State,
60{
61    pub fn workspace(
62        self,
63        workspace: &'a Workspace,
64    ) -> Result<InstallBuilder<'a, Tree, install_builder::SetTree<State>>, WorkspaceTreeError>
65    where
66        State::Tree: install_builder::IsUnset,
67    {
68        let config = self.config;
69        Ok(self._tree(workspace.tree(config)?))
70    }
71}
72
73impl<'a, T, State> InstallBuilder<'a, T, State>
74where
75    State: install_builder::State,
76    T: InstallTree + Clone + Send + Sync,
77{
78    pub fn tree(self, tree: T) -> InstallBuilder<'a, T, install_builder::SetTree<State>>
79    where
80        State::Tree: install_builder::IsUnset,
81    {
82        self._tree(tree)
83    }
84
85    pub fn packages(self, packages: Vec<PackageInstallSpec>) -> Self {
86        Self { packages, ..self }
87    }
88
89    pub fn package(self, package: PackageInstallSpec) -> Self {
90        Self {
91            packages: self
92                .packages
93                .into_iter()
94                .chain(std::iter::once(package))
95                .collect(),
96            ..self
97        }
98    }
99}
100
101impl<State, T> InstallBuilder<'_, T, State>
102where
103    State: install_builder::State + install_builder::IsComplete,
104    T: InstallTree + Clone + Send + Sync + 'static,
105{
106    /// Install the packages.
107    pub async fn install(self) -> Result<Vec<LocalPackage>, InstallError> {
108        let install_built = self._build();
109        if install_built.packages.is_empty() {
110            return Ok(Vec::default());
111        }
112        let progress = match install_built.progress {
113            Some(p) => p,
114            None => MultiProgress::new_arc(install_built.config),
115        };
116        let package_db = match install_built.package_db {
117            Some(db) => db,
118            None => {
119                let bar = progress.map(|p| p.new_bar());
120                RemotePackageDB::from_config(install_built.config, &bar).await?
121            }
122        };
123
124        let duplicate_entrypoints = install_built
125            .packages
126            .iter()
127            .filter(|pkg| pkg.entry_type == tree::EntryType::Entrypoint)
128            .map(|pkg| pkg.package.name())
129            .duplicates()
130            .cloned()
131            .collect_vec();
132
133        if !duplicate_entrypoints.is_empty() {
134            return Err(InstallError::DuplicateEntrypoints(PackageNameList::new(
135                duplicate_entrypoints,
136            )));
137        }
138
139        install_impl(
140            install_built.packages,
141            Arc::new(package_db),
142            install_built.config,
143            &install_built.tree,
144            progress,
145        )
146        .await
147    }
148}
149
150#[derive(Error, Debug)]
151pub enum InstallError {
152    #[error("unable to resolve dependencies:\n{0}")]
153    ResolveDependencies(#[from] ResolveDependenciesError),
154    #[error(transparent)]
155    LuaVersionUnset(#[from] LuaVersionUnset),
156    #[error(transparent)]
157    LuaInstallation(#[from] LuaInstallationError),
158    #[error(transparent)]
159    FlushLockfile(#[from] FlushLockfileError),
160    #[error(transparent)]
161    Tree(#[from] TreeError),
162    #[error(transparent)]
163    WorkspaceTree(#[from] WorkspaceTreeError),
164    #[error("error instantiating LuaRocks compatibility layer:\n{0}")]
165    LuaRocks(#[from] LuaRocksError),
166    #[error("error installing LuaRocks compatibility layer:\n{0}")]
167    LuaRocksInstall(#[from] LuaRocksInstallError),
168    #[error("failed to build {0}: {1}")]
169    Build(PackageName, BuildError),
170    #[error("failed to install build depencency {0}:\n{1}")]
171    BuildDependency(PackageName, BuildError),
172    #[error("error initialising remote package DB:\n{0}")]
173    RemotePackageDB(#[from] RemotePackageDBError),
174    #[error("failed to install pre-built rock {0}:\n{1}")]
175    InstallBinaryRock(PackageName, InstallBinaryRockError),
176    #[error("integrity error for package {0}:\n{1}")]
177    Integrity(PackageName, RemotePackageDbIntegrityError),
178    #[error("cannot install duplicate entrypoints:\n{0}")]
179    DuplicateEntrypoints(PackageNameList),
180}
181
182// TODO(vhyrro): This function has too many arguments. Refactor it.
183#[allow(clippy::too_many_arguments)]
184async fn install_impl<T>(
185    packages: Vec<PackageInstallSpec>,
186    package_db: Arc<RemotePackageDB>,
187    config: &Config,
188    tree: &T,
189    progress_arc: Arc<Progress<MultiProgress>>,
190) -> Result<Vec<LocalPackage>, InstallError>
191where
192    T: InstallTree + Clone + Send + Sync + 'static,
193{
194    let (dep_tx, mut dep_rx) = tokio::sync::mpsc::unbounded_channel();
195    let (build_dep_tx, mut build_dep_rx) = tokio::sync::mpsc::unbounded_channel();
196
197    let lockfile = tree.lockfile()?;
198    let build_lockfile = tree.build_tree(config)?.lockfile()?;
199
200    Resolve::new()
201        .dependencies_tx(dep_tx)
202        .build_dependencies_tx(build_dep_tx)
203        .packages(packages)
204        .package_db(package_db.clone())
205        .lockfile(Arc::new(lockfile.clone()))
206        .build_lockfile(Arc::new(build_lockfile.clone()))
207        .config(config)
208        .progress(progress_arc.clone())
209        .get_all_dependencies()
210        .await?;
211
212    let lua = Arc::new(
213        LuaInstallation::new_from_config(config, &progress_arc.map(|progress| progress.new_bar()))
214            .await?,
215    );
216
217    // We have to install transitive build dependencies sequentially
218    while let Some(build_dep_spec) = build_dep_rx.recv().await {
219        let rockspec = build_dep_spec.downloaded_rock.rockspec();
220        let bar = progress_arc.map(|p| {
221            p.add(ProgressBar::from(format!(
222                "💻 Installing build dependency: {}",
223                build_dep_spec.downloaded_rock.rockspec().package(),
224            )))
225        });
226        let package = rockspec.package().clone();
227        let build_tree = tree.build_tree(config)?;
228        // We have to write to the build tree's lockfile after each build,
229        // so that each transitive build dependency is available for the
230        // next build dependencies that may depend on it.
231        let mut build_lockfile = build_tree.lockfile()?.write_guard();
232        let pkg = Build::new()
233            .rockspec(rockspec)
234            .lua(&lua)
235            .tree(&build_tree)
236            .entry_type(tree::EntryType::Entrypoint)
237            .config(config)
238            .progress(&bar)
239            .constraint(build_dep_spec.spec.constraint())
240            .behaviour(build_dep_spec.build_behaviour)
241            .build()
242            .await
243            .map_err(|err| InstallError::BuildDependency(package, err))?;
244        build_lockfile.add_entrypoint(&pkg);
245    }
246
247    let mut all_packages = HashMap::with_capacity(dep_rx.len());
248    while let Some(dep) = dep_rx.recv().await {
249        all_packages.insert(dep.spec.id(), dep);
250    }
251
252    let installed_packages =
253        futures::stream::iter(all_packages.clone().into_values().map(|install_spec| {
254            let progress_arc = progress_arc.clone();
255            let downloaded_rock = install_spec.downloaded_rock;
256            let config = config.clone();
257            let tree = tree.clone();
258            let lua = lua.clone();
259
260            tokio::spawn({
261                async move {
262                    let pkg = match downloaded_rock {
263                        RemoteRockDownload::RockspecOnly { rockspec_download } => {
264                            install_rockspec(
265                                rockspec_download,
266                                None,
267                                install_spec.spec.constraint(),
268                                install_spec.build_behaviour,
269                                install_spec.pin,
270                                install_spec.opt,
271                                install_spec.entry_type,
272                                &lua,
273                                &tree,
274                                &config,
275                                progress_arc,
276                            )
277                            .await?
278                        }
279                        RemoteRockDownload::BinaryRock {
280                            rockspec_download,
281                            packed_rock,
282                        } => {
283                            install_binary_rock(
284                                rockspec_download,
285                                packed_rock,
286                                install_spec.spec.constraint(),
287                                install_spec.build_behaviour,
288                                install_spec.pin,
289                                install_spec.opt,
290                                install_spec.entry_type,
291                                &config,
292                                &tree,
293                                progress_arc,
294                            )
295                            .await?
296                        }
297                        RemoteRockDownload::SrcRock {
298                            rockspec_download,
299                            src_rock,
300                            source_url,
301                        } => {
302                            let src_rock_source = SrcRockSource {
303                                bytes: src_rock,
304                                source_url,
305                            };
306                            install_rockspec(
307                                rockspec_download,
308                                Some(src_rock_source),
309                                install_spec.spec.constraint(),
310                                install_spec.build_behaviour,
311                                install_spec.pin,
312                                install_spec.opt,
313                                install_spec.entry_type,
314                                &lua,
315                                &tree,
316                                &config,
317                                progress_arc,
318                            )
319                            .await?
320                        }
321                    };
322
323                    Ok::<_, InstallError>((pkg.id(), (pkg, install_spec.entry_type)))
324                }
325            })
326        }))
327        .buffered(config.max_jobs())
328        .collect::<Vec<_>>()
329        .await
330        .into_iter()
331        .flatten()
332        .try_collect::<_, HashMap<LocalPackageId, (LocalPackage, tree::EntryType)>, _>()?;
333
334    let write_dependency = |lockfile: &mut Lockfile<ReadWrite>,
335                            id: &LocalPackageId,
336                            pkg: &LocalPackage,
337                            entry_type: tree::EntryType|
338     -> io::Result<()> {
339        if entry_type == tree::EntryType::Entrypoint {
340            lockfile.add_entrypoint(pkg);
341        }
342
343        for dependency_id in all_packages
344            .get(id)
345            .map(|pkg| pkg.spec.dependencies())
346            .unwrap_or_default()
347            .into_iter()
348        {
349            lockfile.add_dependency(
350                pkg,
351                installed_packages
352                    .get(dependency_id)
353                    .map(|(pkg, _)| pkg)
354                    .ok_or(io::Error::other(
355                        r#"
356error writing dependencies to the lockfile.
357A required dependency was not installed correctly.
358This is likely because an install thread panicked and was interrupted unexpectedly.
359
360[THIS IS A BUG!]
361"#,
362                    ))?,
363            );
364        }
365        Ok(())
366    };
367
368    lockfile.map_then_flush(|lockfile| {
369        for (id, (pkg, is_entrypoint)) in installed_packages.iter() {
370            write_dependency(lockfile, id, pkg, *is_entrypoint)?;
371        }
372        Ok::<_, io::Error>(())
373    })?;
374
375    Ok(installed_packages
376        .into_values()
377        .map(|(pkg, _)| pkg)
378        .collect_vec())
379}
380
381#[allow(clippy::too_many_arguments)]
382async fn install_rockspec<T>(
383    rockspec_download: DownloadedRockspec,
384    src_rock_source: Option<SrcRockSource>,
385    constraint: LockConstraint,
386    behaviour: BuildBehaviour,
387    pin: PinnedState,
388    opt: OptState,
389    entry_type: tree::EntryType,
390    lua: &LuaInstallation,
391    tree: &T,
392    config: &Config,
393    progress_arc: Arc<Progress<MultiProgress>>,
394) -> Result<LocalPackage, InstallError>
395where
396    T: InstallTree + Sync,
397{
398    let progress = Arc::clone(&progress_arc);
399    let rockspec = rockspec_download.rockspec;
400    let source = rockspec_download.source;
401    let package = rockspec.package().clone();
402    let bar = progress.map(|p| p.add(ProgressBar::from(format!("💻 Installing {}", &package,))));
403
404    if let Some(BuildBackendSpec::LuaRock(_)) = &rockspec.build().current_platform().build_backend {
405        let luarocks_tree = tree.build_tree(config)?;
406        let luarocks = LuaRocksInstallation::new(config, luarocks_tree)?;
407        luarocks.ensure_installed(lua, &bar).await?;
408    }
409
410    let source_spec = match src_rock_source {
411        Some(src_rock_source) => RemotePackageSourceSpec::SrcRock(src_rock_source),
412        None => RemotePackageSourceSpec::RockSpec(rockspec_download.source_url),
413    };
414
415    let pkg = Build::new()
416        .rockspec(&rockspec)
417        .lua(lua)
418        .tree(tree)
419        .entry_type(entry_type)
420        .config(config)
421        .progress(&bar)
422        .pin(pin)
423        .opt(opt)
424        .constraint(constraint)
425        .behaviour(behaviour)
426        .source(source)
427        .source_spec(source_spec)
428        .build()
429        .await
430        .map_err(|err| InstallError::Build(package, err))?;
431
432    bar.map(|b| b.finish_and_clear());
433
434    Ok(pkg)
435}
436
437#[allow(clippy::too_many_arguments)]
438async fn install_binary_rock(
439    rockspec_download: DownloadedRockspec,
440    packed_rock: Bytes,
441    constraint: LockConstraint,
442    behaviour: BuildBehaviour,
443    pin: PinnedState,
444    opt: OptState,
445    entry_type: tree::EntryType,
446    config: &Config,
447    tree: &impl InstallTree,
448    progress_arc: Arc<Progress<MultiProgress>>,
449) -> Result<LocalPackage, InstallError> {
450    let progress = Arc::clone(&progress_arc);
451    let rockspec = rockspec_download.rockspec;
452    let package = rockspec.package().clone();
453    let bar = progress.map(|p| {
454        p.add(ProgressBar::from(format!(
455            "💻 Installing {} (pre-built)",
456            &package,
457        )))
458    });
459    let pkg = BinaryRockInstall::new(
460        &rockspec,
461        rockspec_download.source,
462        packed_rock,
463        entry_type,
464        config,
465        tree,
466        &bar,
467    )
468    .pin(pin)
469    .opt(opt)
470    .constraint(constraint)
471    .behaviour(behaviour)
472    .install()
473    .await
474    .map_err(|err| InstallError::InstallBinaryRock(package, err))?;
475
476    bar.map(|b| b.finish_and_clear());
477
478    Ok(pkg)
479}