Skip to content

Commit

Permalink
Run uv build builds in the source distribution bucket (astral-sh#8220)
Browse files Browse the repository at this point in the history
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 astral-sh#8200

---------

Co-authored-by: Charlie Marsh <[email protected]>
  • Loading branch information
konstin and charliermarsh authored Oct 15, 2024
1 parent 824deda commit 3d27b48
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ pub enum CacheBucket {
/// can put next to the wheels as in the `Wheels` bucket.
///
/// 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
/// lazily before installing. So when resolving, we only build the wheel and store the archive
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/build_frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uv_distribution_types::{DependencyMetadata, IndexLocations};
use uv_install_wheel::linker::LinkMode;

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_configuration::{
BuildKind, BuildOptions, BuildOutput, Concurrency, ConfigSettings, Constraints,
Expand Down Expand Up @@ -572,7 +572,7 @@ async fn build_package(
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())
})?;
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?;

// Extract the top-level directory from the archive.
Expand Down
56 changes: 56 additions & 0 deletions crates/uv/tests/it/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::common::{uv_snapshot, TestContext};
use anyhow::Result;
use assert_fs::prelude::*;
use fs_err::File;
use insta::assert_snapshot;
use predicates::prelude::predicate;
use zip::ZipArchive;

#[test]
fn build() -> Result<()> {
Expand Down Expand Up @@ -1762,3 +1765,56 @@ fn build_no_build_logs() -> Result<()> {

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(())
}

0 comments on commit 3d27b48

Please sign in to comment.