From 7dd34cec791e6fdf540ec37bc314fa0856efd8d4 Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Sun, 24 Nov 2024 21:03:03 +0200 Subject: [PATCH] chore(infra): canonicalize path resolution commit-id:c2547652 --- crates/infra_utils/src/path.rs | 19 +++++++------------ crates/infra_utils/src/path_test.rs | 8 ++------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/infra_utils/src/path.rs b/crates/infra_utils/src/path.rs index cfa2444a24..adca04e157 100644 --- a/crates/infra_utils/src/path.rs +++ b/crates/infra_utils/src/path.rs @@ -1,6 +1,6 @@ -use std::env; use std::path::{Path, PathBuf}; use std::sync::LazyLock; +use std::{env, fs}; use thiserror::Error; @@ -10,11 +10,8 @@ mod path_test; #[derive(Debug, Error)] pub enum PathResolutionError { - // TODO(Arni): Handle manifest dir not exist here? - #[error("No file exists at '{path}'")] - PathDoesNotExist { path: PathBuf }, - /// This error is raised when file existence can be neither confirmed nor denied. See - /// [`std::path::Path::try_exists`] for more information. + /// This error is raised when the file path does not exist, or when a non-final component in a + /// path is not a directory. See [`std::fs::canonicalize`] for more information. #[error(transparent)] IoError(#[from] std::io::Error), } @@ -23,6 +20,7 @@ pub enum PathResolutionError { static PATH_TO_CARGO_MANIFEST_DIR: LazyLock> = LazyLock::new(|| env::var("CARGO_MANIFEST_DIR").ok().map(|dir| Path::new(&dir).into())); +// TODO(Tsabary): should not be public. Use a getter instead. pub fn cargo_manifest_dir() -> Option { PATH_TO_CARGO_MANIFEST_DIR.clone() } @@ -34,16 +32,13 @@ pub fn cargo_manifest_dir() -> Option { /// * `relative_path` - A string slice representing the relative path from the project root. /// /// # Returns -/// * An absolute `PathBuf` representing the resolved path starting from the project root. +/// * A `PathBuf` representing the resolved path starting from the project root. pub fn resolve_project_relative_path(relative_path: &str) -> Result { let base_dir = path_of_project_root(); - let path = base_dir.join(relative_path); - if !path.try_exists()? { - return Err(PathResolutionError::PathDoesNotExist { path }); - } + let absolute_path = fs::canonicalize(path)?; - Ok(path) + Ok(absolute_path) } fn path_of_project_root() -> PathBuf { diff --git a/crates/infra_utils/src/path_test.rs b/crates/infra_utils/src/path_test.rs index 88d7f5ff23..f0b757ae82 100644 --- a/crates/infra_utils/src/path_test.rs +++ b/crates/infra_utils/src/path_test.rs @@ -1,4 +1,4 @@ -use crate::path::{path_of_project_root, resolve_project_relative_path, PathResolutionError}; +use crate::path::{path_of_project_root, resolve_project_relative_path}; // TODO: Add a test for PathResolutionError::IoError. #[test] @@ -8,11 +8,7 @@ fn resolve_project_relative_path_on_non_existent_path() { assert!(!expected_path.exists()); let result = resolve_project_relative_path(relative_path); - if let Err(PathResolutionError::PathDoesNotExist { path }) = result { - assert_eq!(path, expected_path); - } else { - panic!("Expected PathDoesNotExist error, got {:?}", result); - } + assert!(result.is_err(), "Expected an non-existent path error, got {:?}", result); } #[test]