Skip to main content

pray_core/
resolve.rs

1use crate::manifest::{Manifest, ManifestPackage, ManifestSource};
2use crate::package_spec::PackageSpec;
3use crate::resolve_context::ResolveOptions;
4use crate::resolve_git_sources::{prepare_git_sources, prepare_pray_ssh_host_keys};
5
6pub use crate::resolve_git::{
7    discover_distribution_root, git_source_cache_directory, global_git_cache_ready,
8};
9pub use crate::resolve_git_legacy::quarantine_legacy_git_checkouts;
10pub use crate::resolve_git_lookup::git_source_cached_repository;
11pub use crate::resolve_git_paths::git_source_cache_directory_with_subdir;
12pub use crate::resolve_git_refresh::{
13    annotate_failed_git_refresh, annotate_missing_git_catalog,
14    resolution_may_benefit_from_git_source_refresh,
15};
16pub use crate::resolve_git_sources::refresh_git_sources;
17use crate::{PrayError, PrayResult};
18use std::collections::BTreeMap;
19use std::path::{Path, PathBuf};
20
21#[derive(Debug, Clone)]
22pub struct ResolvedProject {
23    pub manifest_path: PathBuf,
24    pub project_root: PathBuf,
25    pub manifest: Manifest,
26    pub manifest_hash: String,
27    pub packages: Vec<ResolvedPackage>,
28    pub local_files: Vec<ResolvedLocalFile>,
29    pub source_revisions: BTreeMap<String, String>,
30    pub source_host_keys: BTreeMap<String, String>,
31    pub environment: Option<String>,
32}
33
34#[derive(Debug, Clone)]
35pub struct ResolvedPackage {
36    pub declaration: ManifestPackage,
37    pub root: PathBuf,
38    pub spec: PackageSpec,
39    pub tree_hash: String,
40    pub artifact_hash: String,
41    pub artifact: String,
42    pub selected_exports: Vec<String>,
43    pub source_checksum: String,
44    pub export_bodies: BTreeMap<String, String>,
45    pub skill_files: BTreeMap<String, Vec<String>>,
46    pub signer_fingerprint: Option<String>,
47    /// Highest non-yanked version in registry metadata when the package came from a registry source.
48    pub registry_latest_version: Option<String>,
49    /// True when the package was declared in Prayfile; false for transitive dependencies.
50    pub explicit: bool,
51    pub upstream: Option<crate::package_upstream::LockedUpstream>,
52}
53
54#[derive(Debug, Clone)]
55pub struct ResolvedLocalFile {
56    pub path: PathBuf,
57    pub manifest_path: String,
58    pub content: String,
59    pub source_checksum: String,
60    pub position: String,
61    pub optional: bool,
62}
63
64impl ResolvedProject {
65    pub fn lockfile_hash(&self) -> PrayResult<String> {
66        Ok(self.manifest_hash.clone())
67    }
68}
69
70pub fn project_root_from_manifest(manifest_path: &Path) -> PathBuf {
71    match manifest_path.parent() {
72        Some(parent) if !parent.as_os_str().is_empty() => parent.to_path_buf(),
73        _ => PathBuf::from("."),
74    }
75}
76
77fn canonical_project_root(manifest_path: &Path) -> PrayResult<PathBuf> {
78    let root = project_root_from_manifest(manifest_path);
79    if root.is_absolute() {
80        return Ok(root);
81    }
82    let cwd = std::env::current_dir().map_err(|error| {
83        PrayError::Resolution(format!("failed to resolve project root from cwd: {error}"))
84    })?;
85    Ok(cwd.join(root))
86}
87
88pub fn resolve_project(manifest_path: &Path) -> PrayResult<ResolvedProject> {
89    resolve_project_with_options(manifest_path, &ResolveOptions::default())
90}
91
92pub fn resolve_project_with_git_refresh_fallback(
93    manifest_path: &Path,
94    options: &ResolveOptions,
95    allow_git_refresh_fallback: bool,
96) -> PrayResult<ResolvedProject> {
97    match resolve_project_with_options(manifest_path, options) {
98        Ok(project) => Ok(project),
99        Err(PrayError::Resolution(message))
100            if allow_git_refresh_fallback
101                && !options.offline
102                && !options.refresh_source_revisions
103                && resolution_may_benefit_from_git_source_refresh(&message) =>
104        {
105            let refreshed_options = ResolveOptions {
106                refresh_source_revisions: true,
107                ..options.clone()
108            };
109            match resolve_project_with_options(manifest_path, &refreshed_options) {
110                Ok(project) => Ok(project),
111                Err(error) => {
112                    let lockfile_path =
113                        project_root_from_manifest(manifest_path).join("Prayfile.lock");
114                    Err(annotate_failed_git_refresh(&lockfile_path, error))
115                }
116            }
117        }
118        Err(error) => Err(error),
119    }
120}
121
122pub fn resolve_project_with_options(
123    manifest_path: &Path,
124    options: &ResolveOptions,
125) -> PrayResult<ResolvedProject> {
126    let project_root = canonical_project_root(manifest_path)?;
127    resolve_project_in_context(manifest_path, &project_root, options)
128}
129
130#[path = "resolve_project.rs"]
131mod project;
132pub use project::{resolve_manifest_in_context, resolve_project_in_context};
133
134#[path = "resolve_implied_source.rs"]
135mod implied_source;
136pub(crate) use implied_source::implied_source_name;
137
138#[path = "resolve_package_root.rs"]
139mod package_root;
140pub(crate) use package_root::{resolve_package_root, PackageRootResolution};
141
142#[path = "resolve_upstream.rs"]
143pub mod resolve_upstream;
144pub use crate::resolve_git_materialize_confirm::confirm_git_source_revisions_materializable;
145pub use resolve_upstream::{
146    apply_path_upstream_refreshes, path_fork_drift_lines, plan_path_upstream_conflicts,
147    PathUpstreamConflict,
148};
149
150#[path = "resolve_one.rs"]
151mod one;
152
153#[path = "resolve_local.rs"]
154mod local;
155pub use local::missing_local_embed_guidance;
156pub(crate) use local::resolve_local_file;
157
158fn source_map(sources: &[ManifestSource]) -> BTreeMap<String, ManifestSource> {
159    sources
160        .iter()
161        .map(|source| (source.name.clone(), source.clone()))
162        .collect()
163}
164
165#[cfg(test)]
166#[path = "resolve_unit.rs"]
167mod tests;