Skip to content

Commit 6ca410f

Browse files
committed
Don't include the cargo target directory in wheels built from sdists
1 parent 4c4d695 commit 6ca410f

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎src/binding_generator/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ where
386386
writer,
387387
&context.project.project_layout,
388388
context.project.pyproject_toml.as_ref(),
389+
&context.project.target_dir,
389390
)
390391
.context("Failed to add the python module to the package")?;
391392
}

‎src/module_writer/mod.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub fn write_python_part(
119119
writer: &mut VirtualWriter<WheelWriter>,
120120
project_layout: &ProjectLayout,
121121
pyproject_toml: Option<&PyProjectToml>,
122+
target_dir: &Path,
122123
) -> Result<()> {
123124
let python_dir = &project_layout.python_dir;
124125
let mut python_packages = Vec::new();
@@ -133,11 +134,23 @@ pub fn write_python_part(
133134
python_packages.push(package_path);
134135
}
135136

137+
// Skip the cargo target directory unless it contains a python package (#3319)
138+
let target_dir = target_dir
139+
.normalize()
140+
.ok()
141+
.map(|dir| dir.into_path_buf())
142+
.filter(|dir| {
143+
!python_packages
144+
.iter()
145+
.any(|package| package.starts_with(dir))
146+
});
147+
136148
for absolute in WalkBuilder::new(&project_layout.project_root)
137149
.hidden(false)
138150
.parents(false)
139151
.git_global(false)
140152
.git_exclude(false)
153+
.filter_entry(move |entry| target_dir.as_deref() != Some(entry.path()))
141154
.build()
142155
{
143156
let absolute = match absolute {

‎tests/common/other.rs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ pub fn check_sdist_mtimes(
274274
}
275275

276276
fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<ZipArchive<File>> {
277+
let target_dir = crate::common::shared_target_dir(&package);
278+
build_wheel_files_with_target_dir(package, target_dir, unique_name)
279+
}
280+
281+
fn build_wheel_files_with_target_dir(
282+
package: impl AsRef<Path>,
283+
target_dir: PathBuf,
284+
unique_name: &str,
285+
) -> Result<ZipArchive<File>> {
277286
let manifest_path = package.as_ref().join("Cargo.toml");
278287
let wheel_directory = Path::new("test-crates").join("wheels").join(unique_name);
279288

@@ -285,7 +294,7 @@ fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<Zip
285294
cargo: CargoOptions {
286295
manifest_path: Some(manifest_path),
287296
quiet: true,
288-
target_dir: Some(crate::common::shared_target_dir(&package)),
297+
target_dir: Some(target_dir),
289298
..Default::default()
290299
},
291300
platform: PlatformOptions {
@@ -737,6 +746,31 @@ pub fn test_unreadable_dir() -> Result<()> {
737746
Ok(())
738747
}
739748

749+
/// Test that a target directory inside the python package isn't packaged outside of git.
750+
///
751+
/// See https://github.com/PyO3/maturin/issues/3319
752+
pub fn test_target_dir_in_python_package() -> Result<()> {
753+
let temp_dir = tempfile::tempdir()?;
754+
let project_dir = temp_dir.path().join("bin-with-python-module");
755+
copy_dir_recursive(
756+
Path::new("test-crates/bin-with-python-module"),
757+
&project_dir,
758+
)?;
759+
760+
let target_dir = project_dir.join("bin_with_python_module/target");
761+
let wheel = build_wheel_files_with_target_dir(
762+
&project_dir,
763+
target_dir,
764+
"wheel-files-bin-with-python-module-target-dir",
765+
)?;
766+
let package_files: Vec<_> = wheel
767+
.file_names()
768+
.filter(|name| name.starts_with("bin_with_python_module/"))
769+
.collect();
770+
assert_eq!(package_files, ["bin_with_python_module/__init__.py"]);
771+
Ok(())
772+
}
773+
740774
/// Test that building wheels from an sdist works correctly.
741775
/// This simulates the `maturin build --sdist` workflow: build sdist first,
742776
/// unpack it, then build wheels from the unpacked sdist.

‎tests/run/wheel.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ fn pyo3_wheel_record_has_normalized_paths() {
6565
"wheel-record-pyo3-mixed-include-exclude",
6666
))
6767
}
68+
69+
#[test]
70+
fn target_dir_in_python_package() {
71+
handle_result(other::test_target_dir_in_python_package())
72+
}

0 commit comments

Comments
 (0)