Run `uv build` builds in the source distribution bucket (#8220)

When building a source distribution to a wheels, we perform the build
inside a temporary directory inside the output directory. By default,
the output directory is `dist/` in the repository root. This temp dir
placement allows us to move the final wheel to the output directory
instead of copying it (a temp dir might be on another device, which
means we need to copy instead of moving).

Some build backends such as hatchling traverse upwards from the current
directory (the source dist build location) looking for gitignore files
to consider. By adding a gitignore in `dist/` with `*`, we caused
hatchling to ignore all files in our temporary build directory below it,
causing empty wheels. To prevent this, we add a `.git` file as a phony
git root. We are already using this trick successfully in the cache.
Hatchling sees this `.git` file, considers it a boundary and does not
traverse up to `dist/.gitignore`.

Fixes #8200

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
konsti 2024-10-15 19:29:50 +02:00 committed by GitHub
parent 824dedad76
commit 3d27b484ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 3 deletions

View File

@ -631,7 +631,7 @@ pub enum CacheBucket {
/// can put next to the wheels as in the `Wheels` bucket. /// can put next to the wheels as in the `Wheels` bucket.
/// ///
/// The unzipped source distribution is stored in a directory matching the source distribution /// The unzipped source distribution is stored in a directory matching the source distribution
/// acrhive name. /// archive name.
/// ///
/// Source distributions are built into zipped wheel files (as PEP 517 specifies) and unzipped /// Source distributions are built into zipped wheel files (as PEP 517 specifies) and unzipped
/// lazily before installing. So when resolving, we only build the wheel and store the archive /// lazily before installing. So when resolving, we only build the wheel and store the archive

View File

@ -11,7 +11,7 @@ use uv_distribution_types::{DependencyMetadata, IndexLocations};
use uv_install_wheel::linker::LinkMode; use uv_install_wheel::linker::LinkMode;
use uv_auth::store_credentials_from_url; use uv_auth::store_credentials_from_url;
use uv_cache::Cache; use uv_cache::{Cache, CacheBucket};
use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_configuration::{ use uv_configuration::{
BuildKind, BuildOptions, BuildOutput, Concurrency, ConfigSettings, Constraints, BuildKind, BuildOptions, BuildOutput, Concurrency, ConfigSettings, Constraints,
@ -572,7 +572,7 @@ async fn build_package(
let ext = SourceDistExtension::from_path(path.as_path()).map_err(|err| { let ext = SourceDistExtension::from_path(path.as_path()).map_err(|err| {
anyhow::anyhow!("`{}` is not a valid source distribution, as it ends with an unsupported extension. Expected one of: {err}.", path.user_display()) anyhow::anyhow!("`{}` is not a valid source distribution, as it ends with an unsupported extension. Expected one of: {err}.", path.user_display())
})?; })?;
let temp_dir = tempfile::tempdir_in(&output_dir)?; let temp_dir = tempfile::tempdir_in(cache.bucket(CacheBucket::SourceDistributions))?;
uv_extract::stream::archive(reader, ext, temp_dir.path()).await?; uv_extract::stream::archive(reader, ext, temp_dir.path()).await?;
// Extract the top-level directory from the archive. // Extract the top-level directory from the archive.

View File

@ -1,7 +1,10 @@
use crate::common::{uv_snapshot, TestContext}; use crate::common::{uv_snapshot, TestContext};
use anyhow::Result; use anyhow::Result;
use assert_fs::prelude::*; use assert_fs::prelude::*;
use fs_err::File;
use insta::assert_snapshot;
use predicates::prelude::predicate; use predicates::prelude::predicate;
use zip::ZipArchive;
#[test] #[test]
fn build() -> Result<()> { fn build() -> Result<()> {
@ -1762,3 +1765,56 @@ fn build_no_build_logs() -> Result<()> {
Ok(()) Ok(())
} }
/// Check that we have a working git boundary for builds from source dist to wheel in `dist/`.
#[test]
fn git_boundary_in_dist_build() -> Result<()> {
let context = TestContext::new("3.12");
let project = context.temp_dir.child("demo");
project.child("pyproject.toml").write_str(
r#"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "demo"
version = "0.1.0"
requires-python = ">=3.11"
"#,
)?;
project.child("src/demo/__init__.py").write_str(
r#"
def run():
print("Running like the wind!")
"#,
)?;
uv_snapshot!(&context.filters(), context.build().current_dir(project.path()), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Building source distribution...
Building wheel from source distribution...
Successfully built dist/demo-0.1.0.tar.gz and dist/demo-0.1.0-py3-none-any.whl
"###);
// Check that the source file is included
let reader = File::open(project.join("dist/demo-0.1.0-py3-none-any.whl"))?;
let mut files: Vec<_> = ZipArchive::new(reader)?
.file_names()
.map(ToString::to_string)
.collect();
files.sort();
assert_snapshot!(files.join("\n"), @r###"
demo-0.1.0.dist-info/METADATA
demo-0.1.0.dist-info/RECORD
demo-0.1.0.dist-info/WHEEL
demo/__init__.py
"###);
Ok(())
}