From 9f1bd395758cb2b8b16cfb23102dc002eba83adb Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Thu, 29 Jun 2023 11:59:22 +0200 Subject: [PATCH 1/3] fix: canonicalize path before parsing it as path. --- src/cli/init.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cli/init.rs b/src/cli/init.rs index f3ae3559a..7b1bbb7ea 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -1,4 +1,5 @@ use crate::{config::get_default_author, consts}; +use anyhow::anyhow; use clap::Parser; use minijinja::{context, Environment}; use rattler_conda_types::Platform; @@ -36,7 +37,7 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments pub async fn execute(args: Args) -> anyhow::Result<()> { let env = Environment::new(); - let dir = args.path; + let dir = args.path.canonicalize()?; let manifest_path = dir.join(consts::PROJECT_MANIFEST); let gitignore_path = dir.join(".gitignore"); @@ -49,7 +50,15 @@ pub async fn execute(args: Args) -> anyhow::Result<()> { fs::create_dir_all(&dir).ok(); // Write pixi.toml - let name = dir.file_name().unwrap().to_string_lossy(); + let name = dir + .file_name() + .ok_or_else(|| { + anyhow!( + "Cannot get file or directory name from the path: {}", + dir.to_string_lossy() + ) + })? + .to_string_lossy(); let version = "0.1.0"; let author = get_default_author(); let channel = "conda-forge"; From 1e94885a4daca722cd72e3e69158b7d87e988846 Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Thu, 29 Jun 2023 15:54:59 +0200 Subject: [PATCH 2/3] add test and make the get dir smarter and error more verbose --- src/cli/init.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/cli/init.rs b/src/cli/init.rs index 98ae805ee..ea55e9325 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -3,6 +3,7 @@ use anyhow::anyhow; use clap::Parser; use minijinja::{context, Environment}; use rattler_conda_types::Platform; +use std::io::{Error, ErrorKind}; use std::{fs, path::PathBuf}; /// Creates a new project @@ -41,7 +42,7 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments pub async fn execute(args: Args) -> anyhow::Result<()> { let env = Environment::new(); - let dir = args.path.canonicalize()?; + let dir = get_dir(args.path)?; let manifest_path = dir.join(consts::PROJECT_MANIFEST); let gitignore_path = dir.join(".gitignore"); @@ -102,3 +103,53 @@ pub async fn execute(args: Args) -> anyhow::Result<()> { Ok(()) } + +fn get_dir(path: PathBuf) -> Result { + if path.components().count() == 1 { + Ok(std::env::current_dir().unwrap_or_default().join(path)) + } else { + path.canonicalize().map_err(|e| match e.kind() { + ErrorKind::NotFound => Error::new( + ErrorKind::NotFound, + format!( + "Cannot find '{}' please make sure the folder is reachable", + path.to_string_lossy() + ), + ), + _ => Error::new( + ErrorKind::InvalidInput, + "Cannot canonicalize the given path", + ), + }) + } +} + +#[cfg(test)] +mod tests { + use crate::cli::init::get_dir; + use std::path::PathBuf; + + #[test] + fn test_get_name() { + assert_eq!( + get_dir(PathBuf::from(".")).unwrap(), + std::env::current_dir().unwrap() + ); + assert_eq!( + get_dir(PathBuf::from("test_folder")).unwrap(), + std::env::current_dir().unwrap().join("test_folder") + ); + assert_eq!( + get_dir(std::env::current_dir().unwrap()).unwrap(), + PathBuf::from(std::env::current_dir().unwrap()) + ); + } + + #[test] + fn test_get_name_panic() { + match get_dir(PathBuf::from("invalid/path")) { + Ok(_) => panic!("Expected error, but got OK"), + Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound), + } + } +} From e170607d83cb7a4af21732576190a42e4c99fc73 Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Thu, 29 Jun 2023 16:11:08 +0200 Subject: [PATCH 3/3] fix: canonicalize both current dirs for windows --- src/cli/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/init.rs b/src/cli/init.rs index ea55e9325..0b5f58d3b 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -141,7 +141,7 @@ mod tests { ); assert_eq!( get_dir(std::env::current_dir().unwrap()).unwrap(), - PathBuf::from(std::env::current_dir().unwrap()) + PathBuf::from(std::env::current_dir().unwrap().canonicalize().unwrap()) ); }