Skip to content

Commit c0945d1

Browse files
committed
Only write .python-version files during uv init for workspace members if the version differs
1 parent 00fb6d0 commit c0945d1

File tree

1 file changed

+34
-34
lines changed
  • crates/uv/src/commands/project

1 file changed

+34
-34
lines changed

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@ async fn init_project(
524524
name,
525525
path,
526526
&requires_python,
527-
python_request.as_ref(),
528527
vcs,
529528
build_backend,
530529
author_from,
@@ -571,6 +570,40 @@ async fn init_project(
571570
workspace.install_path().simplified_display().cyan()
572571
)?;
573572
}
573+
// Write .python-version if it doesn't exist in the workspace or if the version differs
574+
if let Some(python_request) = python_request {
575+
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
576+
.await?
577+
.filter(|file| {
578+
file.version()
579+
.is_some_and(|version| *version == python_request)
580+
&& file.path().parent().is_some_and(|parent| {
581+
parent == workspace.install_path() || parent == path
582+
})
583+
})
584+
.is_none()
585+
{
586+
PythonVersionFile::new(path.join(".python-version"))
587+
.with_versions(vec![python_request.clone()])
588+
.write()
589+
.await?;
590+
}
591+
}
592+
} else {
593+
// Write .python-version if it doesn't exist in the project directory.
594+
if let Some(python_request) = python_request {
595+
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
596+
.await?
597+
.filter(|file| file.version().is_some())
598+
.filter(|file| file.path().parent().is_some_and(|parent| parent == path))
599+
.is_none()
600+
{
601+
PythonVersionFile::new(path.join(".python-version"))
602+
.with_versions(vec![python_request.clone()])
603+
.write()
604+
.await?;
605+
}
606+
}
574607
}
575608

576609
Ok(())
@@ -615,7 +648,6 @@ impl InitProjectKind {
615648
name: &PackageName,
616649
path: &Path,
617650
requires_python: &RequiresPython,
618-
python_request: Option<&PythonRequest>,
619651
vcs: Option<VersionControlSystem>,
620652
build_backend: Option<ProjectBuildBackend>,
621653
author_from: Option<AuthorFrom>,
@@ -628,7 +660,6 @@ impl InitProjectKind {
628660
name,
629661
path,
630662
requires_python,
631-
python_request,
632663
vcs,
633664
build_backend,
634665
author_from,
@@ -642,7 +673,6 @@ impl InitProjectKind {
642673
name,
643674
path,
644675
requires_python,
645-
python_request,
646676
vcs,
647677
build_backend,
648678
author_from,
@@ -660,7 +690,6 @@ impl InitProjectKind {
660690
name: &PackageName,
661691
path: &Path,
662692
requires_python: &RequiresPython,
663-
python_request: Option<&PythonRequest>,
664693
vcs: Option<VersionControlSystem>,
665694
build_backend: Option<ProjectBuildBackend>,
666695
author_from: Option<AuthorFrom>,
@@ -716,21 +745,6 @@ impl InitProjectKind {
716745
}
717746
fs_err::write(path.join("pyproject.toml"), pyproject)?;
718747

719-
// Write .python-version if it doesn't exist in the target or is empty.
720-
if let Some(python_request) = python_request {
721-
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
722-
.await?
723-
.filter(|file| file.version().is_some())
724-
.filter(|file| file.path().parent().is_some_and(|parent| parent == path))
725-
.is_none()
726-
{
727-
PythonVersionFile::new(path.join(".python-version"))
728-
.with_versions(vec![python_request.clone()])
729-
.write()
730-
.await?;
731-
}
732-
}
733-
734748
// Initialize the version control system.
735749
init_vcs(path, vcs)?;
736750

@@ -743,7 +757,6 @@ impl InitProjectKind {
743757
name: &PackageName,
744758
path: &Path,
745759
requires_python: &RequiresPython,
746-
python_request: Option<&PythonRequest>,
747760
vcs: Option<VersionControlSystem>,
748761
build_backend: Option<ProjectBuildBackend>,
749762
author_from: Option<AuthorFrom>,
@@ -772,19 +785,6 @@ impl InitProjectKind {
772785
// Generate `src` files
773786
generate_package_scripts(name, path, build_backend, true)?;
774787

775-
// Write .python-version if it doesn't exist.
776-
if let Some(python_request) = python_request {
777-
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
778-
.await?
779-
.is_none()
780-
{
781-
PythonVersionFile::new(path.join(".python-version"))
782-
.with_versions(vec![python_request.clone()])
783-
.write()
784-
.await?;
785-
}
786-
}
787-
788788
// Initialize the version control system.
789789
init_vcs(path, vcs)?;
790790

0 commit comments

Comments
 (0)