1use crate::{MAX_PROGRAM_SIZE, *};
18
19use leo_errors::Result;
20use leo_span::Symbol;
21
22use snarkvm::prelude::{Program as SvmProgram, TestnetV0};
23
24use indexmap::{IndexMap, IndexSet};
25use std::path::Path;
26
27fn find_cached_edition(cache_directory: &Path, name: &str) -> Option<u16> {
30 let program_cache = cache_directory.join(name);
31 if !program_cache.exists() {
32 return None;
33 }
34
35 std::fs::read_dir(&program_cache)
37 .ok()?
38 .filter_map(|entry| entry.ok())
39 .filter_map(|entry| {
40 let file_name = entry.file_name();
41 let name = file_name.to_str()?;
42 name.parse::<u16>().ok()
43 })
44 .max()
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
49pub enum PackageKind {
50 Program,
52 Library,
54 Test,
56}
57
58impl PackageKind {
59 pub fn is_program(&self) -> bool {
60 matches!(self, Self::Program)
61 }
62
63 pub fn is_library(&self) -> bool {
64 matches!(self, Self::Library)
65 }
66
67 pub fn is_test(&self) -> bool {
68 matches!(self, Self::Test)
69 }
70}
71
72#[derive(Clone, Debug)]
74pub struct CompilationUnit {
75 pub name: Symbol,
79 pub data: ProgramData,
80 pub edition: Option<u16>,
81 pub dependencies: IndexSet<Dependency>,
82 pub is_local: bool,
83 pub kind: PackageKind,
84}
85
86impl CompilationUnit {
87 pub fn from_aleo_path<P: AsRef<Path>>(name: Symbol, path: P, map: &IndexMap<Symbol, Dependency>) -> Result<Self> {
90 Self::from_aleo_path_impl(name, path.as_ref(), map)
91 }
92
93 fn from_aleo_path_impl(name: Symbol, path: &Path, map: &IndexMap<Symbol, Dependency>) -> Result<Self> {
94 let bytecode = std::fs::read_to_string(path).map_err(|e| {
95 crate::errors::util_file_io_error(format_args!("Trying to read aleo file at {}", path.display()), e)
96 })?;
97
98 let dependencies = parse_dependencies_from_aleo(name, &bytecode, map)?;
99
100 Ok(CompilationUnit {
101 name,
102 data: ProgramData::Bytecode(bytecode),
103 edition: None,
104 dependencies,
105 is_local: true,
106 kind: PackageKind::Program,
107 })
108 }
109
110 pub fn from_package_path<P: AsRef<Path>>(name: Symbol, path: P) -> Result<Self> {
113 Self::from_package_path_impl(name, path.as_ref())
114 }
115
116 fn from_package_path_impl(name: Symbol, path: &Path) -> Result<Self> {
117 let manifest = Manifest::read_from_file(path.join(MANIFEST_FILENAME))?;
118 let manifest_symbol = crate::symbol(&manifest.program)?;
119 if name != manifest_symbol {
120 return Err(
121 crate::errors::conflicting_manifest(format_args!("{name}"), format_args!("{manifest_symbol}")).into()
122 );
123 }
124 let source_directory = path.join(SOURCE_DIRECTORY);
125 source_directory.read_dir().map_err(|e| {
126 crate::errors::util_file_io_error(
127 format_args!("Failed to read directory {}", source_directory.display()),
128 e,
129 )
130 })?;
131
132 let main_path = source_directory.join(MAIN_FILENAME);
133 let lib_path = source_directory.join(LIB_FILENAME);
134
135 let (source_path, kind) = match (main_path.exists(), lib_path.exists()) {
136 (true, true) => {
137 return Err(crate::errors::ambiguous_entry_file(
138 source_directory.display(),
139 MAIN_FILENAME,
140 LIB_FILENAME,
141 )
142 .into());
143 }
144 (true, false) => (main_path, PackageKind::Program),
145 (false, true) => (lib_path, PackageKind::Library),
146 (false, false) => {
147 return Err(
148 crate::errors::invalid_entry_file(source_directory.display(), MAIN_FILENAME, LIB_FILENAME).into()
149 );
150 }
151 };
152
153 Ok(CompilationUnit {
154 name,
155 data: ProgramData::SourcePath { directory: path.to_path_buf(), source: source_path },
156 edition: None,
157 dependencies: manifest
158 .dependencies
159 .unwrap_or_default()
160 .into_iter()
161 .map(|dependency| {
162 let dep = canonicalize_dependency_path_relative_to(path, dependency)?;
163 if dep.location == Location::Workspace { resolve_workspace_dependency(path, dep) } else { Ok(dep) }
164 })
165 .collect::<Result<IndexSet<_>, _>>()?,
166 is_local: true,
167 kind,
168 })
169 }
170
171 pub fn from_test_path<P: AsRef<Path>>(source_path: P, main_program: Dependency) -> Result<Self> {
178 Self::from_path_test_impl(source_path.as_ref(), main_program)
179 }
180
181 fn from_path_test_impl(source_path: &Path, main_program: Dependency) -> Result<Self> {
182 let name = filename_no_leo_extension(source_path)
183 .ok_or_else(|| crate::errors::failed_path(source_path.display(), ""))?;
184 let test_directory = source_path.parent().ok_or_else(|| {
185 crate::errors::failed_to_open_file(format_args!(
186 "Failed to find directory for test {}",
187 source_path.display()
188 ))
189 })?;
190 let package_directory = test_directory.parent().ok_or_else(|| {
191 crate::errors::failed_to_open_file(format_args!(
192 "Failed to find package for test {}",
193 source_path.display()
194 ))
195 })?;
196 let manifest = Manifest::read_from_file(package_directory.join(MANIFEST_FILENAME))?;
197 let mut dependencies = manifest
198 .dev_dependencies
199 .unwrap_or_default()
200 .into_iter()
201 .map(|dependency| {
202 let dep = canonicalize_dependency_path_relative_to(package_directory, dependency)?;
203 if dep.location == Location::Workspace {
204 resolve_workspace_dependency(package_directory, dep)
205 } else {
206 Ok(dep)
207 }
208 })
209 .collect::<Result<IndexSet<_>, _>>()?;
210 dependencies.insert(main_program);
211
212 Ok(CompilationUnit {
213 name: Symbol::intern(&(name.to_owned() + ".aleo")),
214 edition: None,
215 data: ProgramData::SourcePath {
216 directory: test_directory.to_path_buf(),
217 source: source_path.to_path_buf(),
218 },
219 dependencies,
220 is_local: true,
221 kind: PackageKind::Test,
222 })
223 }
224
225 pub fn fetch<P: AsRef<Path>>(
228 name: Symbol,
229 edition: Option<u16>,
230 home_path: P,
231 network: NetworkName,
232 endpoint: &str,
233 no_cache: bool,
234 network_retries: u32,
235 ) -> Result<Self> {
236 Self::fetch_impl(name, edition, home_path.as_ref(), network, endpoint, no_cache, network_retries)
237 }
238
239 fn fetch_impl(
240 name: Symbol,
241 edition: Option<u16>,
242 home_path: &Path,
243 network: NetworkName,
244 endpoint: &str,
245 no_cache: bool,
246 network_retries: u32,
247 ) -> Result<Self> {
248 let name = Symbol::intern(name.to_string().strip_suffix(".aleo").unwrap_or(&name.to_string()));
251
252 let cache_directory = home_path.join(format!("registry/{network}"));
254
255 let edition = match edition {
258 _ if name == Symbol::intern("credits") => 0,
260 Some(edition) => edition,
261 None if !no_cache => {
262 match find_cached_edition(&cache_directory, &name.to_string()) {
264 Some(cached_edition) => cached_edition,
265 None => crate::fetch_latest_edition(&name.to_string(), endpoint, network, network_retries)?,
266 }
267 }
268 None => crate::fetch_latest_edition(&name.to_string(), endpoint, network, network_retries)?,
270 };
271
272 let cache_directory = cache_directory.join(format!("{name}/{edition}"));
276 let full_cache_path = cache_directory.join(format!("{name}.aleo"));
277 if !cache_directory.exists() {
278 std::fs::create_dir_all(&cache_directory).map_err(|err| {
280 crate::errors::util_file_io_error(format!("Could not write path {}", cache_directory.display()), err)
281 })?;
282 }
283
284 let existing_bytecode = match full_cache_path.exists() {
286 false => None,
287 true => {
288 let existing_contents = std::fs::read_to_string(&full_cache_path).map_err(|e| {
289 crate::errors::util_file_io_error(
290 format_args!("Trying to read cached file at {}", full_cache_path.display()),
291 e,
292 )
293 })?;
294 Some(existing_contents)
295 }
296 };
297
298 let bytecode = match (existing_bytecode, no_cache) {
299 (Some(bytecode), false) => bytecode,
301 (existing, _) => {
303 let primary_url = if name == Symbol::intern("credits") {
305 format!("{endpoint}/{network}/program/credits.aleo")
306 } else {
307 format!("{endpoint}/{network}/program/{name}.aleo/{edition}")
308 };
309 let secondary_url = format!("{endpoint}/{network}/program/{name}.aleo");
310 let contents = fetch_from_network(&primary_url, network_retries)
311 .or_else(|_| fetch_from_network(&secondary_url, network_retries))
312 .map_err(|err| {
313 crate::errors::failed_to_retrieve_from_endpoint(
314 primary_url,
315 format_args!("Failed to fetch program `{name}` from network `{network}`: {err}"),
316 )
317 })?;
318
319 if let Some(existing_contents) = existing
321 && existing_contents != contents
322 {
323 println!(
324 "Warning: The cached file at `{}` is different from the one fetched from the network. The cached file will be overwritten.",
325 full_cache_path.display()
326 );
327 }
328
329 std::fs::write(&full_cache_path, &contents).map_err(|err| {
331 crate::errors::util_file_io_error(
332 format_args!("Could not open file `{}`", full_cache_path.display()),
333 err,
334 )
335 })?;
336
337 contents
338 }
339 };
340
341 let dependencies = parse_dependencies_from_aleo(name, &bytecode, &IndexMap::new())?;
342
343 Ok(CompilationUnit {
344 name: Symbol::intern(&(name.to_string() + ".aleo")),
347 data: ProgramData::Bytecode(bytecode),
348 edition: Some(edition),
349 dependencies,
350 is_local: false,
351 kind: PackageKind::Program,
352 })
353 }
354
355 pub fn from_git(
357 name: Symbol,
358 dependency: &Dependency,
359 home_path: &Path,
360 old_lock: &Lock,
361 new_lock: &mut Lock,
362 offline: bool,
363 declared_deps: &IndexMap<Symbol, Dependency>,
364 ) -> Result<Self> {
365 let git = dependency
366 .git
367 .as_ref()
368 .ok_or_else(|| crate::errors::invalid_manifest_dependency(&dependency.name, "missing `git` URL"))?;
369 let url = git.url.as_str();
370 let reference =
371 git.reference().map_err(|reason| crate::errors::invalid_manifest_dependency(&dependency.name, reason))?;
372 let reference_str = reference.lock_string();
373
374 let memoized = new_lock
377 .commit_for_source(url, &reference_str)
378 .map(|commit| (crate::git::checkout_dir(home_path, url, commit), commit.to_string()))
379 .filter(|(dir, _)| dir.is_dir());
380 let (checkout, commit) = match memoized {
381 Some(hit) => hit,
382 None => {
383 let locked = old_lock.commit_for(&dependency.name, url, &reference_str).map(str::to_string);
384 crate::git::resolve(home_path, &dependency.name, url, &reference, locked.as_deref(), offline)?
385 }
386 };
387 new_lock.record(dependency.name.clone(), url.to_string(), reference_str, commit);
388
389 let located = find_in_checkout(&checkout, &dependency.name)?;
390 let mut unit = if located.extension().and_then(|e| e.to_str()) == Some("aleo") && located.is_file() {
391 Self::from_aleo_path(name, located, declared_deps)?
392 } else {
393 Self::from_package_path(name, located)?
394 };
395
396 unit.dependencies = unit
399 .dependencies
400 .into_iter()
401 .map(|dep| {
402 if dep.location == Location::Local && dep.path.as_ref().is_some_and(|p| p.starts_with(&checkout)) {
403 Dependency {
404 name: dep.name,
405 location: Location::Git,
406 path: None,
407 edition: None,
408 git: Some(git.clone()),
409 }
410 } else {
411 dep
412 }
413 })
414 .collect();
415 Ok(unit)
416 }
417}
418
419pub fn find_in_checkout(checkout: &Path, dep_name: &str) -> Result<std::path::PathBuf> {
424 let bare = crate::bare_unit_name(dep_name);
426 let with_aleo = format!("{bare}.aleo");
427 let (requested, alternate) =
428 if dep_name.ends_with(".aleo") { (with_aleo.as_str(), bare) } else { (bare, with_aleo.as_str()) };
429
430 let mut exact = Vec::new();
431 let mut alt = Vec::new();
432 collect_matching_manifest_dirs(checkout, requested, alternate, 0, &mut exact, &mut alt);
433 let matches = if exact.is_empty() { alt } else { exact };
434 if matches.len() > 1 {
435 let listed = matches
436 .iter()
437 .map(|d| d.strip_prefix(checkout).unwrap_or(d).display().to_string())
438 .collect::<Vec<_>>()
439 .join("`, `");
440 return Err(crate::errors::git_ambiguous_package(dep_name, listed).into());
441 }
442 if let Some(dir) = matches.into_iter().next() {
443 return Ok(dir);
444 }
445 let aleo = checkout.join(&with_aleo);
446 if aleo.is_file() {
447 return Ok(aleo);
448 }
449 Err(crate::errors::git_package_not_found(dep_name, checkout.display()).into())
450}
451
452fn collect_matching_manifest_dirs(
455 dir: &Path,
456 requested: &str,
457 alternate: &str,
458 depth: usize,
459 exact: &mut Vec<std::path::PathBuf>,
460 alt: &mut Vec<std::path::PathBuf>,
461) {
462 const MAX_DEPTH: usize = 6;
464 if depth > MAX_DEPTH {
465 return;
466 }
467
468 #[derive(serde::Deserialize)]
470 struct ManifestProgramOnly {
471 program: String,
472 }
473
474 let manifest = dir.join(MANIFEST_FILENAME);
475 if manifest.is_file()
476 && !manifest.is_symlink()
477 && let Ok(contents) = std::fs::read_to_string(&manifest)
478 && let Ok(parsed) = serde_json::from_str::<ManifestProgramOnly>(&contents)
479 {
480 if parsed.program == requested {
481 exact.push(dir.to_path_buf());
482 } else if parsed.program == alternate {
483 alt.push(dir.to_path_buf());
484 }
485 }
486
487 let mut entries: Vec<_> = std::fs::read_dir(dir).ok().into_iter().flatten().flatten().map(|e| e.path()).collect();
489 entries.sort();
490 for path in entries {
491 if path.is_symlink() || !path.is_dir() {
493 continue;
494 }
495 let Some(name) = path.file_name().map(|n| n.to_string_lossy()) else {
497 continue;
498 };
499 if name.starts_with('.') || name == BUILD_DIRECTORY || name == "target" {
500 continue;
501 }
502 collect_matching_manifest_dirs(&path, requested, alternate, depth + 1, exact, alt);
503 }
504}
505
506pub(crate) fn canonicalize_dependency_path_relative_to(base: &Path, mut dependency: Dependency) -> Result<Dependency> {
509 if let Some(path) = &mut dependency.path {
510 let joined = if path.is_absolute() { path.clone() } else { base.join(&path) };
511 *path = joined.canonicalize().map_err(|e| crate::errors::failed_path(joined.display(), e))?;
512 }
513 Ok(dependency)
514}
515
516fn parse_dependencies_from_aleo(
518 name: Symbol,
519 bytecode: &str,
520 existing: &IndexMap<Symbol, Dependency>,
521) -> Result<IndexSet<Dependency>> {
522 let program_size = bytecode.len();
524
525 if program_size > MAX_PROGRAM_SIZE {
526 return Err(leo_errors::LeoError::Backtraced(crate::errors::program_size_limit_exceeded(
527 name,
528 program_size,
529 MAX_PROGRAM_SIZE,
530 )));
531 }
532
533 let svm_program: SvmProgram<TestnetV0> =
535 bytecode.parse().map_err(|_| crate::errors::snarkvm_parsing_error(name))?;
536 let dependencies = svm_program
537 .imports()
538 .keys()
539 .map(|program_id| {
540 if let Some(dependency) = existing.get(&Symbol::intern(&program_id.to_string())) {
543 dependency.clone()
544 } else {
545 let name = program_id.to_string();
546 Dependency { name, location: Location::Network, path: None, edition: None, ..Default::default() }
547 }
548 })
549 .collect();
550 Ok(dependencies)
551}