Use `--with-requirements` in `uvx` error hint (#8112)

## Summary

Closes https://github.com/astral-sh/uv/issues/6845.
This commit is contained in:
Charlie Marsh 2024-10-11 00:09:33 +02:00 committed by GitHub
parent 12a76690b2
commit 0627b4a8a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 4 deletions

View File

@ -80,7 +80,8 @@ impl RequirementsSource {
Self::RequirementsTxt(path)
}
/// Parse a [`RequirementsSource`] from a user-provided string, assumed to be a package.
/// Parse a [`RequirementsSource`] from a user-provided string, assumed to be a positional
/// package (e.g., `uv pip install flask`).
///
/// If the user provided a value that appears to be a `requirements.txt` file or a local
/// directory, prompt them to correct it (if the terminal is interactive).
@ -121,6 +122,48 @@ impl RequirementsSource {
Self::Package(name)
}
/// Parse a [`RequirementsSource`] from a user-provided string, assumed to be a `--with`
/// package (e.g., `uvx --with flask ruff`).
///
/// If the user provided a value that appears to be a `requirements.txt` file or a local
/// directory, prompt them to correct it (if the terminal is interactive).
pub fn from_with_package(name: String) -> Self {
// If the user provided a `requirements.txt` file without `--with-requirements` (as in
// `uvx --with requirements.txt ruff`), prompt them to correct it.
#[allow(clippy::case_sensitive_file_extension_comparisons)]
if (name.ends_with(".txt") || name.ends_with(".in")) && Path::new(&name).is_file() {
let term = Term::stderr();
if term.is_term() {
let prompt = format!(
"`{name}` looks like a local requirements file but was passed as a package name. Did you mean `--with-requirements {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true).unwrap();
if confirmation {
return Self::from_requirements_file(name.into());
}
}
}
// Similarly, if the user provided a `pyproject.toml` file without `--with-requirements` (as in
// `uvx --with pyproject.toml ruff`), prompt them to correct it.
if (name == "pyproject.toml" || name == "setup.py" || name == "setup.cfg")
&& Path::new(&name).is_file()
{
let term = Term::stderr();
if term.is_term() {
let prompt = format!(
"`{name}` looks like a local metadata file but was passed as a package name. Did you mean `--with-requirements {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true).unwrap();
if confirmation {
return Self::from_requirements_file(name.into());
}
}
}
Self::Package(name)
}
/// Parse a [`RequirementsSource`] from a user-provided string, assumed to be a path to a source
/// tree.
pub fn from_source_tree(path: PathBuf) -> Self {

View File

@ -917,7 +917,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
let requirements = args
.with
.into_iter()
.map(RequirementsSource::from_package)
.map(RequirementsSource::from_with_package)
.chain(
args.with_editable
.into_iter()
@ -966,7 +966,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
let requirements = args
.with
.into_iter()
.map(RequirementsSource::from_package)
.map(RequirementsSource::from_with_package)
.chain(
args.with_requirements
.into_iter()
@ -1302,7 +1302,7 @@ async fn run_project(
let requirements = args
.with
.into_iter()
.map(RequirementsSource::from_package)
.map(RequirementsSource::from_with_package)
.chain(
args.with_editable
.into_iter()