From f6e5409fd44ae536b9559c85422ae36f3f8da215 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Thu, 14 Nov 2024 12:40:21 +0100 Subject: [PATCH] feat: use gitignore when copying the recipe files (#1193) Also ignores the output dir when copying the recipe contents and ignores hidden files when copying the recipe --- src/lib.rs | 9 --------- src/packaging.rs | 34 +++++++++++++++++++++++++++++----- src/recipe/parser/glob_vec.rs | 1 - src/source/copy_dir.rs | 7 ++++--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ffd6f4fcc..d34e5a64c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,15 +151,6 @@ pub async fn get_build_output( if output_dir.exists() { output_dir = canonicalize(&output_dir).into_diagnostic()?; } - if output_dir.starts_with( - recipe_path - .parent() - .expect("Could not get parent of recipe"), - ) { - return Err(miette::miette!( - "The output directory cannot be in the recipe directory.\nThe current output directory is: {}\nSelect a different output directory with the --output-dir option or set the CONDA_BLD_PATH environment variable" - , output_dir.to_string_lossy())); - } let recipe_text = fs::read_to_string(recipe_path).into_diagnostic()?; diff --git a/src/packaging.rs b/src/packaging.rs index b67d469d9..7bdb57c9f 100644 --- a/src/packaging.rs +++ b/src/packaging.rs @@ -22,7 +22,14 @@ mod metadata; pub use file_finder::{content_type, Files, TempFiles}; pub use metadata::{contains_prefix_binary, contains_prefix_text, create_prefix_placeholder}; -use crate::{metadata::Output, package_test::write_test_files, post_process, tool_configuration}; +use crate::{ + metadata::Output, + package_test::write_test_files, + post_process, + recipe::parser::GlobVec, + source::{self, copy_dir}, + tool_configuration, +}; #[allow(missing_docs)] #[derive(Debug, thiserror::Error)] @@ -58,7 +65,7 @@ pub enum PackagingError { RelinkError(#[from] crate::post_process::relink::RelinkError), #[error(transparent)] - SourceError(#[from] crate::source::SourceError), + SourceError(#[from] source::SourceError), #[error("could not create python entry point: {0}")] CannotCreateEntryPoint(String), @@ -90,7 +97,7 @@ fn copy_license_files( let licenses_folder = tmp_dir_path.join("info/licenses/"); fs::create_dir_all(&licenses_folder)?; - let copy_dir = crate::source::copy_dir::CopyDir::new( + let copy_dir = copy_dir::CopyDir::new( &output.build_configuration.directories.recipe_dir, &licenses_folder, ) @@ -101,7 +108,7 @@ fn copy_license_files( let copied_files_recipe_dir = copy_dir.copied_paths(); let any_include_matched_recipe_dir = copy_dir.any_include_glob_matched(); - let copy_dir = crate::source::copy_dir::CopyDir::new( + let copy_dir = copy_dir::CopyDir::new( &output.build_configuration.directories.work_dir, &licenses_folder, ) @@ -139,8 +146,25 @@ fn write_recipe_folder( let recipe_folder = tmp_dir_path.join("info/recipe/"); let recipe_dir = &output.build_configuration.directories.recipe_dir; let recipe_path = &output.build_configuration.directories.recipe_path; + let output_dir = &output.build_configuration.directories.output_dir; + + let mut copy_builder = copy_dir::CopyDir::new(recipe_dir, &recipe_folder) + .use_gitignore(true) + .ignore_hidden_files(true); + + // if the output dir is inside the same directory as the recipe, then we + // need to ignore the output dir when copying + if let Ok(ignore_output) = output_dir.strip_prefix(recipe_dir) { + tracing::info!( + "Ignoring output dir in recipe folder: {}", + output_dir.to_string_lossy() + ); + let output_dir_glob = format!("{}/**", ignore_output.to_string_lossy()); + let glob_vec = GlobVec::from_vec(vec![], Some(vec![&output_dir_glob])); + copy_builder = copy_builder.with_globvec(&glob_vec); + } - let copy_result = crate::source::copy_dir::CopyDir::new(recipe_dir, &recipe_folder).run()?; + let copy_result = copy_builder.run()?; let mut files = Vec::from(copy_result.copied_paths()); diff --git a/src/recipe/parser/glob_vec.rs b/src/recipe/parser/glob_vec.rs index 3bf11c2a6..930e1c49a 100644 --- a/src/recipe/parser/glob_vec.rs +++ b/src/recipe/parser/glob_vec.rs @@ -179,7 +179,6 @@ impl GlobVec { } /// Only used for testing - #[cfg(test)] pub fn from_vec(include: Vec<&str>, exclude: Option>) -> Self { let include_vec: Vec = include .into_iter() diff --git a/src/source/copy_dir.rs b/src/source/copy_dir.rs index 3197b801f..4c48d2993 100644 --- a/src/source/copy_dir.rs +++ b/src/source/copy_dir.rs @@ -107,8 +107,6 @@ pub(crate) fn copy_file( /// It uses the `ignore` crate to read the `.gitignore` file in the source directory and uses the globs /// to filter the files and directories to copy. /// -/// The copy process also ignores hidden files and directories by default. -/// /// # Return /// /// The returned `Vec` contains the paths of the copied files. @@ -130,8 +128,11 @@ impl<'a> CopyDir<'a> { from_path, to_path, globvec: GlobVec::default(), + // use the gitignore file by default use_gitignore: false, + // use the global git ignore file by default use_git_global: false, + // include hidden files by default hidden: false, copy_options: CopyOptions::default(), } @@ -154,7 +155,7 @@ impl<'a> CopyDir<'a> { } #[allow(unused)] - pub fn hidden(mut self, b: bool) -> Self { + pub fn ignore_hidden_files(mut self, b: bool) -> Self { self.hidden = b; self }