Skip to content

Commit 8011778

Browse files
authored
Simplify editable requirement errors and hints (#21130)
## Summary This PR does three things: * It fixes an issue where we were printing raw URLs in errors which could themselves contain credentials. * It cleans up the error messages for end users, into a form which in my opinion is cleaner and clearer. * It makes the underlying code simpler. ~~Which I need as a preparatory change for #20631.~~ (turns out I might not need it in the end) ## Test Plan Snapshots adjusted, and a test-case for the issue has been added.
1 parent dca33f5 commit 8011778

9 files changed

Lines changed: 144 additions & 125 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/uv-pypi-types/src/parsed_url.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ pub enum ParsedUrlError {
4040
MissingExtensionPath(PathBuf, ExtensionError),
4141
}
4242

43+
/// An error converting a parsed URL into an editable source.
44+
#[derive(Debug, Error)]
45+
pub enum MakeEditableError {
46+
#[error("Local archives cannot be editable")]
47+
LocalArchive,
48+
49+
#[error("Remote archives cannot be editable")]
50+
RemoteArchive,
51+
52+
#[error("Git sources cannot be editable")]
53+
Git,
54+
}
55+
4356
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
4457
pub struct VerbatimParsedUrl {
4558
pub parsed_url: ParsedUrl,
@@ -57,6 +70,15 @@ impl VerbatimParsedUrl {
5770
pub fn is_editable(&self) -> bool {
5871
self.parsed_url.is_editable()
5972
}
73+
74+
/// Make the URL an editable source.
75+
///
76+
/// # Errors
77+
///
78+
/// Returns [`MakeEditableError`] if the URL does not refer to a local directory.
79+
pub fn make_editable(&mut self) -> Result<(), MakeEditableError> {
80+
self.parsed_url.make_editable()
81+
}
6082
}
6183

6284
impl Pep508Url for VerbatimParsedUrl {
@@ -212,6 +234,23 @@ impl ParsedUrl {
212234
})
213235
)
214236
}
237+
238+
/// Make the URL an editable source.
239+
///
240+
/// # Errors
241+
///
242+
/// Returns [`MakeEditableError`] if the URL does not refer to a local directory.
243+
fn make_editable(&mut self) -> Result<(), MakeEditableError> {
244+
match self {
245+
Self::Directory(directory) => {
246+
directory.editable = Some(true);
247+
Ok(())
248+
}
249+
Self::Path(_) => Err(MakeEditableError::LocalArchive),
250+
Self::Archive(_) => Err(MakeEditableError::RemoteArchive),
251+
Self::GitDirectory(_) | Self::GitPath(_) => Err(MakeEditableError::Git),
252+
}
253+
}
215254
}
216255

217256
/// A local path URL for a file (i.e., a built or source distribution).

‎crates/uv-requirements-txt/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ workspace = true
1919
uv-client = { workspace = true }
2020
uv-configuration = { workspace = true }
2121
uv-distribution-types = { workspace = true }
22+
uv-errors = { workspace = true }
2223
uv-fs = { workspace = true }
2324
uv-normalize = { workspace = true }
2425
uv-pep508 = { workspace = true }

‎crates/uv-requirements-txt/src/lib.rs‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ use uv_pypi_types::VerbatimParsedUrl;
5959
use uv_redacted::DisplaySafeUrl;
6060
use uv_redacted::DisplaySafeUrlError;
6161

62-
use crate::requirement::EditableError;
63-
pub use crate::requirement::RequirementsTxtRequirement;
62+
pub use crate::requirement::{MakeEditableError, RequirementsTxtRequirement};
6463
use crate::shquote::unquote;
6564

6665
mod requirement;
@@ -745,16 +744,17 @@ fn parse_entry(
745744
Some(requirements_txt)
746745
};
747746

748-
let (requirement, hashes) =
747+
let (mut requirement, hashes) =
749748
parse_requirement_and_hashes(s, content, source, working_dir, true)?;
750-
let requirement =
751-
requirement
752-
.into_editable()
753-
.map_err(|err| RequirementsTxtParserError::NonEditable {
754-
source: err,
755-
start,
756-
end: s.cursor(),
757-
})?;
749+
requirement
750+
.make_editable()
751+
.map_err(|source| RequirementsTxtParserError::NonEditable {
752+
source,
753+
requirement: requirement.to_string(),
754+
start,
755+
end: s.cursor(),
756+
line: calculate_row_column(content, start).0,
757+
})?;
758758
RequirementsTxtStatement::EditableRequirementEntry(RequirementEntry {
759759
requirement,
760760
hashes,
@@ -1170,9 +1170,11 @@ pub enum RequirementsTxtParserError {
11701170
UnsupportedUrl(String),
11711171
MissingRequirementPrefix(String),
11721172
NonEditable {
1173-
source: EditableError,
1173+
source: MakeEditableError,
1174+
requirement: String,
11741175
start: usize,
11751176
end: usize,
1177+
line: usize,
11761178
},
11771179
NoBinary {
11781180
source: uv_normalize::InvalidNameError,
@@ -1245,8 +1247,13 @@ impl Display for RequirementsTxtParserError {
12451247
Self::UnsupportedUrl(url) => {
12461248
write!(f, "Unsupported URL (expected a `file://` scheme): `{url}`")
12471249
}
1248-
Self::NonEditable { .. } => {
1249-
write!(f, "Unsupported editable requirement")
1250+
Self::NonEditable {
1251+
requirement, line, ..
1252+
} => {
1253+
write!(
1254+
f,
1255+
"Unsupported editable requirement at line {line}: `{requirement}`"
1256+
)
12501257
}
12511258
Self::MissingRequirementPrefix(given) => {
12521259
write!(
@@ -1382,10 +1389,12 @@ impl Display for RequirementsTxtFileError {
13821389
self.file.user_display(),
13831390
)
13841391
}
1385-
RequirementsTxtParserError::NonEditable { .. } => {
1392+
RequirementsTxtParserError::NonEditable {
1393+
requirement, line, ..
1394+
} => {
13861395
write!(
13871396
f,
1388-
"Unsupported editable requirement in `{}`",
1397+
"Unsupported editable requirement in `{}` at line {line}: `{requirement}`",
13891398
self.file.user_display(),
13901399
)
13911400
}
@@ -1898,8 +1907,8 @@ mod test {
18981907
filters => filters
18991908
}, {
19001909
insta::assert_snapshot!(errors, @"
1901-
Unsupported editable requirement in `<REQUIREMENTS_TXT>`
1902-
Editable must refer to a local directory, not an HTTPS URL: `https://files.pythonhosted.org/packages/f7/69/96766da2cdb5605e6a31ef2734aff0be17901cefb385b885c2ab88896d76/ruff-0.5.6.tar.gz`
1910+
Unsupported editable requirement in `<REQUIREMENTS_TXT>` at line 1: `https://files.pythonhosted.org/packages/f7/69/96766da2cdb5605e6a31ef2734aff0be17901cefb385b885c2ab88896d76/ruff-0.5.6.tar.gz`
1911+
Remote archives cannot be editable
19031912
");
19041913
});
19051914

‎crates/uv-requirements-txt/src/requirement.rs‎

Lines changed: 24 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
use std::fmt::Display;
22
use std::path::Path;
33

4-
use uv_normalize::PackageName;
4+
use uv_errors::{Hint, Hints};
55
use uv_pep508::{
66
Pep508Error, Pep508ErrorSource, RequirementOrigin, TracingReporter, UnnamedRequirement,
7+
VersionOrUrl,
78
};
8-
use uv_pypi_types::{ParsedDirectoryUrl, ParsedUrl, VerbatimParsedUrl};
9+
use uv_pypi_types::VerbatimParsedUrl;
910

1011
#[derive(Debug, thiserror::Error)]
11-
pub enum EditableError {
12-
#[error("Editable `{0}` must refer to a local directory")]
13-
MissingVersion(PackageName),
12+
pub enum MakeEditableError {
13+
#[error("Registry requirements cannot be editable")]
14+
Registry,
1415

15-
#[error("Editable `{0}` must refer to a local directory, not a versioned package")]
16-
Versioned(PackageName),
17-
18-
#[error("Editable `{0}` must refer to a local directory, not an archive: `{1}`")]
19-
File(PackageName, String),
20-
21-
#[error("Editable `{0}` must refer to a local directory, not an HTTPS URL: `{1}`")]
22-
Https(PackageName, String),
23-
24-
#[error("Editable `{0}` must refer to a local directory, not a Git URL: `{1}`")]
25-
Git(PackageName, String),
26-
27-
#[error("Editable must refer to a local directory, not an archive: `{0}`")]
28-
UnnamedFile(String),
29-
30-
#[error("Editable must refer to a local directory, not an HTTPS URL: `{0}`")]
31-
UnnamedHttps(String),
16+
#[error(transparent)]
17+
Url(#[from] uv_pypi_types::MakeEditableError),
18+
}
3219

33-
#[error("Editable must refer to a local directory, not a Git URL: `{0}`")]
34-
UnnamedGit(String),
20+
impl Hint for MakeEditableError {
21+
fn hints(&self) -> Hints<'_> {
22+
Hints::from("Editable requirements must refer to a local directory")
23+
}
3524
}
3625

3726
/// A requirement specifier in a `requirements.txt` file.
@@ -57,79 +46,24 @@ impl RequirementsTxtRequirement {
5746
}
5847
}
5948

60-
/// Convert the [`RequirementsTxtRequirement`] into an editable requirement.
49+
/// Make the [`RequirementsTxtRequirement`] editable in place.
6150
///
6251
/// # Errors
6352
///
64-
/// Returns [`EditableError`] if the requirement cannot be interpreted as editable.
65-
/// Specifically, only local directory URLs are supported.
66-
pub fn into_editable(self) -> Result<Self, EditableError> {
67-
match self {
53+
/// Returns [`MakeEditableError`] if the requirement does not refer to a local directory.
54+
pub fn make_editable(&mut self) -> Result<(), MakeEditableError> {
55+
let url = match self {
6856
Self::Named(requirement) => {
69-
let Some(version_or_url) = requirement.version_or_url else {
70-
return Err(EditableError::MissingVersion(requirement.name));
57+
let Some(VersionOrUrl::Url(url)) = requirement.version_or_url.as_mut() else {
58+
return Err(MakeEditableError::Registry);
7159
};
72-
73-
let uv_pep508::VersionOrUrl::Url(url) = version_or_url else {
74-
return Err(EditableError::Versioned(requirement.name));
75-
};
76-
77-
let parsed_url = match url.parsed_url {
78-
ParsedUrl::Directory(parsed_url) => parsed_url,
79-
ParsedUrl::Path(_) => {
80-
return Err(EditableError::File(requirement.name, url.to_string()));
81-
}
82-
ParsedUrl::Archive(_) => {
83-
return Err(EditableError::Https(requirement.name, url.to_string()));
84-
}
85-
ParsedUrl::GitDirectory(_) => {
86-
return Err(EditableError::Git(requirement.name, url.to_string()));
87-
}
88-
ParsedUrl::GitPath(_) => {
89-
return Err(EditableError::Git(requirement.name, url.to_string()));
90-
}
91-
};
92-
93-
Ok(Self::Named(uv_pep508::Requirement {
94-
version_or_url: Some(uv_pep508::VersionOrUrl::Url(VerbatimParsedUrl {
95-
verbatim: url.verbatim,
96-
parsed_url: ParsedUrl::Directory(ParsedDirectoryUrl {
97-
editable: Some(true),
98-
..parsed_url
99-
}),
100-
})),
101-
..requirement
102-
}))
60+
url
10361
}
104-
Self::Unnamed(requirement) => {
105-
let parsed_url = match requirement.url.parsed_url {
106-
ParsedUrl::Directory(parsed_url) => parsed_url,
107-
ParsedUrl::Path(_) => {
108-
return Err(EditableError::UnnamedFile(requirement.to_string()));
109-
}
110-
ParsedUrl::Archive(_) => {
111-
return Err(EditableError::UnnamedHttps(requirement.to_string()));
112-
}
113-
ParsedUrl::GitDirectory(_) => {
114-
return Err(EditableError::UnnamedGit(requirement.to_string()));
115-
}
116-
ParsedUrl::GitPath(_) => {
117-
return Err(EditableError::UnnamedGit(requirement.to_string()));
118-
}
119-
};
62+
Self::Unnamed(requirement) => &mut requirement.url,
63+
};
12064

121-
Ok(Self::Unnamed(UnnamedRequirement {
122-
url: VerbatimParsedUrl {
123-
verbatim: requirement.url.verbatim,
124-
parsed_url: ParsedUrl::Directory(ParsedDirectoryUrl {
125-
editable: Some(true),
126-
..parsed_url
127-
}),
128-
},
129-
..requirement
130-
}))
131-
}
132-
}
65+
url.make_editable()?;
66+
Ok(())
13367
}
13468

13569
/// Parse a requirement as seen in a `requirements.txt` file.

‎crates/uv-requirements/src/specification.rs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,16 @@ impl RequirementsSpecification {
261261
)],
262262
..Self::default()
263263
},
264-
RequirementsSource::Editable(requirement) => Self {
265-
requirements: vec![UnresolvedRequirementSpecification::from(
266-
requirement.clone().into_editable()?,
267-
)],
268-
..Self::default()
269-
},
264+
RequirementsSource::Editable(requirement) => {
265+
let mut requirement = requirement.clone();
266+
requirement.make_editable().with_context(|| {
267+
format!("Unsupported editable requirement: `{requirement}`")
268+
})?;
269+
Self {
270+
requirements: vec![UnresolvedRequirementSpecification::from(requirement)],
271+
..Self::default()
272+
}
273+
}
270274
RequirementsSource::RequirementsTxt(path) => {
271275
if !(path.starts_with("http://") || path.starts_with("https://") || path.exists()) {
272276
return Err(anyhow::anyhow!("File not found: `{}`", path.user_display()));

‎crates/uv/src/commands/diagnostics.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub(crate) fn hints_for_error(err: &anyhow::Error) -> Hints<'static> {
252252
collect_hint::<uv_distribution::Error>(cause, &mut hints);
253253
collect_hint::<uv_python::BrokenLink>(cause, &mut hints);
254254
collect_hint::<uv_resolver::PylockTomlError>(cause, &mut hints);
255+
collect_hint::<uv_requirements_txt::MakeEditableError>(cause, &mut hints);
255256
collect_hint::<uv_python::InterpreterError>(cause, &mut hints);
256257
collect_hint::<uv_workspace::pyproject::SourceError>(cause, &mut hints);
257258
collect_hint::<uv_distribution::LoweringError>(cause, &mut hints);

‎crates/uv/tests/pip_compile/pip_compile.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6120,8 +6120,10 @@ fn missing_editable_file() -> Result<()> {
61206120
.arg("requirements.in"), @"
61216121
exit_code: 2 (failure)
61226122
----- stderr -----
6123-
error: Unsupported editable requirement in `requirements.in`
6124-
Caused by: Editable must refer to a local directory, not an archive: `file://[TEMP_DIR]/foo/anyio-3.7.0.tar.gz`
6123+
error: Unsupported editable requirement in `requirements.in` at line 1: `file://[TEMP_DIR]/foo/anyio-3.7.0.tar.gz`
6124+
Caused by: Local archives cannot be editable
6125+
6126+
hint: Editable requirements must refer to a local directory
61256127
");
61266128

61276129
Ok(())

0 commit comments

Comments
 (0)