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
//! Resource fetching service for dependency resolution.
//!
//! This service handles fetching resource content from local files or Git worktrees
//! and resolving canonical paths for dependencies.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use crate::manifest::ResourceDependency;
use super::types::ResolutionCore;
use super::version_resolver::VersionResolutionService;
/// Service for fetching resource content and resolving paths.
pub struct ResourceFetchingService;
impl ResourceFetchingService {
/// Create a new resource fetching service.
pub fn new() -> Self {
Self
}
/// Fetch the content of a resource for metadata extraction.
///
/// This method retrieves the file content from either:
/// - Local filesystem (for path-only dependencies)
/// - Git worktree (for Git-backed dependencies with version)
///
/// This method can prepare versions on-demand if they haven't been prepared yet,
/// which is necessary for transitive dependencies discovered during resolution.
///
/// # Arguments
///
/// * `core` - The resolution core with manifest and cache
/// * `dep` - The resource dependency to fetch
/// * `version_service` - Version service to get/prepare worktree paths
///
/// # Returns
///
/// The file content as a string
pub async fn fetch_content(
core: &ResolutionCore,
dep: &ResourceDependency,
version_service: &mut VersionResolutionService,
) -> Result<String> {
match dep {
ResourceDependency::Simple(path) => {
// Local file - resolve relative to manifest directory
let manifest_dir = core
.manifest
.manifest_dir
.as_ref()
.context("Manifest directory not available for local dependency")?;
let full_path = manifest_dir.join(path);
let canonical_path = full_path
.canonicalize()
.with_context(|| format!("Failed to resolve local path: {}", path))?;
Self::read_with_cache_retry(&canonical_path).await
}
ResourceDependency::Detailed(detailed) => {
if let Some(source) = &detailed.source {
// Git-backed dependency
// Use dep.get_version() to handle branch/rev/version precedence
let version_key = dep.get_version().unwrap_or("HEAD");
let group_key = format!("{}::{}", source, version_key);
// Check if version is already prepared, if not prepare it on-demand
if version_service.get_prepared_version(&group_key).is_none() {
// Prepare this version on-demand (common with transitive dependencies)
// Use dep.get_version() to properly handle branch/rev/version precedence
version_service
.prepare_additional_version(core, source, dep.get_version())
.await
.with_context(|| {
format!(
"Failed to prepare version on-demand for source '{}' @ '{}'",
source, version_key
)
})?;
}
let prepared = version_service.get_prepared_version(&group_key).unwrap();
let worktree_path = &prepared.worktree_path;
let file_path = worktree_path.join(&detailed.path);
// Don't canonicalize Git-backed files - worktrees may have coherency delays
Self::read_with_cache_retry(&file_path).await
} else {
// Local path-only dependency
let manifest_dir = core
.manifest
.manifest_dir
.as_ref()
.context("Manifest directory not available")?;
let full_path = manifest_dir.join(&detailed.path);
let canonical_path = full_path.canonicalize().with_context(|| {
format!("Failed to resolve local path: {}", detailed.path)
})?;
Self::read_with_cache_retry(&canonical_path).await
}
}
}
}
/// Get the canonical path for a dependency.
///
/// Resolves dependency path to its canonical form on the filesystem.
/// Can prepare versions on-demand if needed.
///
/// # Arguments
///
/// * `core` - The resolution core with manifest and cache
/// * `dep` - The resource dependency
/// * `version_service` - Version service to get/prepare worktree paths
///
/// # Returns
///
/// The canonical absolute path to the resource
pub async fn get_canonical_path(
core: &ResolutionCore,
dep: &ResourceDependency,
version_service: &mut VersionResolutionService,
) -> Result<PathBuf> {
match dep {
ResourceDependency::Simple(path) => {
let manifest_dir = core
.manifest
.manifest_dir
.as_ref()
.context("Manifest directory not available")?;
let full_path = manifest_dir.join(path);
full_path
.canonicalize()
.with_context(|| format!("Failed to canonicalize path: {}", path))
}
ResourceDependency::Detailed(detailed) => {
if let Some(source) = &detailed.source {
// Git-backed dependency
// Use dep.get_version() to handle branch/rev/version precedence
let version_key = dep.get_version().unwrap_or("HEAD");
let group_key = format!("{}::{}", source, version_key);
// Check if version is already prepared, if not prepare it on-demand
if version_service.get_prepared_version(&group_key).is_none() {
version_service
.prepare_additional_version(core, source, detailed.version.as_deref())
.await
.with_context(|| {
format!(
"Failed to prepare version on-demand for source '{}' @ '{}'",
source, version_key
)
})?;
}
let prepared = version_service.get_prepared_version(&group_key).unwrap();
let worktree_path = &prepared.worktree_path;
let file_path = worktree_path.join(&detailed.path);
// Return the path without canonicalizing - Git worktrees may have coherency delays
Ok(file_path)
} else {
// Local path-only dependency
let manifest_dir = core
.manifest
.manifest_dir
.as_ref()
.context("Manifest directory not available")?;
let full_path = manifest_dir.join(&detailed.path);
full_path
.canonicalize()
.with_context(|| format!("Failed to canonicalize path: {}", detailed.path))
}
}
}
}
/// Read file with retry logic for cache coherency issues.
///
/// Git worktrees can have filesystem coherency delays after creation.
/// This method retries up to 10 times with 100ms delays between attempts.
async fn read_with_cache_retry(path: &Path) -> Result<String> {
use tokio::time::{Duration, sleep};
const MAX_ATTEMPTS: u32 = 10;
const RETRY_DELAY_MS: u64 = 100;
for attempt in 0..MAX_ATTEMPTS {
match tokio::fs::read_to_string(path).await {
Ok(content) => return Ok(content),
Err(e)
if e.kind() == std::io::ErrorKind::NotFound && attempt < MAX_ATTEMPTS - 1 =>
{
// File not found, but we have retries left
tracing::debug!(
"File not found at {}, retrying ({}/{})",
path.display(),
attempt + 1,
MAX_ATTEMPTS
);
sleep(Duration::from_millis(RETRY_DELAY_MS)).await;
continue;
}
Err(e) => {
// Other error or final attempt
return Err(e)
.with_context(|| format!("Failed to read file: {}", path.display()));
}
}
}
// This should never be reached, but provide a fallback
anyhow::bail!("Failed to read file after {} attempts: {}", MAX_ATTEMPTS, path.display())
}
}
impl Default for ResourceFetchingService {
fn default() -> Self {
Self::new()
}
}