Skip to content

Commit 6a3adaa

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

File tree

1 file changed

+67
-77
lines changed
  • crates/uv/src/commands/project

1 file changed

+67
-77
lines changed

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

Lines changed: 67 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,16 @@ async fn init_project(
519519
(requires_python, python_request)
520520
};
521521

522-
project_kind
523-
.init(
524-
name,
525-
path,
526-
&requires_python,
527-
python_request.as_ref(),
528-
vcs,
529-
build_backend,
530-
author_from,
531-
no_readme,
532-
package,
533-
)
534-
.await?;
522+
project_kind.init(
523+
name,
524+
path,
525+
&requires_python,
526+
vcs,
527+
build_backend,
528+
author_from,
529+
no_readme,
530+
package,
531+
)?;
535532

536533
if let Some(workspace) = workspace {
537534
if workspace.excludes(path)? {
@@ -571,6 +568,40 @@ async fn init_project(
571568
workspace.install_path().simplified_display().cyan()
572569
)?;
573570
}
571+
// Write .python-version if it doesn't exist in the workspace or if the version differs
572+
if let Some(python_request) = python_request {
573+
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
574+
.await?
575+
.filter(|file| {
576+
file.version()
577+
.is_some_and(|version| *version == python_request)
578+
&& file.path().parent().is_some_and(|parent| {
579+
parent == workspace.install_path() || parent == path
580+
})
581+
})
582+
.is_none()
583+
{
584+
PythonVersionFile::new(path.join(".python-version"))
585+
.with_versions(vec![python_request.clone()])
586+
.write()
587+
.await?;
588+
}
589+
}
590+
} else {
591+
// Write .python-version if it doesn't exist in the project directory.
592+
if let Some(python_request) = python_request {
593+
if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default())
594+
.await?
595+
.filter(|file| file.version().is_some())
596+
.filter(|file| file.path().parent().is_some_and(|parent| parent == path))
597+
.is_none()
598+
{
599+
PythonVersionFile::new(path.join(".python-version"))
600+
.with_versions(vec![python_request.clone()])
601+
.write()
602+
.await?;
603+
}
604+
}
574605
}
575606

576607
Ok(())
@@ -610,57 +641,46 @@ impl InitKind {
610641

611642
impl InitProjectKind {
612643
/// Initialize this project kind at the target path.
613-
async fn init(
644+
fn init(
614645
self,
615646
name: &PackageName,
616647
path: &Path,
617648
requires_python: &RequiresPython,
618-
python_request: Option<&PythonRequest>,
619649
vcs: Option<VersionControlSystem>,
620650
build_backend: Option<ProjectBuildBackend>,
621651
author_from: Option<AuthorFrom>,
622652
no_readme: bool,
623653
package: bool,
624654
) -> Result<()> {
625655
match self {
626-
InitProjectKind::Application => {
627-
self.init_application(
628-
name,
629-
path,
630-
requires_python,
631-
python_request,
632-
vcs,
633-
build_backend,
634-
author_from,
635-
no_readme,
636-
package,
637-
)
638-
.await
639-
}
640-
InitProjectKind::Library => {
641-
self.init_library(
642-
name,
643-
path,
644-
requires_python,
645-
python_request,
646-
vcs,
647-
build_backend,
648-
author_from,
649-
no_readme,
650-
package,
651-
)
652-
.await
653-
}
656+
InitProjectKind::Application => InitProjectKind::init_application(
657+
name,
658+
path,
659+
requires_python,
660+
vcs,
661+
build_backend,
662+
author_from,
663+
no_readme,
664+
package,
665+
),
666+
InitProjectKind::Library => InitProjectKind::init_library(
667+
name,
668+
path,
669+
requires_python,
670+
vcs,
671+
build_backend,
672+
author_from,
673+
no_readme,
674+
package,
675+
),
654676
}
655677
}
656678

657679
/// Initialize a Python application at the target path.
658-
async fn init_application(
659-
self,
680+
fn init_application(
660681
name: &PackageName,
661682
path: &Path,
662683
requires_python: &RequiresPython,
663-
python_request: Option<&PythonRequest>,
664684
vcs: Option<VersionControlSystem>,
665685
build_backend: Option<ProjectBuildBackend>,
666686
author_from: Option<AuthorFrom>,
@@ -716,34 +736,17 @@ impl InitProjectKind {
716736
}
717737
fs_err::write(path.join("pyproject.toml"), pyproject)?;
718738

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-
734739
// Initialize the version control system.
735740
init_vcs(path, vcs)?;
736741

737742
Ok(())
738743
}
739744

740745
/// Initialize a library project at the target path.
741-
async fn init_library(
742-
self,
746+
fn init_library(
743747
name: &PackageName,
744748
path: &Path,
745749
requires_python: &RequiresPython,
746-
python_request: Option<&PythonRequest>,
747750
vcs: Option<VersionControlSystem>,
748751
build_backend: Option<ProjectBuildBackend>,
749752
author_from: Option<AuthorFrom>,
@@ -772,19 +775,6 @@ impl InitProjectKind {
772775
// Generate `src` files
773776
generate_package_scripts(name, path, build_backend, true)?;
774777

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-
788778
// Initialize the version control system.
789779
init_vcs(path, vcs)?;
790780

0 commit comments

Comments
 (0)