Skip to content

Commit b552e40

Browse files
committed
Refine --no-sources reinstall logic in do_sync function
- Updated the logic to selectively reinstall URL-based packages only when the resolution selects a registry distribution, preventing unnecessary reinstalls for packages specified via direct URL. - Enhanced debug logging for better clarity on package evaluation during the reinstall process. - Adjusted related test expectations to reflect the changes in package installation behavior. This change improves the efficiency of package management when source installations are disabled.
1 parent 539d3bd commit b552e40

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

crates/uv/src/commands/project/sync.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use uv_configuration::{
1919
use uv_dispatch::BuildDispatch;
2020
use uv_distribution::LoweredExtraBuildDependencies;
2121
use uv_distribution_types::{
22-
DirectorySourceDist, Dist, Index, Name, Requirement, Resolution, ResolvedDist, SourceDist,
22+
BuiltDist, DirectorySourceDist, Dist, Index, Name, Requirement, Resolution, ResolvedDist,
23+
SourceDist,
2324
};
2425
use uv_fs::{PortablePathBuf, Simplified};
2526
use uv_installer::SitePackages;
@@ -788,22 +789,50 @@ pub(super) async fn do_sync(
788789

789790
let site_packages = SitePackages::from_environment(venv)?;
790791

791-
// When --no-sources is used, force reinstall of URL-based packages
792-
// to ensure they are replaced with registry versions
792+
// When --no-sources is used, force reinstall only when the current install is editable/URL
793+
// but the resolution selects a registry distribution for that package. This avoids
794+
// unnecessary reinstalls for packages that are legitimately specified via direct URL.
793795
let reinstall = if matches!(sources, uv_configuration::SourceStrategy::Disabled) {
794-
tracing::debug!("Sources are disabled, checking for URL-based packages to reinstall");
795-
let mut updated_reinstall = reinstall.clone();
796+
tracing::debug!("Sources are disabled; evaluating packages to selectively reinstall");
797+
798+
// Collect package names which the resolution intends to install from a registry.
799+
let resolved_registry_names = {
800+
let mut names = std::collections::HashSet::new();
801+
for resolved in resolution.distributions() {
802+
if let ResolvedDist::Installable { dist, .. } = resolved {
803+
match dist.as_ref() {
804+
Dist::Built(BuiltDist::Registry(_))
805+
| Dist::Source(SourceDist::Registry(_)) => {
806+
names.insert(dist.name().clone());
807+
}
808+
_ => {}
809+
}
810+
}
811+
}
812+
names
813+
};
796814

815+
let mut updated_reinstall = reinstall.clone();
797816
for dist in site_packages.iter() {
798-
if matches!(dist, uv_distribution_types::InstalledDist::Url(_)) {
799-
tracing::debug!("Marking URL-based package for reinstall: {}", dist.name());
817+
let is_url_like = matches!(
818+
dist,
819+
uv_distribution_types::InstalledDist::Url(_)
820+
| uv_distribution_types::InstalledDist::LegacyEditable(_)
821+
);
822+
if is_url_like && resolved_registry_names.contains(dist.name()) {
823+
tracing::debug!(
824+
"Marking package for reinstall due to --no-sources registry preference: {}",
825+
dist.name()
826+
);
800827
updated_reinstall = match updated_reinstall {
801828
uv_configuration::Reinstall::None => {
802829
uv_configuration::Reinstall::Packages(vec![dist.name().clone()], Vec::new())
803830
}
804831
uv_configuration::Reinstall::All => updated_reinstall,
805832
uv_configuration::Reinstall::Packages(mut packages, paths) => {
806-
packages.push(dist.name().clone());
833+
if !packages.contains(dist.name()) {
834+
packages.push(dist.name().clone());
835+
}
807836
uv_configuration::Reinstall::Packages(packages, paths)
808837
}
809838
};

crates/uv/tests/it/sync.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12995,13 +12995,12 @@ fn sync_no_sources_editable_to_package_switch() -> Result<()> {
1299512995
1299612996
----- stderr -----
1299712997
Resolved 4 packages in [TIME]
12998-
Uninstalled 2 packages in [TIME]
12999-
Installed 4 packages in [TIME]
12998+
Uninstalled 1 package in [TIME]
12999+
Installed 3 packages in [TIME]
1300013000
- anyio==4.3.0 (from file://[TEMP_DIR]/local_dep)
1300113001
+ anyio==4.3.0
1300213002
+ idna==3.6
1300313003
+ sniffio==1.3.1
13004-
~ test-no-sources==0.0.1 (from file://[TEMP_DIR]/)
1300513004
");
1300613005

1300713006
Ok(())

0 commit comments

Comments
 (0)