Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve build backend excludes #9281

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 74 additions & 38 deletions crates/uv-build-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::metadata::{PyProjectToml, ValidationError};
use flate2::write::GzEncoder;
use flate2::Compression;
use fs_err::File;
use globset::{Glob, GlobSetBuilder};
use globset::{Glob, GlobSet, GlobSetBuilder};
use itertools::Itertools;
use sha2::{Digest, Sha256};
use std::fs::FileType;
Expand Down Expand Up @@ -318,6 +318,20 @@ pub fn build_wheel(
debug!("Writing wheel at {}", wheel_path.user_display());
let mut wheel_writer = ZipDirectoryWriter::new_wheel(File::create(&wheel_path)?);

// Wheel excludes
// TODO(konstin): The must be stronger than the source dist excludes, otherwise we can get more
// files in source tree -> wheel than for source tree -> source dist -> wheel.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say why you don't want to get more files in source tree -> wheel versus source tree -> source dist -> wheel?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine we exclude src/a/b.py in source dist, but not in wheel. That means in source dist -> wheel, src/a/b.py is part of the final packages, but we remove it in source tree -> source dist, so the source dist -> wheel step can't include it (the file doesn't exist in the .tar.gz), so src/a/b.py isn't part of the final package.

let default_excludes: &[String] = &[
"__pycache__".to_string(),
"*.pyc".to_string(),
"*.pyo".to_string(),
];
let excludes = pyproject_toml
.wheel_settings()
.and_then(|settings| settings.exclude.as_deref())
.unwrap_or(default_excludes);
let exclude_matcher = build_exclude_matcher(excludes)?;

debug!("Adding content files to {}", wheel_path.user_display());
let module_root = pyproject_toml
.wheel_settings()
Expand All @@ -331,7 +345,10 @@ pub fn build_wheel(
if !module_root.join("__init__.py").is_file() {
return Err(Error::MissingModule(module_root));
}
for entry in WalkDir::new(module_root) {
for entry in WalkDir::new(module_root)
.into_iter()
.filter_entry(|entry| !exclude_matcher.is_match(entry.path()))
{
let entry = entry.map_err(|err| Error::WalkDir {
root: source_tree.to_path_buf(),
err,
Expand All @@ -340,9 +357,11 @@ pub fn build_wheel(
let relative_path = entry
.path()
.strip_prefix(&strip_root)
.expect("walkdir starts with root")
.user_display()
.to_string();
.expect("walkdir starts with root");
if exclude_matcher.is_match(relative_path) {
trace!("Excluding from module: `{}`", relative_path.user_display());
}
let relative_path = relative_path.user_display().to_string();

debug!("Adding to wheel: `{relative_path}`");

Expand Down Expand Up @@ -484,15 +503,20 @@ fn wheel_subdir_from_globs(
let license_files_globs: Vec<_> = globs
.iter()
.map(|license_files| {
trace!("Including license files at: `{license_files}`");
trace!(
"Including {} at `{}` with `{}`",
globs_field,
src.user_display(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the thing that will emit an empty string for the CWD? Can we make it emit ./ instead for that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea, but it affects more places so I'll follow up

license_files
);
parse_portable_glob(license_files)
})
.collect::<Result<_, _>>()
.map_err(|err| Error::PortableGlob {
field: globs_field.to_string(),
source: err,
})?;
let license_files_matcher =
let matcher =
GlobDirFilter::from_globs(&license_files_globs).map_err(|err| Error::GlobSetTooLarge {
field: globs_field.to_string(),
source: err,
Expand All @@ -508,7 +532,7 @@ fn wheel_subdir_from_globs(
.expect("walkdir starts with root");

// Fast path: Don't descend into a directory that can't be included.
license_files_matcher.match_directory(relative)
matcher.match_directory(relative)
}) {
let entry = entry.map_err(|err| Error::WalkDir {
root: src.to_path_buf(),
Expand All @@ -520,8 +544,8 @@ fn wheel_subdir_from_globs(
.strip_prefix(src)
.expect("walkdir starts with root");

if !license_files_matcher.match_path(relative) {
trace!("Excluding {}", relative.user_display());
if !matcher.match_path(relative) {
trace!("Excluding {}: `{}`", globs_field, relative.user_display());
continue;
};

Expand Down Expand Up @@ -686,26 +710,7 @@ pub fn build_source_dist(
source: err,
})?;

let mut exclude_builder = GlobSetBuilder::new();
for exclude in settings.exclude {
// Excludes are unanchored
let exclude = if let Some(exclude) = exclude.strip_prefix("/") {
exclude.to_string()
} else {
format!("**/{exclude}").to_string()
};
let glob = parse_portable_glob(&exclude).map_err(|err| Error::PortableGlob {
field: "tool.uv.source-dist.exclude".to_string(),
source: err,
})?;
exclude_builder.add(glob);
}
let exclude_matcher = exclude_builder
.build()
.map_err(|err| Error::GlobSetTooLarge {
field: "tool.uv.source-dist.exclude".to_string(),
source: err,
})?;
let exclude_matcher = build_exclude_matcher(&settings.exclude)?;

// TODO(konsti): Add files linked by pyproject.toml

Expand Down Expand Up @@ -735,7 +740,7 @@ pub fn build_source_dist(
.expect("walkdir starts with root");

if !include_matcher.match_path(relative) || exclude_matcher.is_match(relative) {
trace!("Excluding {}", relative.user_display());
trace!("Excluding: `{}`", relative.user_display());
continue;
};

Expand All @@ -748,6 +753,31 @@ pub fn build_source_dist(
Ok(filename)
}

/// Build a globset matcher for excludes.
fn build_exclude_matcher(excludes: &[String]) -> Result<GlobSet, Error> {
let mut exclude_builder = GlobSetBuilder::new();
for exclude in excludes {
// Excludes are unanchored
let exclude = if let Some(exclude) = exclude.strip_prefix("/") {
exclude.to_string()
} else {
format!("**/{exclude}").to_string()
};
let glob = parse_portable_glob(&exclude).map_err(|err| Error::PortableGlob {
field: "tool.uv.source-dist.exclude".to_string(),
source: err,
})?;
exclude_builder.add(glob);
}
let exclude_matcher = exclude_builder
.build()
.map_err(|err| Error::GlobSetTooLarge {
field: "tool.uv.source-dist.exclude".to_string(),
source: err,
})?;
Ok(exclude_matcher)
}

/// Add a file or a directory to a source distribution.
fn add_source_dist_entry(
tar: &mut tar::Builder<GzEncoder<File>>,
Expand Down Expand Up @@ -1107,6 +1137,12 @@ mod tests {
fs_err::copy(built_by_uv.join(dir), src.path().join(dir)).unwrap();
}

// Add some files to be excluded
let module_root = src.path().join("src").join("built_by_uv");
fs_err::create_dir_all(module_root.join("__pycache__")).unwrap();
File::create(module_root.join("__pycache__").join("compiled.pyc")).unwrap();
File::create(module_root.join("arithmetic").join("circle.pyc")).unwrap();

// Build a wheel from the source tree
let direct_output_dir = TempDir::new().unwrap();
build_wheel(src.path(), direct_output_dir.path(), None, "1.0.0+test").unwrap();
Expand Down Expand Up @@ -1159,13 +1195,6 @@ mod tests {
)
.unwrap();

// Check that we write deterministic wheels.
let wheel_filename = "built_by_uv-0.1.0-py3-none-any.whl";
assert_eq!(
fs_err::read(direct_output_dir.path().join(wheel_filename)).unwrap(),
fs_err::read(indirect_output_dir.path().join(wheel_filename)).unwrap()
);

// Check the contained files and directories
assert_snapshot!(source_dist_contents.iter().map(|path| path.replace('\\', "/")).join("\n"), @r"
built_by_uv-0.1.0/LICENSE-APACHE
Expand Down Expand Up @@ -1220,5 +1249,12 @@ mod tests {
built_by_uv/arithmetic/circle.py
built_by_uv/arithmetic/pi.txt
");

// Check that we write deterministic wheels.
let wheel_filename = "built_by_uv-0.1.0-py3-none-any.whl";
assert_eq!(
fs_err::read(direct_output_dir.path().join(wheel_filename)).unwrap(),
fs_err::read(indirect_output_dir.path().join(wheel_filename)).unwrap()
);
}
}
43 changes: 32 additions & 11 deletions crates/uv-build-backend/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Error;
use globset::{Glob, GlobSetBuilder};
use globset::Glob;
use itertools::Itertools;
use serde::Deserialize;
use std::collections::{BTreeMap, Bound};
Expand All @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use tracing::{debug, trace};
use uv_fs::Simplified;
use uv_globfilter::parse_portable_glob;
use uv_globfilter::{parse_portable_glob, GlobDirFilter};
use uv_normalize::{ExtraName, PackageName};
use uv_pep440::{Version, VersionSpecifiers};
use uv_pep508::{Requirement, VersionOrUrl};
Expand Down Expand Up @@ -328,7 +328,7 @@ impl PyProjectToml {
};

let mut license_files = Vec::new();
let mut license_glob_builder = GlobSetBuilder::new();
let mut license_globs_parsed = Vec::new();
for license_glob in license_globs {
let pep639_glob =
parse_portable_glob(license_glob).map_err(|err| Error::PortableGlob {
Expand All @@ -341,28 +341,38 @@ impl PyProjectToml {
.join(pep639_glob.to_string())
.to_string_lossy()
.to_string();
license_glob_builder.add(Glob::new(&absolute_glob).map_err(|err| {
license_globs_parsed.push(Glob::new(&absolute_glob).map_err(|err| {
Error::GlobSet {
field: "project.license-files".to_string(),
err,
}
})?);
}
let license_globs = license_glob_builder.build().map_err(|err| Error::GlobSet {
field: "project.license-files".to_string(),
err,
})?;
let license_globs =
GlobDirFilter::from_globs(&license_globs_parsed).map_err(|err| {
Error::GlobSetTooLarge {
field: "tool.uv.source-dist.include".to_string(),
source: err,
}
})?;

for entry in WalkDir::new(".") {
for entry in WalkDir::new(root).into_iter().filter_entry(|entry| {
license_globs.match_directory(
entry
.path()
.strip_prefix(root)
.expect("walkdir starts with root"),
)
}) {
let entry = entry.map_err(|err| Error::WalkDir {
root: PathBuf::from("."),
err,
})?;
let relative = entry
.path()
.strip_prefix("./")
.strip_prefix(root)
.expect("walkdir starts with root");
if !license_globs.is_match(relative) {
if !license_globs.match_path(relative) {
trace!("Not a license files match: `{}`", relative.user_display());
continue;
}
Expand Down Expand Up @@ -711,6 +721,17 @@ pub(crate) struct WheelSettings {
/// The directory that contains the module directory, usually `src`, or an empty path when
/// using the flat layout over the src layout.
pub(crate) module_root: Option<PathBuf>,

/// Glob expressions which files and directories to exclude from the previous source
/// distribution includes.
///
/// Excludes are not anchored, which means that `__pycache__` excludes all directories named
/// `__pycache__` and it's children anywhere. To anchor a directory, use a `/` prefix, e.g.,
/// `/dist` will exclude only `<project root>/dist`.
///
/// The glob syntax is the reduced portable glob from
/// [PEP 639](https://peps.python.org/pep-0639/#add-license-FILES-key).
pub(crate) exclude: Option<Vec<String>>,
/// Data includes for wheels.
pub(crate) data: Option<WheelDataIncludes>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-globfilter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() {
.to_path_buf();

if !include_matcher.match_path(&relative) || exclude_matcher.is_match(&relative) {
trace!("Excluding: {}", relative.display());
trace!("Excluding: `{}`", relative.display());
continue;
};
println!("{}", relative.display());
Expand Down
Loading