From 2d45fc76bfd8aec88a11286b644944d9b50d7f87 Mon Sep 17 00:00:00 2001 From: Ethan Uppal <113849268+ethanuppal@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:48:00 -0400 Subject: [PATCH] Support absolute imports (#2179) --- calyx-frontend/src/workspace.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/calyx-frontend/src/workspace.rs b/calyx-frontend/src/workspace.rs index 1518176e15..d6ffaca3f3 100644 --- a/calyx-frontend/src/workspace.rs +++ b/calyx-frontend/src/workspace.rs @@ -54,8 +54,11 @@ pub struct Workspace { impl Workspace { /// Returns the absolute location to an imported file. - /// Imports can refer to files either in the library path or in the parent - /// folder. + /// + /// An import path is first resolved as an absolute or + /// relative(-to-`parent`) path, and if no file exists at either such + /// extended path exists, it assumed to be under the library path + /// `lib_path`. fn canonicalize_import( import: S, parent: &Path, @@ -64,17 +67,23 @@ impl Workspace { where S: AsRef + Clone, { - let parent_path = parent.join(import.clone()); - if parent_path.exists() { - return Ok(parent_path); + let absolute_import = import.as_ref(); + if absolute_import.is_absolute() && absolute_import.exists() { + return Ok(import.as_ref().to_path_buf()); } - let lib = lib_path.join(import.clone()); - if lib.exists() { - return Ok(lib); + + let relative_import = parent.join(import.clone()); + if relative_import.exists() { + return Ok(relative_import); + } + + let library_import = lib_path.join(import.clone()); + if library_import.exists() { + return Ok(library_import); } Err(Error::invalid_file( - format!("Import path `{}` found neither in the parent ({}) nor library path ({})", + format!("Import path `{}` found neither as an absolute path, nor in the parent ({}), nor in library path ({})", import.as_ref().to_string_lossy(), parent.to_string_lossy(), lib_path.to_string_lossy()