From a7b251f65e6d24855185c49bcbf64fc8bdfd7861 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 23 Mar 2024 09:33:29 -0400 Subject: [PATCH] Limit overrides and constraints to `requirements.txt` format (#2632) ## Summary I don't see a great reason to allow this, and it adds a lot of complexity, so `pyproject.toml` files are now limited to `pip compile` and `pip install -r` -- they can't be passed as `-c` or `--override`. --- crates/uv-requirements/src/sources.rs | 26 ++++++++++++++++++++++++-- crates/uv/src/main.rs | 16 ++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/crates/uv-requirements/src/sources.rs b/crates/uv-requirements/src/sources.rs index 55f6af1e5f97..16398bda6cca 100644 --- a/crates/uv-requirements/src/sources.rs +++ b/crates/uv-requirements/src/sources.rs @@ -4,6 +4,7 @@ use console::Term; use uv_fs::Simplified; use uv_normalize::ExtraName; +use uv_warnings::warn_user; use crate::confirm; @@ -20,8 +21,9 @@ pub enum RequirementsSource { } impl RequirementsSource { - /// Parse a [`RequirementsSource`] from a [`PathBuf`]. - pub fn from_path(path: PathBuf) -> Self { + /// Parse a [`RequirementsSource`] from a [`PathBuf`]. The file type is determined by the file + /// extension. + pub fn from_requirements_file(path: PathBuf) -> Self { if path.ends_with("pyproject.toml") { Self::PyprojectToml(path) } else { @@ -29,6 +31,26 @@ impl RequirementsSource { } } + /// Parse a [`RequirementsSource`] from a constraints file. + pub fn from_constraints_file(path: PathBuf) -> Self { + if path.ends_with("pyproject.toml") { + warn_user!( + "The file `{}` appears to be a `pyproject.toml` file, but constraints must be specified in `requirements.txt` format.", path.user_display() + ); + } + Self::RequirementsTxt(path) + } + + /// Parse a [`RequirementsSource`] from an overrides file. + pub fn from_overrides_file(path: PathBuf) -> Self { + if path.ends_with("pyproject.toml") { + warn_user!( + "The file `{}` appears to be a `pyproject.toml` file, but overrides must be specified in `requirements.txt` format.", path.user_display() + ); + } + Self::RequirementsTxt(path) + } + /// Parse a [`RequirementsSource`] from a user-provided string, assumed to be a package. /// /// If the user provided a value that appears to be a `requirements.txt` file or a local diff --git a/crates/uv/src/main.rs b/crates/uv/src/main.rs index 70aaa5b10134..43d4da1242d3 100644 --- a/crates/uv/src/main.rs +++ b/crates/uv/src/main.rs @@ -1464,17 +1464,17 @@ async fn run() -> Result { let requirements = args .src_file .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_requirements_file) .collect::>(); let constraints = args .constraint .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_constraints_file) .collect::>(); let overrides = args .r#override .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_overrides_file) .collect::>(); let index_urls = IndexLocations::new( args.index_url.and_then(Maybe::into_option), @@ -1567,7 +1567,7 @@ async fn run() -> Result { let sources = args .src_file .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_requirements_file) .collect::>(); let reinstall = Reinstall::from_args(args.reinstall, args.reinstall_package); let no_binary = NoBinary::from_args(args.no_binary); @@ -1618,18 +1618,18 @@ async fn run() -> Result { .chain( args.requirement .into_iter() - .map(RequirementsSource::from_path), + .map(RequirementsSource::from_requirements_file), ) .collect::>(); let constraints = args .constraint .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_constraints_file) .collect::>(); let overrides = args .r#override .into_iter() - .map(RequirementsSource::from_path) + .map(RequirementsSource::from_overrides_file) .collect::>(); let index_urls = IndexLocations::new( args.index_url.and_then(Maybe::into_option), @@ -1714,7 +1714,7 @@ async fn run() -> Result { .chain( args.requirement .into_iter() - .map(RequirementsSource::from_path), + .map(RequirementsSource::from_requirements_file), ) .collect::>(); commands::pip_uninstall(