Skip to content

Commit

Permalink
Accept --build-constraints in uv build
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 5, 2024
1 parent 2636ebe commit 92cb2c3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,15 @@ pub struct BuildArgs {
#[arg(long)]
pub wheel: bool,

/// Constrain build dependencies using the given requirements files when building
/// distributions.
///
/// Constraints files are `requirements.txt`-like files that only control the _version_ of a
/// build dependency that's installed. However, including a package in a constraints file will
/// _not_ trigger the inclusion of that package on its own.
#[arg(long, short, env = "UV_BUILD_CONSTRAINT", value_delimiter = ' ', value_parser = parse_maybe_file_path)]
pub build_constraint: Vec<Maybe<PathBuf>>,

/// The Python interpreter to use for the build environment.
///
/// By default, builds are executed in isolated virtual environments. The
Expand Down
11 changes: 9 additions & 2 deletions crates/uv/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::printer::Printer;
use crate::settings::{ResolverSettings, ResolverSettingsRef};
use std::borrow::Cow;

use crate::commands::pip::operations;
use anyhow::Result;
use distribution_filename::SourceDistExtension;
use owo_colors::OwoColorize;
Expand All @@ -20,6 +21,7 @@ use uv_python::{
EnvironmentPreference, PythonDownloads, PythonEnvironment, PythonInstallation,
PythonPreference, PythonRequest, PythonVersionFile, VersionRequest,
};
use uv_requirements::RequirementsSource;
use uv_resolver::{FlatIndex, RequiresPython};
use uv_types::{BuildContext, BuildIsolation, HashStrategy};
use uv_workspace::{DiscoveryOptions, Workspace};
Expand All @@ -32,6 +34,7 @@ pub(crate) async fn build(
output_dir: Option<PathBuf>,
sdist: bool,
wheel: bool,
build_constraints: Vec<RequirementsSource>,
python: Option<String>,
settings: ResolverSettings,
no_config: bool,
Expand All @@ -49,6 +52,7 @@ pub(crate) async fn build(
output_dir.as_deref(),
sdist,
wheel,
&build_constraints,
python.as_deref(),
settings.as_ref(),
no_config,
Expand Down Expand Up @@ -88,6 +92,7 @@ async fn build_impl(
output_dir: Option<&Path>,
sdist: bool,
wheel: bool,
build_constraints: &[RequirementsSource],
python_request: Option<&str>,
settings: ResolverSettingsRef<'_>,
no_config: bool,
Expand Down Expand Up @@ -225,6 +230,10 @@ async fn build_impl(
store_credentials_from_url(url);
}

// Read build constraints.
let build_constraints =
operations::read_constraints(build_constraints, &client_builder).await?;

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls)
Expand All @@ -251,8 +260,6 @@ async fn build_impl(

// TODO(charlie): These are all default values. We should consider whether we want to make them
// optional on the downstream APIs.
let build_constraints = Constraints::default();
let build_hasher = HashStrategy::default();
let hasher = HashStrategy::None;

// Resolve the flat indexes from `--find-links`.
Expand Down
8 changes: 8 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,20 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
.combine(Refresh::from(args.settings.upgrade.clone())),
);

// Resolve the build constraints.
let build_constraints = args
.build_constraint
.into_iter()
.map(RequirementsSource::from_constraints_txt)
.collect::<Vec<_>>();

commands::build(
args.src,
args.package,
args.out_dir,
args.sdist,
args.wheel,
build_constraints,
args.python,
args.settings,
cli.no_config,
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,7 @@ pub(crate) struct BuildSettings {
pub(crate) out_dir: Option<PathBuf>,
pub(crate) sdist: bool,
pub(crate) wheel: bool,
pub(crate) build_constraint: Vec<PathBuf>,
pub(crate) python: Option<String>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverSettings,
Expand All @@ -1643,6 +1644,7 @@ impl BuildSettings {
package,
sdist,
wheel,
build_constraint,
python,
build,
refresh,
Expand All @@ -1655,6 +1657,10 @@ impl BuildSettings {
out_dir,
sdist,
wheel,
build_constraint: build_constraint
.into_iter()
.filter_map(Maybe::into_option)
.collect(),
python,
refresh: Refresh::from(refresh),
settings: ResolverSettings::combine(resolver_options(resolver, build), filesystem),
Expand Down
60 changes: 60 additions & 0 deletions crates/uv/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,3 +1146,63 @@ fn workspace() -> Result<()> {

Ok(())
}

#[test]
fn build_constraints() -> Result<()> {
let context = TestContext::new("3.12");
let filters = context
.filters()
.into_iter()
.chain([
(r"exit code: 1", "exit status: 1"),
(r"bdist\.[^/\\\s]+-[^/\\\s]+", "bdist.linux-x86_64"),
(r"\\\.", ""),
])
.collect::<Vec<_>>();

let project = context.temp_dir.child("project");

let constraints = project.child("constraints.txt");
constraints.write_str("setuptools==0.1.0")?;

let pyproject_toml = project.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio==3.7.0"]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#,
)?;

project.child("src").child("__init__.py").touch()?;
project.child("README").touch()?;

uv_snapshot!(&filters, context.build().arg("--build-constraint").arg("constraints.txt").current_dir(&project), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
Building source distribution...
error: Failed to install requirements from `build-system.requires` (resolve)
Caused by: No solution found when resolving: setuptools>=42
Caused by: Because you require setuptools>=42 and setuptools==0.1.0, we can conclude that your requirements are unsatisfiable.
"###);

project
.child("dist")
.child("project-0.1.0.tar.gz")
.assert(predicate::path::missing());
project
.child("dist")
.child("project-0.1.0-py3-none-any.whl")
.assert(predicate::path::missing());

Ok(())
}
5 changes: 5 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -6225,6 +6225,11 @@ uv build [OPTIONS] [SRC]
<p>WARNING: Hosts included in this list will not be verified against the system&#8217;s certificate store. Only use <code>--allow-insecure-host</code> in a secure network with verified sources, as it bypasses SSL verification and could expose you to MITM attacks.</p>

<p>May also be set with the <code>UV_INSECURE_HOST</code> environment variable.</p>
</dd><dt><code>--build-constraint</code>, <code>-b</code> <i>build-constraint</i></dt><dd><p>Constrain build dependencies using the given requirements files when building distributions.</p>

<p>Constraints files are <code>requirements.txt</code>-like files that only control the <em>version</em> of a build dependency that&#8217;s installed. However, including a package in a constraints file will <em>not</em> trigger the inclusion of that package on its own.</p>

<p>May also be set with the <code>UV_BUILD_CONSTRAINT</code> environment variable.</p>
</dd><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>

<p>Defaults to <code>$HOME/Library/Caches/uv</code> on macOS, <code>$XDG_CACHE_HOME/uv</code> or <code>$HOME/.cache/uv</code> on Linux, and <code>%LOCALAPPDATA%\uv\cache</code> on Windows.</p>
Expand Down

0 comments on commit 92cb2c3

Please sign in to comment.