From 570b5bd52ec0333eed4c7b3adcf8b1f8374eec56 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Thu, 25 Jul 2024 10:58:38 -0600 Subject: [PATCH] Don't check .gitignore files outside of project directory When building inside a .gitignore'd output directory as part of another build system maturin may accidentially pick up the parent .gitignore. Lets just disable searching for parent .gitignore files since this functionality is broken anyways as the logic does not match git's .gitignore logic anyways. For example in buildroot we have an output directory that has its contents gitignored in which maturin will build packages, changing some minor implementation details in how we ignore this directory triggered this bug. https://github.com/buildroot/buildroot/commit/a14c862c08865e053d5fce90a0d0323b8f9e4bc0 As the output dir files are supposed to be ignored both before and after this change this indicates that ignore::WalkBuilder parent matching is fundamentally broken and can not be used reliably in the first place as it does not match git's ignore logic. Also disable git exclude and git global excludes which may cause problems. Signed-off-by: James Hilliard --- src/module_writer.rs | 11 ++++++++++- src/project_layout.rs | 4 ++++ test-crates/pyo3-mixed-implicit/.gitignore | 1 + test-crates/pyo3-mixed-include-exclude/.gitignore | 1 + test-crates/pyo3-mixed-py-subdir/.gitignore | 1 + test-crates/pyo3-mixed-src/.gitignore | 1 + test-crates/pyo3-mixed-submodule/.gitignore | 1 + test-crates/pyo3-mixed-with-path-dep/.gitignore | 1 + test-crates/pyo3-mixed/.gitignore | 1 + tests/run.rs | 2 ++ 10 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test-crates/pyo3-mixed-implicit/.gitignore create mode 100644 test-crates/pyo3-mixed-include-exclude/.gitignore create mode 100644 test-crates/pyo3-mixed-py-subdir/.gitignore create mode 100644 test-crates/pyo3-mixed-src/.gitignore create mode 100644 test-crates/pyo3-mixed-submodule/.gitignore create mode 100644 test-crates/pyo3-mixed-with-path-dep/.gitignore create mode 100644 test-crates/pyo3-mixed/.gitignore diff --git a/src/module_writer.rs b/src/module_writer.rs index f0283fb0f..9e85b3556 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -1286,8 +1286,17 @@ pub fn write_python_part( } for package in python_packages { - for absolute in WalkBuilder::new(package).hidden(false).build() { + for absolute in WalkBuilder::new(&project_layout.project_root) + .hidden(false) + .parents(false) + .git_global(false) + .git_exclude(false) + .build() + { let absolute = absolute?.into_path(); + if !absolute.starts_with(package.as_path()) { + continue; + } let relative = absolute.strip_prefix(python_dir).unwrap(); if absolute.is_dir() { writer.add_directory(relative)?; diff --git a/src/project_layout.rs b/src/project_layout.rs index 1714530ba..fe53fd5f8 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -14,6 +14,8 @@ const PYPROJECT_TOML: &str = "pyproject.toml"; /// Whether this project is pure rust or rust mixed with python and whether it has wheel data #[derive(Clone, Debug, PartialEq, Eq)] pub struct ProjectLayout { + /// Contains the absolute path to the project root directory + pub project_root: PathBuf, /// Contains the absolute path to the python source directory pub python_dir: PathBuf, /// Contains the canonicalized (i.e. absolute) path to the python part of the project @@ -397,6 +399,7 @@ impl ProjectLayout { eprintln!("🍹 Building a mixed python/rust project"); Ok(ProjectLayout { + project_root: project_root.to_path_buf(), python_dir: python_root, python_packages, python_module: Some(python_module), @@ -415,6 +418,7 @@ impl ProjectLayout { } Ok(ProjectLayout { + project_root: project_root.to_path_buf(), python_dir: python_root, python_packages, python_module: None, diff --git a/test-crates/pyo3-mixed-implicit/.gitignore b/test-crates/pyo3-mixed-implicit/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-implicit/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed-include-exclude/.gitignore b/test-crates/pyo3-mixed-include-exclude/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-include-exclude/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed-py-subdir/.gitignore b/test-crates/pyo3-mixed-py-subdir/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-py-subdir/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed-src/.gitignore b/test-crates/pyo3-mixed-src/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-src/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed-submodule/.gitignore b/test-crates/pyo3-mixed-submodule/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-submodule/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed-with-path-dep/.gitignore b/test-crates/pyo3-mixed-with-path-dep/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed-with-path-dep/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/test-crates/pyo3-mixed/.gitignore b/test-crates/pyo3-mixed/.gitignore new file mode 100644 index 000000000..18880a5ac --- /dev/null +++ b/test-crates/pyo3-mixed/.gitignore @@ -0,0 +1 @@ +*.py[cdo] diff --git a/tests/run.rs b/tests/run.rs index b6eed3410..eca9416da 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -729,6 +729,7 @@ fn pyo3_mixed_include_exclude_sdist() { SdistGenerator::Cargo, expect![[r#" { + "pyo3_mixed_include_exclude-2.1.3/.gitignore", "pyo3_mixed_include_exclude-2.1.3/Cargo.lock", "pyo3_mixed_include_exclude-2.1.3/Cargo.toml", "pyo3_mixed_include_exclude-2.1.3/PKG-INFO", @@ -758,6 +759,7 @@ fn pyo3_mixed_include_exclude_git_sdist_generator() { SdistGenerator::Git, expect![[r#" { + "pyo3_mixed_include_exclude-2.1.3/.gitignore", "pyo3_mixed_include_exclude-2.1.3/Cargo.lock", "pyo3_mixed_include_exclude-2.1.3/Cargo.toml", "pyo3_mixed_include_exclude-2.1.3/PKG-INFO",