1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
use std::collections::BTreeMap;
use std::io;
use std::path::{Path, PathBuf};
use either::Either;
use owo_colors::OwoColorize;
use thiserror::Error;
use uv_auth::CredentialsCache;
use uv_distribution_filename::DistExtension;
use uv_distribution_types::{
Index, IndexLocations, IndexMetadata, IndexName, Origin, Requirement, RequirementSource,
};
use uv_fs::normalize_path;
use uv_git_types::{GitLfs, GitReference, GitUrl, GitUrlParseError};
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pep440::VersionSpecifiers;
use uv_pep508::{MarkerTree, VerbatimUrl, VersionOrUrl, looks_like_git_repository};
use uv_pypi_types::{ConflictItem, ParsedGitUrl, ParsedUrlError, VerbatimParsedUrl};
use uv_redacted::{DisplaySafeUrl, DisplaySafeUrlError};
use uv_workspace::Workspace;
use uv_workspace::pyproject::{PyProjectToml, Source, Sources};
use crate::metadata::GitWorkspaceMember;
#[derive(Debug, Clone)]
pub struct LoweredRequirement(Requirement);
#[derive(Debug, Clone, Copy)]
enum RequirementOrigin {
/// The `tool.uv.sources` were read from the project.
Project,
/// The `tool.uv.sources` were read from the workspace root.
Workspace,
}
impl LoweredRequirement {
/// Combine `project.dependencies` or `project.optional-dependencies` with `tool.uv.sources`.
pub(crate) fn from_requirement<'data>(
requirement: uv_pep508::Requirement<VerbatimParsedUrl>,
project_name: Option<&'data PackageName>,
project_dir: &'data Path,
project_sources: &'data BTreeMap<PackageName, Sources>,
project_indexes: &'data [Index],
extra: Option<&ExtraName>,
group: Option<&GroupName>,
locations: &'data IndexLocations,
workspace: &'data Workspace,
git_member: Option<&'data GitWorkspaceMember<'data>>,
editable: bool,
credentials_cache: &'data CredentialsCache,
) -> impl Iterator<Item = Result<Self, LoweringError>> + use<'data> + 'data {
// Identify the source from the `tool.uv.sources` table.
let (sources, origin) = if let Some(source) = project_sources.get(&requirement.name) {
(Some(source), RequirementOrigin::Project)
} else if let Some(source) = workspace.sources().get(&requirement.name) {
(Some(source), RequirementOrigin::Workspace)
} else {
(None, RequirementOrigin::Project)
};
// If the source only applies to a given extra or dependency group, filter it out.
let sources = sources.map(|sources| {
sources
.iter()
.filter(|source| {
if let Some(target) = source.extra() {
if extra != Some(target) {
return false;
}
}
if let Some(target) = source.group() {
if group != Some(target) {
return false;
}
}
true
})
.cloned()
.collect::<Sources>()
});
// If you use a package that's part of the workspace...
if workspace.packages().contains_key(&requirement.name) {
// And it's not a recursive self-inclusion (extras that activate other extras), e.g.
// `framework[machine_learning]` depends on `framework[cuda]`.
if project_name.is_none_or(|project_name| *project_name != requirement.name) {
// It must be declared as a workspace source.
let Some(sources) = sources.as_ref() else {
// No sources were declared for the workspace package.
return Either::Left(std::iter::once(Err(
LoweringError::MissingWorkspaceSource(requirement.name.clone()),
)));
};
for source in sources.iter() {
match source {
Source::Git { .. } => {
return Either::Left(std::iter::once(Err(
LoweringError::NonWorkspaceSource(
requirement.name.clone(),
SourceKind::Git,
),
)));
}
Source::Url { .. } => {
return Either::Left(std::iter::once(Err(
LoweringError::NonWorkspaceSource(
requirement.name.clone(),
SourceKind::Url,
),
)));
}
Source::Path { .. } => {
return Either::Left(std::iter::once(Err(
LoweringError::NonWorkspaceSource(
requirement.name.clone(),
SourceKind::Path,
),
)));
}
Source::Registry { .. } => {
return Either::Left(std::iter::once(Err(
LoweringError::NonWorkspaceSource(
requirement.name.clone(),
SourceKind::Registry,
),
)));
}
Source::Workspace { .. } => {
// OK
}
}
}
}
}
let Some(sources) = sources else {
return Either::Left(std::iter::once(Ok(Self(Requirement::from(requirement)))));
};
// Determine whether the markers cover the full space for the requirement. If not, fill the
// remaining space with the negation of the sources.
let remaining = {
// Determine the space covered by the sources.
let mut total = MarkerTree::FALSE;
for source in sources.iter() {
total.or(source.marker());
}
// Determine the space covered by the requirement.
let mut remaining = total.negate();
remaining.and(requirement.marker);
Self(Requirement {
marker: remaining,
..Requirement::from(requirement.clone())
})
};
Either::Right(
sources
.into_iter()
.map(move |source| {
let (source, mut marker) = match source {
Source::Git {
git,
subdirectory,
rev,
tag,
branch,
lfs,
marker,
..
} => {
let source = git_source(
&git,
subdirectory.map(Box::<Path>::from),
rev,
tag,
branch,
lfs,
)?;
(source, marker)
}
Source::Url {
url,
subdirectory,
marker,
..
} => {
let source =
url_source(&requirement, url, subdirectory.map(Box::<Path>::from))?;
(source, marker)
}
Source::Path {
path,
editable,
package,
marker,
..
} => {
let source = path_source(
path,
git_member,
origin,
project_dir,
workspace.install_path(),
editable,
package,
)?;
(source, marker)
}
Source::Registry {
index,
marker,
extra,
group,
} => {
// Identify the named index from either the project indexes or the workspace indexes,
// in that order.
let Some(index) = locations
.indexes()
.filter(|index| matches!(index.origin, Some(Origin::Cli)))
.chain(project_indexes.iter())
.chain(workspace.indexes().iter())
.find(|Index { name, .. }| {
name.as_ref().is_some_and(|name| *name == index)
})
else {
let hint = missing_index_hint(locations, &index);
return Err(LoweringError::MissingIndex {
package: requirement.name.clone(),
index,
hint,
});
};
if let Some(credentials) = index.credentials() {
credentials_cache.store_credentials(index.raw_url(), credentials);
}
let index = IndexMetadata {
url: index.url.clone(),
format: index.format,
};
let conflict = project_name.and_then(|project_name| {
if let Some(extra) = extra {
Some(ConflictItem::from((project_name.clone(), extra)))
} else {
group.map(|group| {
ConflictItem::from((project_name.clone(), group))
})
}
});
let source = registry_source(&requirement, index, conflict);
(source, marker)
}
Source::Workspace {
workspace: is_workspace,
marker,
..
} => {
if !is_workspace {
return Err(LoweringError::WorkspaceFalse);
}
let member = workspace
.packages()
.get(&requirement.name)
.ok_or_else(|| {
LoweringError::UndeclaredWorkspacePackage(
requirement.name.clone(),
)
})?
.clone();
// Say we have:
// ```
// root
// ├── main_workspace <- We want to the path from here ...
// │ ├── pyproject.toml
// │ └── uv.lock
// └──current_workspace
// └── packages
// └── current_package <- ... to here.
// └── pyproject.toml
// ```
// The path we need in the lockfile: `../current_workspace/packages/current_project`
// member root: `/root/current_workspace/packages/current_project`
// workspace install root: `/root/current_workspace`
// relative to workspace: `packages/current_project`
// workspace lock root: `../current_workspace`
// relative to main workspace: `../current_workspace/packages/current_project`
let url = VerbatimUrl::from_absolute_path(member.root())?;
let install_path = url.to_file_path().map_err(|()| {
LoweringError::RelativeTo(io::Error::other(
"Invalid path in file URL",
))
})?;
let source = if let Some(git_member) = &git_member {
// If the workspace comes from a Git dependency, all workspace
// members need to be Git dependencies, too.
let subdirectory =
uv_fs::relative_to(member.root(), git_member.fetch_root)
.expect("Workspace member must be relative");
let subdirectory = normalize_path(subdirectory);
RequirementSource::Git {
git: git_member.git_source.git.clone(),
subdirectory: if subdirectory == PathBuf::new() {
None
} else {
Some(subdirectory.into_owned().into_boxed_path())
},
url,
}
} else {
let value = workspace.required_members().get(&requirement.name);
let is_required_member = value.is_some();
let editability = value.copied().flatten();
if member.pyproject_toml().is_package(!is_required_member) {
RequirementSource::Directory {
install_path: install_path.into_boxed_path(),
url,
editable: Some(editability.unwrap_or(editable)),
r#virtual: Some(false),
}
} else {
RequirementSource::Directory {
install_path: install_path.into_boxed_path(),
url,
editable: Some(false),
r#virtual: Some(true),
}
}
};
(source, marker)
}
};
marker.and(requirement.marker);
Ok(Self(Requirement {
name: requirement.name.clone(),
extras: requirement.extras.clone(),
groups: Box::new([]),
marker,
source,
origin: requirement.origin.clone(),
}))
})
.chain(std::iter::once(Ok(remaining)))
.filter(|requirement| match requirement {
Ok(requirement) => !requirement.0.marker.is_false(),
Err(_) => true,
}),
)
}
/// Lower a [`uv_pep508::Requirement`] in a non-workspace setting (for example, in a PEP 723
/// script, which runs in an isolated context).
pub fn from_non_workspace_requirement<'data>(
requirement: uv_pep508::Requirement<VerbatimParsedUrl>,
dir: &'data Path,
sources: &'data BTreeMap<PackageName, Sources>,
indexes: &'data [Index],
locations: &'data IndexLocations,
credentials_cache: &'data CredentialsCache,
) -> impl Iterator<Item = Result<Self, LoweringError>> + 'data {
let source = sources.get(&requirement.name).cloned();
let Some(source) = source else {
return Either::Left(std::iter::once(Ok(Self(Requirement::from(requirement)))));
};
// If the source only applies to a given extra, filter it out.
let source = source
.iter()
.filter(|source| {
source.extra().is_none_or(|target| {
requirement
.marker
.top_level_extra_name()
.is_some_and(|extra| &*extra == target)
})
})
.cloned()
.collect::<Sources>();
// Determine whether the markers cover the full space for the requirement. If not, fill the
// remaining space with the negation of the sources.
let remaining = {
// Determine the space covered by the sources.
let mut total = MarkerTree::FALSE;
for source in source.iter() {
total.or(source.marker());
}
// Determine the space covered by the requirement.
let mut remaining = total.negate();
remaining.and(requirement.marker);
Self(Requirement {
marker: remaining,
..Requirement::from(requirement.clone())
})
};
Either::Right(
source
.into_iter()
.map(move |source| {
let (source, mut marker) = match source {
Source::Git {
git,
subdirectory,
rev,
tag,
branch,
lfs,
marker,
..
} => {
let source = git_source(
&git,
subdirectory.map(Box::<Path>::from),
rev,
tag,
branch,
lfs,
)?;
(source, marker)
}
Source::Url {
url,
subdirectory,
marker,
..
} => {
let source =
url_source(&requirement, url, subdirectory.map(Box::<Path>::from))?;
(source, marker)
}
Source::Path {
path,
editable,
package,
marker,
..
} => {
let source = path_source(
path,
None,
RequirementOrigin::Project,
dir,
dir,
editable,
package,
)?;
(source, marker)
}
Source::Registry { index, marker, .. } => {
let Some(index) = locations
.indexes()
.filter(|index| matches!(index.origin, Some(Origin::Cli)))
.chain(indexes.iter())
.find(|Index { name, .. }| {
name.as_ref().is_some_and(|name| *name == index)
})
else {
let hint = missing_index_hint(locations, &index);
return Err(LoweringError::MissingIndex {
package: requirement.name.clone(),
index,
hint,
});
};
if let Some(credentials) = index.credentials() {
credentials_cache.store_credentials(index.raw_url(), credentials);
}
let index = IndexMetadata {
url: index.url.clone(),
format: index.format,
};
let conflict = None;
let source = registry_source(&requirement, index, conflict);
(source, marker)
}
Source::Workspace { .. } => {
return Err(LoweringError::WorkspaceMember);
}
};
marker.and(requirement.marker);
Ok(Self(Requirement {
name: requirement.name.clone(),
extras: requirement.extras.clone(),
groups: Box::new([]),
marker,
source,
origin: requirement.origin.clone(),
}))
})
.chain(std::iter::once(Ok(remaining)))
.filter(|requirement| match requirement {
Ok(requirement) => !requirement.0.marker.is_false(),
Err(_) => true,
}),
)
}
/// Convert back into a [`Requirement`].
pub fn into_inner(self) -> Requirement {
self.0
}
}
/// An error parsing and merging `tool.uv.sources` with
/// `project.{dependencies,optional-dependencies}`.
#[derive(Debug, Error)]
pub enum LoweringError {
#[error(
"`{0}` is included as a workspace member, but is missing an entry in `tool.uv.sources` (e.g., `{0} = {{ workspace = true }}`)"
)]
MissingWorkspaceSource(PackageName),
#[error(
"`{0}` is included as a workspace member, but references a {1} in `tool.uv.sources`. Workspace members must be declared as workspace sources (e.g., `{0} = {{ workspace = true }}`)."
)]
NonWorkspaceSource(PackageName, SourceKind),
#[error(
"`{0}` references a workspace in `tool.uv.sources` (e.g., `{0} = {{ workspace = true }}`), but is not a workspace member"
)]
UndeclaredWorkspacePackage(PackageName),
#[error("Can only specify one of: `rev`, `tag`, or `branch`")]
MoreThanOneGitRef,
#[error(transparent)]
GitUrlParse(#[from] GitUrlParseError),
#[error("Package `{package}` references an undeclared index: `{index}`{}", if let Some(hint) = hint { format!("\n\n{}{} {hint}", "hint".bold().cyan(), ":".bold()) } else { String::new() })]
MissingIndex {
package: PackageName,
index: IndexName,
hint: Option<String>,
},
#[error("Workspace members are not allowed in non-workspace contexts")]
WorkspaceMember,
#[error(transparent)]
InvalidUrl(#[from] DisplaySafeUrlError),
#[error(transparent)]
InvalidVerbatimUrl(#[from] uv_pep508::VerbatimUrlError),
#[error("Fragments are not allowed in URLs: `{0}`")]
ForbiddenFragment(DisplaySafeUrl),
#[error(
"`{0}` is associated with a URL source, but references a Git repository. Consider using a Git source instead (e.g., `{0} = {{ git = \"{1}\" }}`)"
)]
MissingGitSource(PackageName, DisplaySafeUrl),
#[error("`workspace = false` is not yet supported")]
WorkspaceFalse,
#[error("Source with `editable = true` must refer to a local directory, not a file: `{0}`")]
EditableFile(String),
#[error("Source with `package = true` must refer to a local directory, not a file: `{0}`")]
PackagedFile(String),
#[error(
"Git repository references local file source, but only directories are supported as transitive Git dependencies: `{0}`"
)]
GitFile(String),
#[error(transparent)]
ParsedUrl(#[from] ParsedUrlError),
#[error("Path must be UTF-8: `{0}`")]
NonUtf8Path(PathBuf),
#[error(transparent)] // Function attaches the context
RelativeTo(io::Error),
}
#[derive(Debug, Copy, Clone)]
pub enum SourceKind {
Path,
Url,
Git,
Registry,
}
impl std::fmt::Display for SourceKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Path => write!(f, "path"),
Self::Url => write!(f, "URL"),
Self::Git => write!(f, "Git"),
Self::Registry => write!(f, "registry"),
}
}
}
/// Generate a hint for a missing index if the index name is found in a configuration file
/// (e.g., `uv.toml`) rather than in the project's `pyproject.toml`.
fn missing_index_hint(locations: &IndexLocations, index: &IndexName) -> Option<String> {
let config_index = locations
.simple_indexes()
.filter(|idx| !matches!(idx.origin, Some(Origin::Cli)))
.find(|idx| idx.name.as_ref().is_some_and(|name| *name == *index));
config_index.and_then(|idx| {
let source = match idx.origin {
Some(Origin::User) => "a user-level `uv.toml`",
Some(Origin::System) => "a system-level `uv.toml`",
Some(Origin::Project) => "a project-level `uv.toml`",
Some(Origin::Cli | Origin::RequirementsTxt) | None => return None,
};
Some(format!(
"Index `{index}` was found in {source}, but indexes \
referenced via `tool.uv.sources` must be defined in the project's \
`pyproject.toml`"
))
})
}
/// Convert a Git source into a [`RequirementSource`].
fn git_source(
git: &DisplaySafeUrl,
subdirectory: Option<Box<Path>>,
rev: Option<String>,
tag: Option<String>,
branch: Option<String>,
lfs: Option<bool>,
) -> Result<RequirementSource, LoweringError> {
let reference = match (rev, tag, branch) {
(None, None, None) => GitReference::DefaultBranch,
(Some(rev), None, None) => GitReference::from_rev(rev),
(None, Some(tag), None) => GitReference::Tag(tag),
(None, None, Some(branch)) => GitReference::Branch(branch),
_ => return Err(LoweringError::MoreThanOneGitRef),
};
// Create a PEP 508-compatible URL.
let mut url = DisplaySafeUrl::parse(&format!("git+{git}"))?;
if let Some(rev) = reference.as_str() {
let path = format!("{}@{}", url.path(), rev);
url.set_path(&path);
}
let mut frags: Vec<String> = Vec::new();
if let Some(subdirectory) = subdirectory.as_ref() {
let subdirectory = subdirectory
.to_str()
.ok_or_else(|| LoweringError::NonUtf8Path(subdirectory.to_path_buf()))?;
frags.push(format!("subdirectory={subdirectory}"));
}
// Loads Git LFS Enablement according to priority.
// First: lfs = true, lfs = false from pyproject.toml
// Second: UV_GIT_LFS from environment
let lfs = GitLfs::from(lfs);
// Preserve that we're using Git LFS in the Verbatim Url representations
if lfs.enabled() {
frags.push("lfs=true".to_string());
}
if !frags.is_empty() {
url.set_fragment(Some(&frags.join("&")));
}
let url = VerbatimUrl::from_url(url);
let repository = git.clone();
Ok(RequirementSource::Git {
url,
git: GitUrl::from_fields(repository, reference, None, lfs)?,
subdirectory,
})
}
/// Convert a URL source into a [`RequirementSource`].
fn url_source(
requirement: &uv_pep508::Requirement<VerbatimParsedUrl>,
url: DisplaySafeUrl,
subdirectory: Option<Box<Path>>,
) -> Result<RequirementSource, LoweringError> {
let mut verbatim_url = url.clone();
if verbatim_url.fragment().is_some() {
return Err(LoweringError::ForbiddenFragment(url));
}
if let Some(subdirectory) = subdirectory.as_ref() {
let subdirectory = subdirectory
.to_str()
.ok_or_else(|| LoweringError::NonUtf8Path(subdirectory.to_path_buf()))?;
verbatim_url.set_fragment(Some(&format!("subdirectory={subdirectory}")));
}
let ext = match DistExtension::from_path(url.path()) {
Ok(ext) => ext,
Err(..) if looks_like_git_repository(&url) => {
return Err(LoweringError::MissingGitSource(
requirement.name.clone(),
url.clone(),
));
}
Err(err) => {
return Err(ParsedUrlError::MissingExtensionUrl(url.to_string(), err).into());
}
};
let verbatim_url = VerbatimUrl::from_url(verbatim_url);
Ok(RequirementSource::Url {
location: url,
subdirectory,
ext,
url: verbatim_url,
})
}
/// Convert a registry source into a [`RequirementSource`].
fn registry_source(
requirement: &uv_pep508::Requirement<VerbatimParsedUrl>,
index: IndexMetadata,
conflict: Option<ConflictItem>,
) -> RequirementSource {
match &requirement.version_or_url {
None => RequirementSource::Registry {
specifier: VersionSpecifiers::empty(),
index: Some(index),
conflict,
},
Some(VersionOrUrl::VersionSpecifier(version)) => RequirementSource::Registry {
specifier: version.clone(),
index: Some(index),
conflict,
},
Some(VersionOrUrl::Url(_)) => RequirementSource::Registry {
specifier: VersionSpecifiers::empty(),
index: Some(index),
conflict,
},
}
}
/// Convert a path string to a file or directory source.
fn path_source(
path: impl AsRef<Path>,
git_member: Option<&GitWorkspaceMember>,
origin: RequirementOrigin,
project_dir: &Path,
workspace_root: &Path,
editable: Option<bool>,
package: Option<bool>,
) -> Result<RequirementSource, LoweringError> {
let path = path.as_ref();
let base = match origin {
RequirementOrigin::Project => project_dir,
RequirementOrigin::Workspace => workspace_root,
};
let url = VerbatimUrl::from_path(path, base)?.with_given(path.to_string_lossy());
let install_path = url
.to_file_path()
.map_err(|()| LoweringError::RelativeTo(io::Error::other("Invalid path in file URL")))?;
let is_dir = if let Ok(metadata) = install_path.metadata() {
metadata.is_dir()
} else {
install_path.extension().is_none()
};
if is_dir {
if let Some(git_member) = git_member {
let git = git_member.git_source.git.clone();
let subdirectory = uv_fs::relative_to(install_path, git_member.fetch_root)
.expect("Workspace member must be relative");
let subdirectory = normalize_path(subdirectory);
let subdirectory = if subdirectory == PathBuf::new() {
None
} else {
Some(subdirectory.into_owned().into_boxed_path())
};
let url = DisplaySafeUrl::from(ParsedGitUrl {
url: git.clone(),
subdirectory: subdirectory.clone(),
});
return Ok(RequirementSource::Git {
git,
subdirectory,
url: VerbatimUrl::from_url(url),
});
}
if editable == Some(true) {
Ok(RequirementSource::Directory {
install_path: install_path.into_boxed_path(),
url,
editable,
r#virtual: Some(false),
})
} else {
// Determine whether the project is a package or virtual.
// If the `package` option is unset, check if `tool.uv.package` is set
// on the path source (otherwise, default to `true`).
let is_package = package.unwrap_or_else(|| {
let pyproject_path = install_path.join("pyproject.toml");
fs_err::read_to_string(&pyproject_path)
.ok()
.and_then(|contents| PyProjectToml::from_string(contents, pyproject_path).ok())
// We don't require a build system for path dependencies
.map(|pyproject_toml| pyproject_toml.is_package(false))
.unwrap_or(true)
});
// If the project is not a package, treat it as a virtual dependency.
let r#virtual = !is_package;
Ok(RequirementSource::Directory {
install_path: install_path.into_boxed_path(),
url,
editable: Some(false),
r#virtual: Some(r#virtual),
})
}
} else {
// TODO(charlie): If a Git repo contains a source that points to a file, what should we do?
if git_member.is_some() {
return Err(LoweringError::GitFile(url.to_string()));
}
if editable == Some(true) {
return Err(LoweringError::EditableFile(url.to_string()));
}
if package == Some(true) {
return Err(LoweringError::PackagedFile(url.to_string()));
}
Ok(RequirementSource::Path {
ext: DistExtension::from_path(&install_path)
.map_err(|err| ParsedUrlError::MissingExtensionPath(path.to_path_buf(), err))?,
install_path: install_path.into_boxed_path(),
url,
})
}
}